springboot +es +jestResult

<dependency>
			<groupId>org.elasticsearch</groupId>
			<artifactId>elasticsearch</artifactId>
			<version>5.5.3</version>
		</dependency>
<!--对应使用的es版本,下面的包不需要对应-->

		<dependency>
			<groupId>io.searchbox</groupId>
			<artifactId>jest</artifactId>
			<version>6.3.1</version>
		</dependency>
  elasticsearch:
     jest:
        uris:  http://*****************:19210
        connection-timeout: 9000
        read-timeout: 9000
        multi-threaded: true

一.大量数据方式一

@Autowired
	JestClient jestClient;

//实现

List<EsEntity> entityListAll = new ArrayList<>();
        try {
        /* 工厂创建链接 ,可不用,应为引入了jar包
       JestClient jestClient1=jestClient;
        try {

             if(jestClient==null){
                 JestClientFactory factory = new JestClientFactory();
                     factory.setHttpClientConfig(new HttpClientConfig
                             .Builder("http://localhost:9200")
                             .multiThreaded(true)
                             .build());
                      jestClient1=factory.getObject();

             }*/
            //1.构建搜索对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //bool
            BoolQueryBuilder search = new BoolQueryBuilder();
            //条件查询  MatchQueryBuilder 模糊查   TermsQueryBuilder 精准查
            search.must(new MatchQueryBuilder("@type", "RT.CTS.STATION.DI"));
            search.must(new MatchQueryBuilder("DATA_TYPE", "A.0001.0042.R001"));
            search.must(new TermsQueryBuilder("DATA_TIME", hour));
            String[] clo = {"FILE_NAME_O", "FILE_NAME_N", "type", "PROCESS_START_TIME", "IIiii", "PROCESS_STATE",
                    "PROCESS_END_TIME", "DATA_TYPE", "DATA_TIME", "TRAN_TIME","BUSINESS_STATE"};
//fetchSource 筛选字段  ;from(0).size(5000) 相当于MySQL的limit
            searchSourceBuilder.query(search).from(0).size(5000).fetchSource(clo, null);
            Search search1 = new Search.Builder(searchSourceBuilder.toString()).addIndex(tableIndex + day)
                    .build();
            JestResult jestResult = jestClient.execute(search1);
            //查询判断
            if (jestResult.isSucceeded()) {
                //如果查询es 成功 转换成对象
                entityListAll = jestResult.getSourceAsObjectList(EsEntity.class);
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
            return  entityListAll;
        }
        return entityListAll;

方式二:引入 SearchScroll,注意配置中要把集群配置全,要不然查询结果会不全,或者把游标时间设置长一些

  public List<EsEntity> getEsEntityList(String hour,String day){
        List<EsEntity> entityListAll = new ArrayList<>();
        try {
        /*JestClient jestClient1=jestClient;
        try {

             if(jestClient==null){
                 JestClientFactory factory = new JestClientFactory();
                     factory.setHttpClientConfig(new HttpClientConfig
                             .Builder("http://localhost:9200")
                             .multiThreaded(true)
                             .build());
                      jestClient1=factory.getObject();

             }*/
            //1.构建搜索对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //bool
            BoolQueryBuilder search = new BoolQueryBuilder();
            //条件查询
            search.must(new MatchQueryBuilder("@type", "RT.CTS.STATION.DI"));
            search.must(new MatchQueryBuilder("DATA_TYPE", "A.0001.0042.R001"));
            search.must(new MatchQueryBuilder("DATA_TIME", hour));
            String[] clo = {"FILE_NAME_O", "FILE_NAME_N", "type", "PROCESS_START_TIME", "IIiii", "PROCESS_STATE",
                    "PROCESS_END_TIME", "DATA_TYPE", "DATA_TIME", "TRAN_TIME","BUSINESS_STATE"};
            searchSourceBuilder.query(search).fetchSource(clo, null);
            Search search1 = new Search.Builder(searchSourceBuilder.toString()).addIndex(tableIndex + day).setParameter(Parameters.SIZE, 200).setParameter(Parameters.SCROLL, "1m")
                    .build();

            JestResult jestResult = jestClient.execute(search1);
            String scrollId = jestResult.getJsonObject().get("_scroll_id").getAsString();
            //查询判断
            if (jestResult.isSucceeded()) {
                //如果查询es 成功
                for (int i = 0; i < 30; i++) {
                    SearchScroll scroll = new SearchScroll.Builder(scrollId, "1m").build();
                    jestResult = jestClient.execute(scroll);
                    List<EsEntity> entityList = jestResult.getSourceAsObjectList(EsEntity.class);
                    if (!entityList.isEmpty()) {
                        entityListAll.addAll(entityList);
                    } else {
                        break;
                    }
                }
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
            /*try {
                jestClient1.close();
            } catch (IOException e1) {
                System.out.println(e.getMessage());
                return  entityListAll;
            }*/
            return  entityListAll;
        }
        return entityListAll;
    }

方式三:SQL

Search search1=new Search.Builder(
				"{ \n" +
						"    \"query\":{\n" +
						"        \"bool\":{\n" +
						"            \"must\":[\n" +
						"\n" +
						"                {\"term\":{\n" +
						"                \"@type\": \"RT.CTS.STATION.DI\"\n" +
						"                }},\n" +
						"                {\"term\":{        \n" +
						"                \"DATA_TYPE\": \"A.0001.0042.R001\"\n" +
						"                }},\n" +
						"                {\"term\":{ \n" +
						"                \"DATA_TIME\": \"2021-04-24 08:00\"\n" +
						"                }},\n" +
						"                {\"terms\":{ \n" +
						"                \"IIiii\": [\"K1001\",\"K1002\",\"K1003\"]\n" +
						"                }}\n" +
						"            ]\n" +
						"        }\n" +
						"    },\n" +
						"    \"from\": 0,\n" +
						"    \"size\": 10\n" +
						"}"

		).addIndex("atlantic_log_e10adc3949ba59abbe56e057f20f88dd_20210424")
				.build();

		SearchResult jestResult= jestClient.execute(search1);
		jestResult.isSucceeded();
		//jestResult.getHits()
		System.out.println(jestResult.getJsonString());

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个开源的Java框架,用于构建独立的、可扩展的、基于微服务的应用程序。它提供了一种快速、简单的方式来构建和部署应用程序,也简化了与各种数据库、消息队列和其他外部系统进行集成的过程。同时,Spring Boot提供了一套强大的开发工具和功能,方便开发人员进行开发、测试和部署。 Elasticsearch是一个基于Lucene的分布式搜索和分析引擎,它被广泛应用于构建实时搜索、数据分析和数据存储的解决方案。Elasticsearch具有高性能、可扩展、可靠和易于使用的特点,可以处理大规模的数据,并提供全文搜索、聚合分析和实时监控等功能。 Oracle是一种关系型数据库管理系统(RDBMS),它是全球领先的企业级数据库解决方案之一。Oracle提供了高度可靠和安全的数据管理功能,同时支持事务处理、并发性控制、数据恢复和备份等重要特性。它还提供了丰富的管理和开发工具,方便开发人员进行数据库的设计、开发和管理。 在使用Spring Boot开发应用程序时,可以通过集成Elasticsearch和Oracle来满足不同的需求。使用Elasticsearch,可以轻松地实现全文搜索、聚合分析和实时监控等功能。而Oracle数据库可以用于存储结构化数据,并提供事务处理、数据完整性和安全性等特性。通过使用这两个技术,可以构建出高效、可靠和安全的应用程序,满足不同场景下的各种需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值