大数据之实时项目 第9天 es 查询总数

上篇:大数据之实时项目 第8天 es保存


1、kibana基本操作

(1)过滤查询,按条件查询,如查询日期,执行语句

GET gmall0315_dau/_search
{
  "query":{
    "bool": {
      "filter": {
        "term": {
          "logDate": "2020-03-18"
        }
      }
    }
  }
}

如图所描述
在这里插入图片描述


2、创建一个子模块gmall0315-publisher

这个模块用来发布数据

(1)工程创建步骤
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
子模块gmall0315-publisher,创建ok

(2)pom文件依赖添加:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.study.gmall0315</groupId>
    <artifactId>gmall0315-publisher</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gmall0315-publisher</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.study.gmall0315</groupId>
            <artifactId>gmall0315-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

依赖导入,刷新后
在这里插入图片描述
(3) 代码编写
在这里插入图片描述

PublisherService.java

package com.study.gmall0315publisher.service;

public interface PublisherService {

    public Integer getDauTotal(String date);
}

PublishServiceimpl.java

package com.study.gmall0315publisher.service.impl;

import com.study.gmall0315.common.constant.GmallConstant;
import com.study.gmall0315publisher.service.PublisherService;
import io.searchbox.client.JestClient;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;

@Service
public class PublishServiceimpl implements PublisherService {
  @Autowired
  JestClient jestClient;


    @Override
    public Integer getDauTotal(String date) {
        String query="{\n" +
                "  \"query\":{\n" +
                "    \"bool\": {\n" +
                "      \"filter\": {\n" +
                "        \"term\": {\n" +
                "          \"logDate\": \"2020-03-18\"\n" +
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}";

         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
         BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
         boolQueryBuilder.filter(new TermQueryBuilder("logDate",date));
        searchSourceBuilder.query(boolQueryBuilder);

        System.out.println(searchSourceBuilder.toString());

        Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(GmallConstant.ES_INDEX_DAU).addType("_doc").build();
        Integer total=0;
         try {
             SearchResult searchResult = jestClient.execute(search);
             total= Math.toIntExact(searchResult.getTotal());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return total;
    }
}

PublisherController.java

package com.study.gmall0315publisher.controller;

import com.alibaba.fastjson.JSON;
import com.study.gmall0315publisher.service.PublisherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PublisherController {
    @Autowired
    PublisherService publisherService;

    @GetMapping("realtime-total")
    public String getTotal(@RequestParam("date")String date){
        List<Map>totalList=new ArrayList<>();
       Map dauMap = new HashMap();
       dauMap.put("id","dau");
       dauMap.put("name","新增日活");

       Integer dauTotal = publisherService.getDauTotal(date);
        dauMap.put("value",dauTotal);
        totalList.add(dauMap);

        Map newMidMap = new HashMap();
        newMidMap.put("id","newMid");
        newMidMap.put("name","新增设备");

        newMidMap.put("value",233);
        totalList.add(newMidMap);

        return JSON.toJSONString(totalList);


    }
}

application.properties

spring.elasticsearch.rest.uris=http://flink102:9200


(4) 运行程序,控制台打印错误信息
在这里插入图片描述
在pom文件里,引入以下日志包,就解决问题以上文件

 <!-- 日志包  防止日志冲突 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/log4j-over-slf4j -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

再次启动程序,控制台打印日志
在这里插入图片描述
(5)url访问
访问之前,需要在自己的window环境的etc/hosts文件下做ip映射

127.0.0.1 publisher

访问
http://publisher:8080/realtime-total?date=2020-03-18
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值