12.2 开发对外接口
12.2.1 在PublisherService
中添加方法
/**
* 获取指定日期订单的销售额
* @param day
* @return
*/
def getOrderTotalAmount(day: String): Double
/**
* 获取指定日期每个小时的销售额
* @param day
* @return
*/
def getOrderHourTotalAmount(day: String): Map[String, Double]
12.2.2 在PublisherServiceImp
中实现新增的方法
/**
* 获取指定日期订单的销售额
*
* @param day
* @return
*/
override def getOrderTotalAmount(day: String): Double = {
val queryDSL =
s"""
|{
| "query": {
| "bool": {
| "filter": {
| "term": {
| "createDate": "$day"
| }
| }
| }
| }
| , "aggs": {
| "sum_totalAmount": {
| "sum": {
| "field": "totalAmount"
| }
|
| }
| }
|}
""".stripMargin
val search: Search = new Search.Builder(queryDSL)
.addIndex(GmallConstant.ES_INDEX_ORDER)
.addType("_doc").build()
val result: SearchResult = jestClient.execute(search)
result.getAggregations.getSumAggregation("sum_totalAmount").getSum
}
/**
* 获取指定日期每个小时的销售额
*
* @param day
* @return
*/
override def getOrderHourTotalAmount(day: String): Map[String, Double] = {
val searchDSL: String =
s"""
|{
| "query": {
| "bool": {
| "filter": {
| "term": {
| "createDate": "$day"
| }
| }
| }
| }
| , "aggs": {
| "groupby_createHour": {
| "terms": {
| "field": "createHour",
| "size": 24
| }
| , "aggs": {
| "sum_totalAmount": {
| "sum": {
| "field": "totalAmount"
| }
| }
| }
| }
| }
|}
""".stripMargin
val search: Search = new Search.Builder(searchDSL)
.addIndex(GmallConstant.ES_INDEX_ORDER)
.addType("_doc")
.build()
val result: SearchResult = jestClient.execute(search)
// 得到聚合后的结果
val buckets: util.List[TermsAggregation#Entry] =
result.getAggregations.getTermsAggregation("groupby_createHour").getBuckets
val hour2TotalAmount: mutable.Map[String, Double] = mutable.Map[String, Double]()
import scala.collection.JavaConversions._
for (bucket <- buckets) {
hour2TotalAmount += bucket.getKey -> bucket.getSumAggregation("sum_totalAmount").getSum
}
hour2TotalAmount.toMap
}
12.2.3 PublisherControler
注意观察新增的就可以了
package com.atguigu.dw.gmallpublisher.controller
import java.text.SimpleDateFormat
import java.util.Date
import com.atguigu.dw.gmallpublisher.service.PublisherService
import org.apache.commons.lang.time.DateUtils
import org.json4s.jackson.JsonMethods
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.{GetMapping, RequestParam, RestController}
import scala.collection.mutable
/*
日活:
http://hadoop201:8070/realtime-total?date=2019-05-15
[
{"id":"dau","name":"新增日活","value":1200},
{"id":"new_mid","name":"新增用户","value":233}
]
*/
/*
明细:
http://hadoop201:8070/realtime-hour?id=dau&&date=2019-05-15
{
"yesterday":{"钟点":数量, "钟点":数量, ...},
"today":{"钟点":数量, "钟点":数量, ...}
}
*/
@RestController
class PublisherController {
@Autowired
var publisherService: PublisherService = _
@GetMapping(Array("realtime-total"))
def getRealTimeTotal(@RequestParam("date") date: String): String = {
val total: Long = publisherService.getDauTotal(date)
val totalAmount: Double = publisherService.getOrderTotalAmount(date)
val result =
s"""
|[
| {"id":"dau","name":"新增日活","value":$total},
| {"id":"new_mid","name":"新增用户","value":233},
| {"id":"order_amount","name":"新增交易额","value":${totalAmount}}
|]
""".stripMargin
result
}
@GetMapping(Array("realtime-hour"))
def getRealTimeHour(@RequestParam("id") id: String, @RequestParam("date") date: String) = {
if (id == "dau") {
val todayMap: Map[String, Long] = publisherService.getDauHour2countMap(date)
val yesterdayMap: Map[String, Long] = publisherService.getDauHour2countMap(date2Yesterday(date))
val resultMap: mutable.Map[String, Map[String, Long]] = mutable.Map[String, Map[String, Long]]()
resultMap += "today" -> todayMap
resultMap += "yesterday" -> yesterdayMap
println(resultMap)
// 转变成 json 格式字符串输出
import org.json4s.JsonDSL._
JsonMethods.compact(JsonMethods.render(resultMap))
} else if (id == "order_amount") {
val todayMap: Map[String, Double] = publisherService.getOrderHourTotalAmount(date)
val yesterdayMap: Map[String, Double] = publisherService.getOrderHourTotalAmount(date2Yesterday(date))
val resultMap: mutable.Map[String, Map[String, Double]] = mutable.Map[String, Map[String, Double]]()
resultMap += "today" -> todayMap
resultMap += "yesterday" -> yesterdayMap
println(resultMap)
// 转变成 json 格式字符串输出
import org.json4s.JsonDSL._
JsonMethods.compact(JsonMethods.render(resultMap))
} else {
null
}
}
private val dateFormat = new SimpleDateFormat("yyyy-MM-dd")
// 通过今天计算出来昨天
private def date2Yesterday(date: String): String = {
val today: Date = dateFormat.parse(date)
val yesterday: Date = DateUtils.addDays(today, -1)
dateFormat.format(yesterday)
}
}