我有一个Postgresql 9.1表,其中包含“city”,“admin1”和“country”列。我想选择输出格式为“city,admin1,country”的唯一值。
我可能有纽约和明尼苏达州罗切斯特的多个条目,但我只想要其中一个。这是为了填充 flex 搜索索引,而我正在使用JDBC river plugin
以下SELECT在PGAdmin3的SQL编辑器中执行我想要的操作:
SELECT city || ', ' || admin1 || ', ' || country AS city FROM table GROUP BY city, admin1, country;
但在以下方面失败
curl -XPUT 'localhost:9200/_river/nibbel_river/_meta' -d '{
"type" : "jdbc",
"jdbc" : {
"driver" : "org.postgresql.Driver",
"url" : "jdbc:postgresql://localhost:5432/table",
"user" : "",
"password" : "",
"sql" : "SELECT city || ', ' || admin1 || ', ' || country AS city FROM everything GROUP BY city, admin1, country",
"poll" : "7d"
},
"index" : {
"index" : "city",
"type" : "everything"
}
}'
与错误
curl: (6) Couldn't resolve host ' || admin1 || ,'
curl: (3) [globbing] unmatched close brace/bracket at pos 102
{"error":"MapperParsingException[Failed to parse [jdbc.sql]]; nested: JsonParseException[Unexpected end-of-input in VALUE_STRING\n at [Source: [B@1192b0c; line: 8, column: 565]]; ","status":400}
这是什么语法?
最佳答案
查询中的所有撇号都必须像这样进行转义:'\''
例如:
curl -XPUT 'localhost:9200/_river/nibbel_river/_meta' -d '{
"type" : "jdbc",
"jdbc" : {
"driver" : "org.postgresql.Driver",
"url" : "jdbc:postgresql://localhost:5432/table",
"user" : "",
"password" : "",
"sql" : "SELECT city || '\'', '\'' || admin1 || '\'', '\'' || country AS city FROM everything GROUP BY city, admin1, country",
"poll" : "7d"
},
"index" : {
"index" : "city",
"type" : "everything"
}
}'
关于curl - curl中的PostgreSQL查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13443628/