java操作elasticsearch实现批量添加数据(bulk)
java操作elasticsearch实现批量添加主要使用了bulk
代码如下:
//bulk批量操作(批量添加) @Test public void test7() throws IOException { //1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称 Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.创建访问ES服务器的客户端 TransportClient client = new PreBuiltTransportClient(settings) //获取es主机中节点的ip地址及端口号(以下是单个节点案例) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); BulkRequestBuilder builder = client.prepareBulk(); IndexRequestBuilder request = client.prepareIndex("lib2","books","8").setSource( XContentFactory.jsonBuilder() .startObject() .field("title","python") .field("price",99) .endObject() ); IndexRequestBuilder request2 = client.prepareIndex("lib2","books","9").setSource( XContentFactory.jsonBuilder() .startObject() .field("title","VR") .field("price",29) .endObject() ); builder.add(request); builder.add(request2);
//该方法ES默认是分片1秒钟后刷新,即插入成功后马上查询,插入的数据不能马上被查出 BulkResponse response = builder.get(); System.out.println(response.status()); if(response.hasFailures()) { System.out.println("操作失败"); } }
下一篇博客本人将书写java操作elasticsearch实现查询删除及查询所有。对后期博客感兴趣的朋友可以关注交流,转发请说明出处,本人的博客地址为:https://www.cnblogs.com/chenyuanbo/
技术在于交流!