网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
private MysqlMapper mysqlMapper;
@Override
public JSONObject queryOrderStatsByDate(String dt) {
if (null == dt){
dt = LocalDate.now().toString();
}
List data = mysqlMapper.queryOrderStatsByDate(dt);
System.out.println(data);
List ocs = data.stream().map(d -> d.getOrderCount()).collect(Collectors.toList());
List rcs = data.stream().map(d -> d.getRefundCount()).collect(Collectors.toList());
List dts = data.stream().map(d -> d.getDt()).collect(Collectors.toList());
JSONObject oJO = new JSONObject();
oJO.put(“name”,“新增订单数”);
oJO.put(“data”,ocs);
JSONObject rJO = new JSONObject();
rJO.put(“name”,“退单数”);
rJO.put(“data”,rcs);
List series = new ArrayList<>(2);
series.add(oJO);
series.add(rJO);
JSONObject dataJO = new JSONObject();
dataJO.put(“categories”,dts);
dataJO.put(“series”,series);
JSONObject result = new JSONObject();
result.put(“status”,0);
result.put(“msg”,“”);
result.put(“data”,dataJO);
return result ;
}
}
5.2.4 编写Controller
使用@RestController注解可以标识当前类为控制器,并且可以将处理方法的返回值作为数据写入响应体返回。
@RestController
public class DataInterfaceController
{
@Autowired
private DataInterfaceService dIService;
@RequestMapping(value = “/orderStats”)
public Object queryOrderStats(String date){
return dIService.queryOrderStatsByDate(date);
}
}
5.2.5 测试
5.3 从Redis中返回数据
源数据结构: key=hotphone, valueType=zset。member为手机品牌,score为热度值。
案例二:在Redis中查询实时访问量前5的热点手机品牌。效果图:
5.3.1 封装Bean
此案例需要的数据字段个数较少,可以不封装Bean。
5.3.2 编写Mapper
Redis为NoSQL数据库,可以使用Spring提供的RedisTemplate客户端进行操作,因此也无需封装Mapper。
5.3.3 编写ServiceImpl
根据可视化图表中接口需要返回的数据格式,编写Service的方法实现。
@Service
public class DataInterfaceServiceImpl implements DataInterfaceService
{
@Autowired
private RedisTemplate<String,String> redisTemplate;
@Override
public JSONObject queryVisitStatsTop5() {
Set<ZSetOperations.TypedTuple> typedTuples = redisTemplate.opsForZSet().reverseRangeWithScores(redisDataKey, 0, 4);
List scores = new ArrayList<>();
List brands = new ArrayList<>();
List series = new ArrayList<>(1);
for (ZSetOperations.TypedTuple t : typedTuples) {
scores.add(t.getScore());
brands.add(t.getValue());
}
JSONObject result = getJsonObject(scores, brands, series);
return result;
}
}
5.3.4 编写Controller
使用@RestController注解可以标识当前类为控制器,并且可以将处理方法的返回值作为数据写入响应体返回。
@RestController
public class DataInterfaceController
{
@Autowired
private DataInterfaceService dIService;
@RequestMapping(value = “/phoneVistTop5”)
public Object queryPhoneVistTop5(){
return dIService.queryVisitStatsTop5();
}
}
5.3.5 测试
5.4 从Clickhouse中返回数据
源数据结构:
案例三:从ck中查询各手机当日的销售额。效果图:
案例四:从ck中计算每个手机品牌昨天和今天的总销售额。效果图:
读取Clickhouse可以使用JDBC来读取,这里使用Mybatis简化JDBC操作。
5.4.1 封装Bean
//案例三
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PhoneSales
{
private String brand;
private Double salesAmount;
}
//案例四
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PhoneSalesCompare
{
private String brand;
private Double todaySalesAmount;
private Double yestodaySalesAmount;
}
5.4.2 编写Mapper
@Mapper
@DS(“ck”)
public interface CKMapper
{
//案例三
@Select(“select brand,sum(saleAmount) salesAmount from di where dt = #{dt} group by brand”)
List queryPhoneSalesToday(String dt);
//案例四
@Select("select " +
" ifNull(t1.brand,t2.brand) brand, " +
" ifNull(todayAmount,0.0) todaySalesAmount, " +
" ifNull(yestodayAmount,0.0) yestodaySalesAmount " +
"from " +
"(select brand,sum(saleAmount) todayAmount from di where toDate(dt) = today() group by brand) t1 " +
"full join " +
"(select brand,sum(saleAmount) yestodayAmount from di where toDate(dt) = yesterday() group by brand) t2 " +
"on t1.brand = t2.brand ")
List queryPhoneSales();
}
5.4.3 编写ServiceImpl
根据可视化图表中接口需要返回的数据格式,编写Service的方法实现。
@Service
public class DataInterfaceServiceImpl implements DataInterfaceService
{
@Autowired
private CKMapper ckMapper;
//案例三
@Override
public JSONObject querySalesAmountToday() {
List datas = ckMapper.queryPhoneSalesToday(LocalDate.now().toString());
List scores = new ArrayList<>();
List brands = new ArrayList<>();
List series = new ArrayList<>(1);
for (PhoneSales data : datas) {
scores.add(data.getSalesAmount());
brands.add(data.getBrand());
}
JSONObject result = getJsonObject(scores, brands, series);
return result;
}
//案例四
@Override
public JSONObject querySalesAmount() {
List data = ckMapper.queryPhoneSales();
List today = new ArrayList<>();
List yesterday = new ArrayList<>();
List brands = new ArrayList<>();
List series = new ArrayList<>(2);
for (PhoneSalesCompare d : data) {
today.add(d.getTodaySalesAmount());
yesterday.add(d.getYestodaySalesAmount());
brands.add(d.getBrand());
}
JSONObject todayJO = new JSONObject();
JSONObject yesterdayJO = new JSONObject();
JSONObject result = new JSONObject();
JSONObject dataJO = new JSONObject();
todayJO.put(“name”,“今天”);
todayJO.put(“data”,today);
yesterdayJO.put(“name”,“昨天”);
yesterdayJO.put(“data”,yesterday);
series.add(todayJO);
series.add(yesterdayJO);
dataJO.put(“categories”,brands);
dataJO.put(“series”,series);
result.put(“status”,0);
result.put(“msg”,“”);
result.put(“data”,dataJO);
return result;
}
}
5.4.4 编写Controller
@RestController
public class DataInterfaceController
{
@Autowired
private DataInterfaceService dIService;
//案例三
@RequestMapping(value = “/phoneSalesAmount”)
public Object queryPhoneSalesAmountToday(){
return dIService.querySalesAmountToday();
}
//案例四
@RequestMapping(value = “/phoneSalesAmountCompare”)
public Object queryPhoneSalesAmount(){
return dIService.querySalesAmount();
}
}
5.4.5 测试
案例三:
案例四:
5.5 从ES中返回数据
源数据结构:
{ “mappings”:
{ “_doc”:
{ “properties”:
{
“_class”:
{ “type”: “keyword”, “index”: false, “doc_values”: false },
“id”: { “type”: “long” },
“name”: { “type”: “keyword” },
“profession”: { “type”: “keyword” },
“sex”: { “type”: “keyword” },
“source”: { “type”: “keyword” }
}
}
}
}
案例五:查询ES中手机购买用户的访问渠道统计信息。效果图:
案例六:查询ES中手机购买用户的行业背景及性别比例。效果图:
ES是NoSql数据库,这里使用SpringData中提供的ElasticsearchRestTemplate客户端访问数据库。
5.5.1 封装Bean
封装Bean映射ES目标index中的数据模型。
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = “di”)
public class Customer
{
@Id
private Integer id;
@Field(type = FieldType.Keyword)
private String name;
@Field(type = FieldType.Keyword)
private String sex;
@Field(type = FieldType.Keyword)
private String source;
@Field(type = FieldType.Keyword)
private String profession;
}
5.5.2 编写Dao及Impl
public interface ESDao
{
SearchHits queryFromES(AbstractAggregationBuilder aggregationBuilder,Class t);
}
在ESDao中编写通用的聚合查询方法。并提供实现如下:
@Repository
public class ESDaoImpl implements ESDao
{
@Autowired
private ElasticsearchRestTemplate et;
@Override
public SearchHits queryFromES(AbstractAggregationBuilder aggregationBuilder, Class t) {
Query query = new NativeSearchQueryBuilder()
.addAggregation(aggregationBuilder)
.build();
SearchHits searchHits = et.search(query, t);
return searchHits;
}
}
5.5.3 编写ServiceImpl
根据可视化图表中接口需要返回的数据格式,编写Service的方法实现。
@Service
public class DataInterfaceServiceImpl implements DataInterfaceService
{
@Autowired
private ESDao esDao;
//案例五
@Override
public JSONObject querySourceData() {
TermsAggregationBuilder aggregation = AggregationBuilders.terms(“sc”)
.field(“source”);
SearchHits searchHits = esDao.queryFromES(aggregation, Customer.class);
Aggregations aggregations = searchHits.getAggregations();
Terms tagg= aggregations.get(“sc”);
List<? extends Terms.Bucket> elasticBucket = tagg.getBuckets();
List data = new ArrayList<>();
elasticBucket.forEach(el -> {
data.add(new SourceInfo(el.getKeyAsString(),el.getDocCount()));
});
JSONObject jsonObject = new JSONObject();
jsonObject.put(“status”,0);
jsonObject.put(“msg”,“”);
jsonObject.put(“data”,data);
return jsonObject;
}
//案例六
@Override
public JSONObject queryIndustryStats() {
TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders
.terms(“pc”).field(“profession”).size(10)
.subAggregation(AggregationBuilders.terms(“sc”).field(“sex”).size(2));
SearchHits searchHits = esDao.queryFromES(termsAggregationBuilder, Customer.class);
Aggregations aggregations = searchHits.getAggregations();
List categeryList = new ArrayList<>();
List maleList = new ArrayList<>();
List femaleList = new ArrayList<>();
Terms byCompanyAggregation = aggregations.get(“pc”);
List<? extends Terms.Bucket> elasticBucket = byCompanyAggregation.getBuckets();
elasticBucket.forEach(el -> {
categeryList.add(el.getKeyAsString());
Terms sexAggregation = el.getAggregations().get(“sc”);
List<? extends Terms.Bucket> buckets = sexAggregation.getBuckets();
buckets.forEach(d -> {
if (“男”.equals(d.getKeyAsString())) {
double malePercent = d.getDocCount() * 100 / el.getDocCount() ;
maleList.add((int)malePercent);
femaleList.add(100 - (int)malePercent);
}
});
});
List series = Arrays.asList(
new IndustryInfo(“男”, “%”, maleList),
new IndustryInfo(“女”, “%”, femaleList)
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
ist.add(el.getKeyAsString());
Terms sexAggregation = el.getAggregations().get(“sc”);
List<? extends Terms.Bucket> buckets = sexAggregation.getBuckets();
buckets.forEach(d -> {
if (“男”.equals(d.getKeyAsString())) {
double malePercent = d.getDocCount() * 100 / el.getDocCount() ;
maleList.add((int)malePercent);
femaleList.add(100 - (int)malePercent);
}
});
});
List series = Arrays.asList(
new IndustryInfo(“男”, “%”, maleList),
new IndustryInfo(“女”, “%”, femaleList)
[外链图片转存中…(img-ruN6zVnn-1715463861224)]
[外链图片转存中…(img-K5DdSYLA-1715463861224)]
[外链图片转存中…(img-B1smpyWr-1715463861224)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新