zoukankan      html  css  js  c++  java
  • TransportClient 基础CRUD

    插入数据时,可以自定义路由routing
    参考:https://blog.csdn.net/jatpen/article/details/102632745

        /**
         * 简单查询es 指定index type id
         */
        @Test
        public void search() throws UnknownHostException {
            //获取client
            Settings settings= Settings.builder().put("cluster.name","myes").build();
            TransportClient client=new PreBuiltTransportClient(settings);
            client.addTransportAddress(
                    new TransportAddress(InetAddress.getByName("localhost"),9300));
            // 发起请求得到响应
            GetResponse  response=client.prepareGet("index1","type1","6")
                //指定自定义路由routing1
                    .setRouting("routing1").get();
        }
    
        /**
         * 增加文档
         */
        @Test
        public void insert() throws Exception{
            Settings settings= Settings.builder().put("cluster.name","myes").build();
            TransportClient client=new PreBuiltTransportClient(settings);
            client.addTransportAddress(
                    new TransportAddress(InetAddress.getByName("localhost"),9300));
            XContentBuilder contentBuilder= XContentFactory.jsonBuilder()
                    .startObject()
                    .field("author","cchilei")
                    .field("id","8")
                    .field("title","插入id=8")
                    .endObject();
            IndexResponse indexResponse = client.prepareIndex("index1", "type1")
                    .setSource(contentBuilder)
                    .get();
        }
    
        /**
         * 删除文档
         */
        @Test
        public void delete() throws Exception{
            Settings settings=Settings.builder().put("cluster.name","myes").build();
            TransportClient client=new PreBuiltTransportClient(settings);
            client.addTransportAddress(
                    new TransportAddress(InetAddress.getByName("localhost"),9300));
            DeleteResponse deleteResponse = client
                    .prepareDelete("index1", "type1", "8").get();
        }
    
        /**
         * 修改文档
         */
        @Test
        public void update() throws Exception {
            Settings settings = Settings.builder().put("cluster.name", "myes").build();
            TransportClient client = new PreBuiltTransportClient(settings);
            client.addTransportAddress(
                    new TransportAddress(InetAddress.getByName("localhost"), 9300));
    
            UpdateRequest request = new UpdateRequest();
            XContentBuilder contentBuilder = XContentFactory.jsonBuilder()
                    .startObject()
                    .field("id", "6868")
                    .endObject();
            request.index("index1")
                    .type("type1")
                    .id("6")
                    .doc(contentBuilder);
            UpdateResponse updateResponse = client.update(request).get();
        }
    
  • 相关阅读:
    《Java程序设计》 第一周学习任务(2)
    《Java程序设计》 第一周学习任务(1)
    Git 提示fatal: remote origin already exists 解决办法
    写给小白的酸酸乳使用方法
    美國Tarrant County College
    硬盘数据恢复工具终身版
    安卓手机系统安装虚拟机
    linux网络基础
    Linux基础命令:read
    Linux shell基础
  • 原文地址:https://www.cnblogs.com/cchilei/p/13612837.html
Copyright © 2011-2022 走看看