仓库位置
日志生成:https://github.com/SmallScorpion/gmall-mock.git
日志服务器:https://github.com/SmallScorpion/gmall-spark-ch-es-realtime.git
访问路径
数据格式
新建spring模块pom
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>5.3.3</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>commons-compiler</artifactId>
<version>2.7.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.4.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
controller
package com.warehouse.gmall.publisher.controller;
import com.alibaba.fastjson.JSON;
import com.warehouse.gmall.publisher.service.EsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
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
EsService esService;
@RequestMapping(value = "realtime-total", method = RequestMethod.GET)
public String realtimeTotal(@RequestParam("date") String dt) {
List<Map<String, Object>> rsList = new ArrayList();
// {"id":"dau","name":"新增日活","value":1200}
Map<String, Object> dauMap = new HashMap();
dauMap.put("id", "dau");
dauMap.put("name", "新增日活");
Long dauTotal = esService.getDauTotal(dt);
dauMap.put("value", dauTotal);
// 添加
rsList.add(dauMap);
// {"id":"new_mid","name":"新增设备","value":233}
Map<String, Object> newMap = new HashMap();
newMap.put("id", "new_mid");
newMap.put("name", "新增设备");
newMap.put("value", 222);
// 添加
rsList.add(newMap);
return JSON.toJSONString( rsList );
}
}
serivce
public interface EsService {
/**
* 某一天的总数
* @param date
* @return
*/
public Long getDauTotal( String date );
/**
* 某一天的每个小时数量
* @param date
* @return
*/
public Map getDauHour(String date );
}
serviceimpl
package com.warehouse.gmall.publisher.service.impl;
import com.warehouse.gmall.publisher.service.EsService;
import io.searchbox.client.JestClient;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.lang.model.element.VariableElement;
import java.io.IOException;
import java.util.Map;
@Service
public class EsServiceImpl implements EsService {
// 添加ES
@Autowired
JestClient jestClient;
@Override
public Long getDauTotal(String date) {
// es index
String indexName = "gmall_ch_dau_info" + date + "-query";
// 查询语句
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query( new MatchAllQueryBuilder());
// 构建查询
Search search = new Search.Builder(searchSourceBuilder.toString())
.addIndex(indexName)
.addType("_doc")
.build();
try {
// 执行查询
SearchResult searchResult = jestClient.execute(search);
// 获取总数
return searchResult.getTotal();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("es 查询异常");
}
}
@Override
public Map getDauHour(String date) {
return null;
}
}
application.properties
spring.elasticsearch.jest.uris=http://hadoop102:9200,http://hadoop102:9200
server.port=8070