zoukankan      html  css  js  c++  java
  • es增删改查

    es增删改查

    es增删改查
    
      /**
         * 存储ES数据  可用
         */
        @Test
        public void saveDataToEs(UploadInfo uploadInfo) {
            // 指定ES集群
            Settings settings = Settings.builder().put("cluster.name","jtcn-es").build();
            System.out.println(settings);
    
            TransportClient client = null;
            XContentBuilder doc = null;
            try {
                if (uploadInfo != null && uploadInfo.getData() != null) {
                    // 创建ES客户端,客户端端口号默认9300,不要写web端口9200
                    client = new PreBuiltTransportClient(settings)
                            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("34.115.11.11"), Integer.parseInt("9200")));
    
                    String[] array = uploadInfo.getData().split(',');
    
                    // 构建文档数据
                    doc = XContentFactory.jsonBuilder().startObject()
                            .field("id", UUID.randomUUID())
                            .field("gpsNbr", array[1])
                            .field("gpsInfoType", 1)
                            .field("policeId", array[2])
                            .field("policeType", array[3])
                            .field("lng", array[4])
                            .field("lat", array[5])
                            .array("localtion", array[4], array[5])
                            .field("updateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))
                            .field("speed", 0)
                            .field("publishTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))
                            .field("orgCode", array[10])
                            .field("isContain", 0)
                            .field("distance", 0)
                            .field("updateMin", System.currentTimeMillis()).endObject();
                    String nowDate = DateUtil.formatDate(DateUtil.DATE_FORMAT_YYYYMMDD);
                    // 添加文档
                    IndexResponse response = client.prepareIndex("tongling_police_gps_data_"+nowDate, "info", UUID.randomUUID().toString()).setSource(doc).get();
    //+nowDate
                    // 查看执行状态,创建成功后,之前没有CREATED,之前存在OK
                    System.out.println("response.status():" + response.status());
                }
            } catch (Exception ex) {
                System.out.print("保存数据到es发生异常: " + ex + "
    ");
            }
            client.close();
    
        }
    

      

     /**
         * 从es中获取数据  可用
         *
         * @throws
         */
        @Test
        public void queryEs() {
            // 指定ES集群
            Settings settings = Settings.builder().put("cluster.name", "jtcn-es").build();
            System.out.println(settings);
            // 创建ES客户端,客户端端口号默认9300,不要写web端口9200
            TransportClient client = null;
            try {
                client = new PreBuiltTransportClient(settings)
                        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("34.115.121.46"), 9300));
            } catch (Exception ex) {
                System.out.print("Consumer Kafka have an ERROR: " + ex + "
    ");
            }
    
            // 数据查询
            GetResponse response = client.prepareGet("tonglingIndex", "user", "100").execute().actionGet();
    
            System.out.println("response:" + response);
            System.out.println("response.getSourceAsString():" + response.getSourceAsString());
    
            client.close();
    
        }
    

      

  • 相关阅读:
    EasyHook库系列使用教程之四钩子的启动与停止
    inputclean插件的使用方法
    机器学习(十三)——机器学习中的矩阵方法(3)病态矩阵、协同过滤的ALS算法(1)
    <LeetCode OJ> 204. Count Primes
    leetcode 235: Lowest Common Ancestor of a Binary Search Tree
    数据结构经常使用算法
    调侃物联网开源框架,我们什么时候也来开源一个?
    字符编码简单介绍
    PriorityQueue ,ArrayList , 数组排序
    从struts2.1开始Convention零配置
  • 原文地址:https://www.cnblogs.com/shenhaha520/p/13606621.html
Copyright © 2011-2022 走看看