Skip to content

Instantly share code, notes, and snippets.

@nmunro-cvt
Created April 2, 2015 18:56
Show Gist options
  • Save nmunro-cvt/0be42236e9abf4359a44 to your computer and use it in GitHub Desktop.
Save nmunro-cvt/0be42236e9abf4359a44 to your computer and use it in GitHub Desktop.
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import java.util.Date;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.client.transport.TransportClient;
import com.cvent.common.ElasticsearchClientFactory;
public class BulkRequestWithListener {
public static final void main(String[] args) throws Exception {
TransportClient client = ElasticsearchClientFactory.getTransportClient();
BulkRequestBuilder bulkRequest = client.prepareBulk();
bulkRequest.add(client.prepareIndex("twitter", "tweet", "2")
.setSource(jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", new Date())
.field("message", "another post")
.endObject()
)
);
bulkRequest.execute()
.addListener(new ActionListener<BulkResponse>() {
public void onResponse(BulkResponse response) {
System.out.println(">>>>> onResponse");
System.out.println(response.toString());
}
public void onFailure(Throwable e) {
System.out.println(">>>>> onFailure");
e.printStackTrace();
}
});
// keep main thread alive
Thread.sleep(10 * 1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment