博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ElasticSearch6(三)-- Java API实现简单的增删改查
阅读量:4685 次
发布时间:2019-06-09

本文共 4308 字,大约阅读时间需要 14 分钟。

基于ElasticSearch6.2.4, Java API创建索引、查询、修改、删除,pom依赖和获取es连接 可查看

package com.xsjt.learn;import java.io.IOException;import java.net.InetAddress;import java.text.SimpleDateFormat;import java.util.Date;import org.elasticsearch.action.delete.DeleteResponse;import org.elasticsearch.action.get.GetResponse;import org.elasticsearch.action.index.IndexResponse;import org.elasticsearch.action.update.UpdateResponse;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.common.transport.TransportAddress;import org.elasticsearch.common.xcontent.XContentType;import org.elasticsearch.transport.client.PreBuiltTransportClient;import org.junit.After;import org.junit.Before;import org.junit.Test;import com.alibaba.fastjson.JSONObject;/** * ClassName:Crud 简单的增删改查 Date: 2018年3月21日 下午12:51:15 *  * @author xbq * @version * @since JDK 1.8 */public class Crud {    public final static String HOST = "192.168.10.58";    // http请求的端口是9200,客户端是9300    public final static int PORT = 9300;     private TransportClient client = null;    /**     * getConnection:(获取连接).     *      * @author xbq Date:2018年3月21日下午4:03:32     *     * @throws Exception     */    @SuppressWarnings({ "resource", "unchecked" })    @Before    public void getConnection() throws Exception {        // 设置集群名称        Settings settings = Settings.builder().put("cluster.name", "nmtx-cluster").build();        // 创建client        client = new PreBuiltTransportClient(settings)                .addTransportAddresses(new TransportAddress(InetAddress.getByName(HOST), PORT));    }        /**     * closeConnection:(关闭连接).     *      * @author xbq Date:2018年3月21日下午4:03:45     *     */    @After    public void closeConnection() {        if (client != null) {            client.close();        }    }    /**     * testIndex:(创建索引).     *      * @author xbq Date:2018年3月21日下午4:04:16     * @throws IOException     *     */    @Test    public void testCreateIndex() throws IOException {        JSONObject jsonObject = new JSONObject();        jsonObject.put("orderNo", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "001");        jsonObject.put("orderName", "购买元宝");        jsonObject.put("orderTime", new Date());        jsonObject.put("price", 1.5);        jsonObject.put("ip", "192.168.1.111");                IndexResponse response = client.prepareIndex("rxpay", "order").setSource(jsonObject.toString(), XContentType.JSON).get();        System.out.println("索引名称:" + response.getIndex());        System.out.println("类型:" + response.getType());        System.out.println("文档ID:" + response.getId()); // 第一次使用是1        System.out.println("当前实例状态:" + response.status());    }        /**     * testQuery:(查询).       * @author xbq     * Date:2018年5月3日下午6:04:22     */    @Test    public void testQuery() {        try {            GetResponse response = client.prepareGet("rxpay", "order", "6W2QKmMBhrIcTC9dgt7A").execute().actionGet();            System.out.println(response.getSourceAsString());        } catch (Exception e) {            e.printStackTrace();        }    }        /**     * testUpdate:(更新).       * @author xbq     * Date:2018年5月3日下午6:07:58     */    @Test    public void testUpdate() {        JSONObject json = new JSONObject();        json.put("user", "Joe");        json.put("age", "22");        json.put("sex", "男");        json.put("orderTime", "6666666");        UpdateResponse response = client.prepareUpdate("rxpay", "order", "6W2QKmMBhrIcTC9dgt7A").setDoc(json.toJSONString(), XContentType.JSON).get();        System.out.println("索引名称:" + response.getIndex());        System.out.println("类型:" + response.getType());        System.out.println("文档ID:" + response.getId());        System.out.println("当前实例状态:" + response.status());    }        /**     * testDelete:(删除).       * @author xbq     * Date:2018年5月4日下午5:44:32     */    @Test    public void testDelete() {        DeleteResponse response = client.prepareDelete("rxpay", "order", "6W2QKmMBhrIcTC9dgt7A").get();        System.out.println("索引名称:" + response.getIndex());        System.out.println("类型:" + response.getType());        System.out.println("文档ID:" + response.getId());        System.out.println("当前实例状态:" + response.status());    }}

 

转载于:https://www.cnblogs.com/xbq8080/p/8995051.html

你可能感兴趣的文章
Tomcat6启用Gzip压缩功能
查看>>
Java字节码浅析(二)
查看>>
Vue选项卡
查看>>
http协议和四个层之间的关系
查看>>
(3)剑指Offer之数值的整数次方和调整数组元素顺序
查看>>
MongoDB学习笔记(索引)
查看>>
iOS - UIView
查看>>
VIM7.3设置(for Windows)
查看>>
[bzoj 1143]最长反链二分图最大匹配
查看>>
SpringBoot(一)
查看>>
Azure Powershell script检测登陆并部署ARM Template
查看>>
SSO
查看>>
【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters
查看>>
常用git命令
查看>>
深入了解HTTP协议、HTTP协议原则
查看>>
软件开发者最重要的四大技能(转)
查看>>
redis集群【转】
查看>>
get、put、post、delete含义与区别
查看>>
JS中innerHTML,innerText,value区别
查看>>
taglib.jsp
查看>>