目的
訪問路徑
數據格式
創建一個Spring Initializr項目
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</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>
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>5.3.3</version>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
增加EsService
在java\com\atguigu\spark\gmall\dw\publisher\service\EsService.java
import java.util.Map;
public interface EsService {
//日活的總數查詢
public Long getDauTotal(String date);
//日活的分時查詢
public Map getDauHour(String date);
}
增加EsServiceImpl
在java\com\atguigu\spark\gmall\dw\publisher\service\impl\EsServiceImpl.java
import com.atguigu.spark.gmall.dw.publisher.service.EsService;
import io.searchbox.client.JestClient;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.search.aggregation.TermsAggregation;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class EsServiceImpl implements EsService {
@Autowired
JestClient jestClient;
@Override
public Long getDauTotal(String date) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(new MatchAllQueryBuilder());
String query = searchSourceBuilder.toString();
date=date.replace("-","");
String indexName="gmall1122_dau_info_"+date+"-query";
Search search = new Search.Builder(query).addIndex(indexName).addType("_doc").build();
Long total=0L;
try {
SearchResult searchResult = jestClient.execute(search);
total = searchResult.getTotal();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("查詢ES異常");
}
return total;
}
@Override
public Map getDauHour(String date) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("groupby_hr").field("hr").size(24);
searchSourceBuilder.aggregation(aggregationBuilder);
String query = searchSourceBuilder.toString();
date=date.replace("-","");
String indexName="gmall1122_dau_info_"+date+"-query";
Search search = new Search.Builder(query).addIndex(indexName).addType("_doc").build();
Map aggsMap=new HashMap();
try {
SearchResult searchResult = jestClient.execute(search);
List<TermsAggregation.Entry> buckets = searchResult.getAggregations().getTermsAggregation("groupby_hr").getBuckets();
for (TermsAggregation.Entry bucket : buckets) {
aggsMap.put( bucket.getKey(),bucket.getCount());
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("查詢ES異常");
}
return aggsMap;
}
}
增加PublisherController
在java\com\atguigu\spark\gmall\dw\publisher\controller\PublisherController.java
import com.alibaba.fastjson.JSON;
import com.atguigu.spark.gmall.dw.publisher.service.EsService;
import org.apache.commons.lang3.time.DateUtils;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@RestController
public class PublisherController {
@Autowired
EsService esService;
//@RequestMapping(value = "realtime-total" ,method = RequestMethod.GET)
@GetMapping("realtime-total")
public String realtimeTotal(@RequestParam("date") String dt){
List<Map> totalList=new ArrayList<>();
HashMap dauMap = new HashMap<>();
dauMap.put("id","dau");
dauMap.put("name","新增日活");
Long dauTotal = esService.getDauTotal(dt);
dauMap.put("value",dauTotal);
totalList.add(dauMap);
HashMap newMidMap = new HashMap<>();
newMidMap.put("id","dau");
newMidMap.put("name","新增設備");
newMidMap.put("value",233);
totalList.add(newMidMap);
return JSON.toJSONString(totalList);
}
@GetMapping("realtime-hour")
public String realtimeHour(@RequestParam("id") String id , @RequestParam("date") String dt ){
if("dau".equals(id)){
Map<String, Map> hourMap=new HashMap<>();
Map dauHourTdMap = esService.getDauHour(dt);
hourMap.put("today",dauHourTdMap);
String yd = getYd(dt);
Map dauHourYdMap = esService.getDauHour(yd);
hourMap.put("yesterday",dauHourYdMap);
return JSON.toJSONString(hourMap);
}
return null;
}
private String getYd(String td){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String yd=null;
try {
Date tdDate = dateFormat.parse(td);
Date ydDate = DateUtils.addDays(tdDate, -1);
yd=dateFormat.format(ydDate);
} catch (ParseException e) {
e.printStackTrace();
throw new RuntimeException("日期格式轉變失敗");
}
return yd;
}
}
主機映射
修改windwos/System32/drivers/etc/hosts