如何在我的java代码中执行这个curl命令?

时间:2016-03-31 02:37:17

标签: java json curl elasticsearch

我有以下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和名称键值对。有什么建议?感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用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。