opendistro for es数据库

创建客户端

     public static RestHighLevelClient initESClient() {

            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

            SSLContext sc = null;

            try{
                sc = SSLContext.getInstance("SSL");
                sc.init(null, trustAllCerts, new SecureRandom());
            }catch(KeyManagementException e){
                e.printStackTrace();
            }catch(NoSuchAlgorithmException e){
                e.printStackTrace();
           }
            SSLIOSessionStrategy sessionStrategy = new SSLIOSessionStrategy(sc, new NullHostNameVerifier());
            SecuredHttpClientConfigCallback httpClientConfigCallback = new      
                    SecuredHttpClientConfigCallback(sessionStrategy,credentialsProvider);
            credentialsProvider.setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials("admin", "admin"));
            RestClientBuilder restClientBuilder = RestClient.builder(
                    new HttpHost("10.10.27.41", 9200, "https"))
                    .setHttpClientConfigCallback(httpClientConfigCallback);
            RestHighLevelClient client = new RestHighLevelClient(restClientBuilder);
            return client;
        }


        static TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }
            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        }};


        public static class NullHostNameVerifier implements HostnameVerifier {
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            }

SecuredHttpClientConfigCallback类

        class SecuredHttpClientConfigCallback implements  RestClientBuilder.HttpClientConfigCallback {
            @Nullable
            private final CredentialsProvider credentialsProvider;
            /**
             * The {@link SSLIOSessionStrategy} for all requests to enable SSL / TLS encryption.
             */
            private final SSLIOSessionStrategy sslStrategy;
            /**
             * Create a new {@link SecuredHttpClientConfigCallback}.
             *
             * @param credentialsProvider The credential provider, if a username/password have been supplied
             * @param sslStrategy The SSL strategy, if SSL / TLS have been supplied
             * @throws NullPointerException if {@code sslStrategy} is {@code null}
             */
            SecuredHttpClientConfigCallback(final SSLIOSessionStrategy sslStrategy,
                               @Nullable final CredentialsProvider credentialsProvider) {
                this.sslStrategy = Objects.requireNonNull(sslStrategy);
                this.credentialsProvider = credentialsProvider;
            }

            /**
             * Get the {@link CredentialsProvider} that will be added to the HTTP client.
             * @return Can be {@code null}.
             */
            @Nullable
            CredentialsProvider getCredentialsProvider() {
                return credentialsProvider;
            }

            /**
             * Get the {@link SSLIOSessionStrategy} that will be added to the HTTP client.
             *
             * @return Never {@code null}.
             */
            SSLIOSessionStrategy getSSLStrategy() {
                return sslStrategy;
            }

            /**
             * Sets the {@linkplain HttpAsyncClientBuilder#setDefaultCredentialsProvider(CredentialsProvider) credential provider},
             *
             * @param httpClientBuilder The client to configure.
             * @return Always {@code httpClientBuilder}.
             */

            @Override
            public HttpAsyncClientBuilder customizeHttpClient(final HttpAsyncClientBuilder httpClientBuilder) {
               // enable SSL / TLS
                httpClientBuilder.setSSLStrategy(sslStrategy)
                // enable user authentication
                if (credentialsProvider != null) {
                    httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
               }
               return httpClientBuilder;
            }
        }

创建索引
方法一:

    public void createIndex(StructuredLog logResult){

              RestHighLevelClient client = ElasticSearchIndexUtil.initESClient();

              SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

              long time = logResult.getTimestamp();

              Date d = new Date(time*1000);

              IndexRequest request = new IndexRequest(esConfig.getTemplatedLogIndex() + "_" + format.format(d), "_doc"); 
              try {
                    request.source(mapper.writeValueAsString(logResult), XContentType.JSON);
              } catch (JsonProcessingException e) {
                    e.printStackTrace();
              }
              IndexResponse indexResponse = null;
              try {
                    try {
                          indexResponse = client.index(request, RequestOptions.DEFAULT);
                    } catch (IOException e) {
                        e.printStackTrace();
                   }
                } catch (ElasticsearchException e) {
                    if (e.status() == RestStatus.CONFLICT) {
                        System.out.println("冲突了,请在此写冲突处理逻辑!" + e.getDetailedMessage());
                    }
                }
            }

方法二:

        public void createIndex(StructuredLog logResult){
              Map<String, Object> indexSchema = LogESDao.buildLogIndexSchema();
              RestHighLevelClient client = initESClient();
              GetIndexRequest gir = new GetIndexRequest().indices("test");
              try {
                    if (!client.indices().exists(gir, RequestOptions.DEFAULT)) {
                    CreateIndexRequest request = new CreateIndexRequest("test");
                    request.mapping("_doc", indexSchema);
                    request.settings(Settings.builder().put("number_of_shards",2).put("number_of_replicas", 2).build());
                    client.indices().create(request, RequestOptions.DEFAULT);
              }
              } catch (IOException e) {
              e.printStackTrace();
                }
  }

buildLogIndexSchema类

        private Map<String, Object> buildLogIndexSchema() {
                Map<String, Object> map = Maps.newConcurrentMap();
                Map<String, Object> attr = Maps.newConcurrentMap();

                Map<String, Object> infos = Maps.newConcurrentMap();
                // 时间戳
                infos.put("type", "date");
                infos.put("format", "epoch_second");
                attr.put("timestamp", infos);

                // 设备IP
                infos = Maps.newConcurrentMap();
                infos.put("type", "ip");
                attr.put("sourceIp", infos);

                // 级别
                infos = Maps.newConcurrentMap();
                infos.put("type", "keyword");
                attr.put("level", infos);

                // <Date> <Time> <Pid> <Level> <Component>: <Content>
                attr.put("component", infos);
                attr.put("content", infos);
                attr.put("pid", infos);
                attr.put("templateId", infos);
                map.put("properties", attr);
                return map;
            }

增加数据

        public Integer insertLog() throws IOException {
                RestHighLevelClient client = initESClient();
                Log log=new Log();
                IndexRequest request = new IndexRequest("drain-log_2020-10-24", "_doc"); 
                try {
                    request.source(mapper.writeValueAsString(log), XContentType.JSON);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
                IndexResponse indexResponse = null;
                try {
                    try {
                        indexResponse = client.index(request, RequestOptions.DEFAULT);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (ElasticsearchException e) {
                    if (e.status() == RestStatus.CONFLICT) {
                        System.out.println("冲突了,请在此写冲突处理逻辑!" + e.getDetailedMessage());
                    }
                }
            }

更新数据

        public Integer updateLog(List<LogUpdate> list) throws IOException {

                RestHighLevelClient client = initESClient();

                for(LogUpdate logupdate:list){

                    UpdateRequest updateRequest = new UpdateRequest(logupdate.getIndex(), logupdate.getKey());
                    Map<String, Object> map = new HashMap<>();
                    map.put("level", logupdate.getLevel());
                    updateRequest.doc(map);
                    UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
                }
                return 1;
            }

查询数据
方法一(RestHighLevelClient 方法):

        public Integer searchLog() throws IOException {
                RestHighLevelClient client = initESClient();
                SearchRequest searchRequest=new SearchRequest().indices("drain-log_2020-10-29").types("_doc");
                SearchSourceBuilder searchSourceBuilder =new SearchSourceBuilder();
                searchSourceBuilder.query(QueryBuilders.matchAllQuery());
                searchSourceBuilder.fetchSource(true);
                searchSourceBuilder.size(1000);
                searchRequest.scroll(TimeValue.timeValueMinutes(5L));
                searchRequest.source(searchSourceBuilder);
                SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
                SearchHits hits = searchResponse.getHits();

                ClearScrollRequest clearScrollRequest = null;
                while (true){
                      if (response != null && response.getHits().getHits().length > 0) {
                          for (SearchHit hit : response.getHits().getHits()) {
                                Map<String,Object> source=hit.getSourceAsMap();
                                System.out.println(source);
                          }
                      scrollId = response.getScrollId();
                      if(scrollId == null){
                            break;
                     }
                     SearchScrollRequest searchScrollRequest = new SearchScrollRequest(scrollId).scroll(TimeValue.timeValueMinutes(1));;
                     response = client.searchScroll(searchScrollRequest, RequestOptions.DEFAULT);
                }else{
                    break;
                }            
              }
              //清除游标
              if (response.getScrollId()!=null && !response.getScrollId().equals("")){
                    clearScrollRequest = new ClearScrollRequest();
                    clearScrollRequest.addScrollId(scrollId);
              }
              ClearScrollResponse clearScrollResponse=client.clearScroll(clearScrollRequest,RequestOptions.DEFAULT);
        }

方法二(JDBC方法):

        public Integer searchLog() throws IOException {
                String url = "jdbc:elasticsearch://https://10.10.27.41:9200/";
                Properties properties = new Properties();
                properties.put("useSSL", "true");
                properties.put("trustSelfSigned", "true");
                properties.put("hostnameVerification", "false");
                properties.put("user", "admin");
                properties.put("password", "admin");
                Connection con = DriverManager.getConnection(url, properties);
                Statement st = con.createStatement();
                ResultSet rs = null;
                rs = st.executeQuery("SELECT COUNT(*) FROM drain-log_2020-10-29");
                while (rs.next()) {
                    System.out.println(rs.getId());
                }
                con.close();
              }
posted @ 2020-10-31 12:16  怀念ぅ风锍  阅读(227)  评论(0)    收藏  举报