2016-03-31 10 views
0

Aşağıdaki kıvırın komutunu vardır:Bu curl komutunu java kodumda nasıl yaparım?

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

Benim java kodu şu şekildedir:

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(); 
} 

benim yerel sürücüde depolanan Data.JSON dosyasında, kod yürütülürken, bunu görüyorum hata:

{ 
    "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: [[email protected]; 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: [[email protected]; 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: [[email protected]; 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: [[email protected]; 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: [[email protected]; line: 1, column: 3]]; }]", 
"status" : 400 
} 

Bunun neden olduğu hakkında bir fikriniz var mı? Kimlik ve Ad Anahtarı değer çiftlerini sorguda belirtilen tarihten itibaren saklamak istiyorum. Herhangi bir tavsiye? Teşekkürler.

+0

Neden harici bir işlem kullanmak yerine bu POST'u gerçekleştirmek için java sınıflarını kullanmıyorsunuz? –

+0

Lütfen bana bir örnek göster? – Sweet

+0

Sanırım bu, http://stackoverflow.com/questions/3324717/sending-http-post-request-in-java adresinde yardımcı olabilir –

cevap

0

Sen 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 okuyunuz kullanarak bunu yapabilirsiniz. ES java api ile oynamak için size yardımcı olabilir.