【问题标题】:How to do execute this curl command in my java code?如何在我的 java 代码中执行这个 curl 命令?
【发布时间】:2016-03-31 02:37:17
【问题描述】:

我有以下 curl 命令:

curl -XPOST 'localhost:9200/indexname/status/_search?pretty=1&size=1000000&q=date:"2012/10/10"' -d '{"_source": {"include": [ "ID", "Name" ]}}'

我的java代码是这样的:

public static void main(String[] args) throws IOException{

ProcessBuilder pb = new ProcessBuilder("curl","-g","-H", "Accept:application/json", "-XPOST","localhost:9200/indexname/status/_search?pretty=1&size=10000","-d '{\"_source\": {\"include\": [ \"ID\", \"Name\" ]}}'");

  pb.directory(new File("localpath"));
  pb.redirectErrorStream(true);
  Process p = pb.start();
  InputStream is = p.getInputStream();

  FileOutputStream outputStream = new FileOutputStream(
          "localpath/data.json");

  String line;
  BufferedInputStream bis = new BufferedInputStream(is);
  byte[] bytes = new byte[100];
  int numberByteReaded;
  while ((numberByteReaded = bis.read(bytes, 0, 100)) != -1) {

      outputStream.write(bytes, 0, numberByteReaded);
      Arrays.fill(bytes, (byte) 0);
  }
  outputStream.flush();
  outputStream.close();
 }

在执行代码时,在存储在本地驱动器中的 data.json 文件中,我看到了这个错误:

{
   "error" : "SearchPhaseExecutionException[Failed to execute phase [query],  all shards failed; shardFailures {[EsJClybMTnONMBSDBIfS9g][indexname][0]: SearchParseException[[indexname][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [ '{\"_source\": {\"include\": [ \"ID\", \"Name\" ]}}']]]; nested: JsonParseException[Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B@1f7c7994; line: 1, column: 3]]; }{[EsJClybMTnONMBSDBIfS9g][indexname][1]: SearchParseException[[indexname][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [ '{\"_source\": {\"include\": [ \"ID\", \"Name\" ]}}']]]; nested: JsonParseException[Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B@1f7c7994; line: 1, column: 3]]; }{[EsJClybMTnONMBSDBIfS9g][indexname][2]: SearchParseException[[indexname][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [ '{\"_source\": {\"include\": [ \"ID\", \"Name\" ]}}']]]; nested: JsonParseException[Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B@1f7c7994; line: 1, column: 3]]; }{[EsJClybMTnONMBSDBIfS9g][indexname][3]: SearchParseException[[indexname][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [ '{\"_source\": {\"include\": [ \"ID\", \"Name\" ]}}']]]; nested: JsonParseException[Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B@1f7c7994; line: 1, column: 3]]; }{[EsJClybMTnONMBSDBIfS9g][indexname][4]: SearchParseException[[indexname][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [ '{\"_source\": {\"include\": [ \"ID\", \"Name\" ]}}']]]; nested: JsonParseException[Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B@1f7c7994; line: 1, column: 3]]; }]",
 "status" : 400
}

知道为什么会这样吗?我想存储查询中提到的日期的 ID 和名称键值对。有什么建议吗?谢谢。

【问题讨论】:

标签: java json curl elasticsearch


【解决方案1】:

您可以使用ES java client 完成此操作

client = new TransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress(HOST, PORT));

String queryString = "{\"size\":10,  \"query\": { \"term\": {\"date\": \"VALUE\"}},\"_source\": {\"include\": [ \"ID\", \"Name\" ]}}";
SearchResponse response = client.prepareSearch("indexname").setTypes("status").setSource(queryString)
                .execute().actionGet();

请阅读java client for elasticsearch。对你玩 ES java api 会有帮助。

【讨论】:

    猜你喜欢
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2016-06-22
    • 2012-07-08
    • 1970-01-01
    相关资源
    最近更新 更多