上篇:大数据之实时项目 第8天 es保存
(1)过滤查询,按条件查询,如查询日期,执行语句
GET gmall0315_dau/_search
{
"query":{
"bool": {
"filter": {
"term": {
"logDate": "2020-03-18"
}
}
}
}
}
这个模块用来发布数据
(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>
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