Hi
I am trying to push bulk of Json fles to ElasticSearch. i am using elasticsearch 1.5.2 version for bulk push using Java API.
public class TestIndex {
public static void main(String[] args) throws ParseException, IOException, org.json.simple.parser.ParseException {
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", "test-cluster").build();
Client client = new TransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress("localhost", 9200));
String hostname = "localhost";
int port = 9200;
// bulk API
BulkRequestBuilder bulkBuilder = client.prepareBulk();
long bulkBuilderLength = 0;
String index = "testindex";
String type = "string";
String _id = null;
BufferedReader br =null;
try{
String readLine ;
br = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream("accounts.json"))));
JSONParser parser = new JSONParser();
StringBuilder out = new StringBuilder();
while((readLine = br.readLine()) != null){
System.out.println(readLine);
String newLine = System.getProperty("\n");
out.append(readLine);
out.append(newLine);
if (readLine.startsWith("{"index")){
continue;
} else {
Object json = parser.parse(readLine);
if(((JSONObject)json).get("account_number") != null){
_id = String.valueOf(((JSONObject)json).get("account_number"));
System.out.println(_id);
}
JSONObject jsonObject = (JSONObject) json;
bulkBuilder.add(client.prepareIndex(index, type, String.valueOf(_id)).setSource(jsonObject));
bulkBuilderLength++;
}
}}catch(Exception e){}
br.close();
if(bulkBuilder.numberOfActions() > 0){
System.out.println("##### " + bulkBuilderLength + " data indexed.");
BulkResponse bulkRes = (BulkResponse) bulkBuilder.execute();
if(bulkRes.hasFailures()){
System.out.println("##### Bulk Request failure with error: " + bulkRes.buildFailureMessage());
}
bulkBuilder = client.prepareBulk();}}}
Facing Issue :
Aug 27, 2016 7:47:38 PM org.elasticsearch.plugins.PluginsService
INFO: [Watcher] loaded [], sites []
Aug 27, 2016 7:47:44 PM org.elasticsearch.client.transport.TransportClientNodesService$SimpleNodeSampler doSample
INFO: [Watcher] failed to get node info for [#transport#-1][bigtapp-Lenovo-B40-80][inet[localhost/127.0.0.1:9200]], disconnecting...
org.elasticsearch.transport.ReceiveTimeoutTransportException: [][inet[localhost/127.0.0.1:9200]][cluster:monitor/nodes/info] request_id [0] timed out after [5001ms]
at org.elasticsearch.transport.TransportService$TimeoutHandler.run(TransportService.java:529)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
{"index":{"_id":"1"}}
{"account_number":1,"balance":39225,"firstname":"Amber"}
1
{"index":{"_id":"7"}}
{"account_number":7,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M"}
7
{"index":{"_id":"13"}}
{"account_number":13,"balance":32838,"firstname":"Nanette","lastname":"Bates","age":28,"gender":"F",
2 data indexed.
Exception in thread "main" org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: []
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:305)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:200)
at org.elasticsearch.client.transport.support.InternalTransportClient.execute(InternalTransportClient.java:106)
at org.elasticsearch.client.support.AbstractClient.bulk(AbstractClient.java:163)
at org.elasticsearch.client.transport.TransportClient.bulk(TransportClient.java:356)
at org.elasticsearch.action.bulk.BulkRequestBuilder.doExecute(BulkRequestBuilder.java:164)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:91)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:65)
at ElasticSearch.ElasticSearch.TestIndex.main(TestIndex.java:90)
My sample json File (accounts.json)
{"index":{"_id":"1"}}
{"account_number":1,"balance":39225,"firstname":"Amber"}
{"index":{"_id":"7"}}
{"account_number":7,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M"}
{"index":{"_id":"13"}}
{"account_number":13,"balance":32838,"firstname":"Nanette","lastname":"Bates","age":28,"gender":"F",
"address":"789 Madison Street","employer":"Quility","email":"nanettebates@quility.com","city":"Nogal","state":"VA"}