JAVA API调用elasticsearch实现基本增删改查,apielasticsearch
elasticsearch支持很多api的操作,这边先简单的介绍一下Java增删改查的API操作。想要学习更多的操作,可以阅读官网api文档。官网地址:https://www.elastic.co/guide/en/elasticsearch/client/index.html1.下面开始具体的内容介绍,首先是创建索引,具体代码如下:/** * 创建索引库 * @return void * 索引库的名称必须为小写 * @throws IOException * @Title: addIndex1 */@Testpublic void addIndex1() throws IOException { IndexResponse response = client.prepareIndex("msg", "tweet", "1").setSource(XContentFactory.jsonBuilder() .startObject().field("name", "linzhiqiang") .field("date", new Date()) .field("msg", "hello world") .endObject()).get(); System.out.println("索引名称:" + response.getIndex() + "\n类型:" + response.getType() + "\n文档ID:" + response.getId() + "\n当前实例状态:" + response.status());}/** * 添加索引:传入json字符串 * @return void * @Title: addIndex2 */@Testpublic void addIndex2() { String jsonStr = "{" + "\"userName\":\"张三\"," + "\"sendDate\":\"2017-11-30\"," + "\"msg\":\"你好李四\"" + "}"; IndexResponse response = client.prepareIndex("weixin", "tweet").setSource(jsonStr, XContentType.JSON).get(); System.out.println("json索引名称:" + response.getIndex() + "\njson类型:" + response.getType() + "\njson文档ID:" + response.getId() + "\n当前实例json状态:" + response.status());
}
/** * 创建索引-传入Map对象 * @return void * @Title: addIndex3 */@Testpublic void addIndex3() { Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "小妹" ); map.put("age",18); map.put("sex", "女"); map.put("address", "广东省广州市天河区上社"); map.put("phone", "15521202233"); map.put("height", "175"); map.put("weight", "60"); IndexResponse response = client.prepareIndex("species", "person").setSource(map).get(); System.out.println("map索引名称:" + response.getIndex() + "\n map类型:" + response.getType() + "\n map文档ID:" + response.getId() + "\n当前实例map状态:" + response.status());}
/** * 传递json对象 * 需要添加依赖:gson * @return void * @Title: addIndex4 */@Testpublic void addIndex4() { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("userName", "张三"); jsonObject.addProperty("sendDate", "2017-11-23"); jsonObject.addProperty("msg", "你好李四"); IndexResponse response = client.prepareIndex("qq", "tweet").setSource(jsonObject, XContentType.JSON).get(); System.out.println("jsonObject索引名称:" + response.getIndex() + "\n jsonObject类型:" + response.getType() + "\n jsonObject文档ID:" + response.getId() + "\n当前实例jsonObject状态:" + response.status());}
我们可以创建Json、Map、JsonObject、自定义字段等,创建好之后可以去ES系统中查看是否添加成功。如果不进行分片设置每次创建默认有五个分片数量,具体情况如下图所示。2.创建好之后,我们可以通过ES提供的API进行相应的查询操作,具体代码如下所示:/** * 从索引库获取数据 * * @return void * @Title: query */@Testpublic void query() { GetResponse getResponse = client.prepareGet("species", "person", "AWNtYjiVjqSYg4HhYcQZ").get(); System.out.println("索引库的数据:" + getResponse.getSourceAsString());}查询的结果如下所示:ES上面对应索引的数据如下所示:3.下面我们将上面查询到的id(AWNtYjiVjqSYg4HhYcQZ)进行相应的修改,修改索引的API操作如下代码所示:/** * 更新索引库数据 * @Title: updateData * @return void */@Testpublic void updateData() { Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "大妹" ); map.put("age",20); map.put("sex", "女"); map.put("address", "广东省广州市天河区上社"); map.put("phone", "15521202233"); map.put("height", "180"); map.put("weight", "70"); UpdateResponse updateResponse = client.prepareUpdate("species", "person", "AWNtYjiVjqSYg4HhYcQZ") .setDoc(map).get(); System.out.println("updateResponse索引名称:" + updateResponse.getIndex() + "\n updateResponse类型:" + updateResponse.getType() + "\n updateResponse文档ID:" + updateResponse.getId() + "\n当前实例updateResponse状态:" + updateResponse.status());}我们在重新查询一下id为:AWNtYjiVjqSYg4HhYcQZ的索引文档,看一下数据是否已经修改。结果如下所示:我们从上面的截图可以看出数据确实已经修改完毕了,证明修改的API操作是成功的。4.最后我们看一下最后的删除索引操作,具体的代码如下所示:/** * 根据索引名称,类别,文档ID 删除索引库的数据 * @Title: deleteData * @return void */@Testpublic void deleteData() { DeleteResponse deleteResponse = client.prepareDelete("species", "person", "AWNtYjiVjqSYg4HhYcQZ").get(); System.out.println("deleteResponse索引名称:" + deleteResponse.getIndex() + "\n deleteResponse类型:" + deleteResponse.getType() + "\n deleteResponse文档ID:" + deleteResponse.getId() + "\n当前实例deleteResponse状态:" + deleteResponse.status());}下面我们重新执行一下查询id为:AWNtYjiVjqSYg4HhYcQZ看看是否还可以查询到对应的数据。结果如下所示:
可以看到不管是查询结果还是ES数据都没有那条对应的数据了,这证明我们删除的API操作是成功的。大家可能会有疑问,问什么都没有看到连接ES系统的代码,因为我这边代码是写在Test测试用例中,所以每一个操作都需要连接到ES系统这个操作。所以这边我抽象出来变成父类,具体代码如下所示:public class ESTest { /** * 192.168.11.24 测试IP地址 * 47.106.165.2 外网IP地址 */ public final static String HOST = "192.168.11.24";
public TransportClient client = null;
public final static int PORT = 9300;
/** * 获取客户端连接信息 * * @return void * @throws UnknownHostException * @Title: getConnect */ @SuppressWarnings({"resource", "unchecked"}) @Before public void getConnect() { try { Settings settings = Settings.builder() .put("cluster.name", "my-application").build(); client = new PreBuiltTransportClient(settings).addTransportAddress( new InetSocketTransportAddress(InetAddress.getByName(HOST), PORT)); } catch (Exception e) { e.printStackTrace(); } }
/** * 关闭连接 * * @return void * @Title: closeConnect */ @After public void closeConnect() { if (null != client) { client.close(); } }}上面的IP地址是对应测试和生产的IP地址,不管是生产还是测试的ES系统,Java对应的API操作的端口都是9300,记住是9300!!!这样简单的JAVA API调用elasticsearch实现基本增删改查就完成了,当然这个是基础的不能再基础的东西,后面还有关键的查询操作、多索引聚合操作、批量操作等等之类的操作。
对文章有什么疑问或者想要看更多文章可以加我订阅号,欢迎大家的踩踩~
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。