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();
    
        }
    

      

  • 相关阅读:
    PDF数据提取------2.相关类介绍
    Google搜索的常用技巧
    a helper class for generating xls or csv file
    正则 提取html标签value
    获取 windows地址栏 网页地址栏 文件名
    MSSQL将逗号分隔的字符串转换成列显示
    C# String.Format字符串中包含"{" "}"时需注意的问题
    格式化JSON中时间
    Oracle 10G创建表空间
    Sqlserver取最近一分组中最新一条记录
  • 原文地址:https://www.cnblogs.com/shenhaha520/p/13606621.html
Copyright © 2011-2022 走看看