package com.alibaba.newcpw.xx.logDeal;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.newcpw.xx.utils.JsonAnalysis;
import com.alibaba.newcpw.xx.utils.OdpsUtil;
import com.aliyun.odps.data.Record;
import org.apache.http.util.TextUtils;
import java.util.HashMap;
import java.util.List;
public class AppSceneCount {
private static String accessId = "xx";
private static String accessKey = "xx";
public static void main(String[] args) {
String sql = "SELECT content FROM xx.xx where ds='20200402' limit 3000;";
List<Record> list = OdpsUtil.getSQLResult(sql, accessId, accessKey);
System.out.println("list为" + list);
// 定义HashMap集合,去重,存取key value
HashMap<String, Integer> appScenes = new HashMap<>();
// 定义set集合,去重,只存key
// Set<String> appScenes = new HashSet<>();
for (int i = 0; i < list.size(); i++) {
// 获取单条SQL的查询字段内容
Record record = list.get(i);
// 正序读取数据库内容,与SQL查询出的内容一致
// System.out.println("读取odps数据库的第"+i+"条数据:"+record);
String appScene = parseJSON(record.getString("content"), i); // i == count
// appScenes.add(appScene);
// object为value,个数
Object object = appScenes.get(appScene);
// System.out.println("object为"+object);
if (object != null) {
int value = Integer.parseInt(object.toString());
appScenes.put(appScene, value + 1);
} else {
appScenes.put(appScene, 1);
}
// 若日志显示为红色,则打印err的日志
// System.err.println("第【"+i+"】条数据判断结束");
}
System.err.println("数据库一次查询的数量为" + list.size());
System.err.println("appScenes去重后的列表为" + appScenes);
System.err.println("搜索场景个数为:" + appScenes.size());
}
/**
* @param jsonText
* @param count
*/
private static String parseJSON(String jsonText, int count) {
String appScene = null;
// // SQL中的content字段解析,jsonText为整个content内容,查找origialParam字段
// int firstIndex = jsonText.indexOf("{");
// // 从{开始截取字符串
// String jsonTextReal = jsonText.substring(firstIndex);
// // 把字符串转换为json
// JSONObject json = JSON.parseObject(jsonTextReal);
JSONObject json = JsonAnalysis.jsonText(jsonText, count);
System.out.println("第【" + count + "】条数据,ytsoku.content.origialParam的json内容为=======" + json);
// appScene
if (json.containsKey("appScene")) {
appScene = json.getString("appScene");
// System.out.println(appScene);
// 把int类型转换为string类型
if (TextUtils.isEmpty(appScene)) {
// System.out.println("第【"+count+"】条数据,appScene客户端传参为空,结果为"+appScene);
}
}
return appScene;
}
}
结果:
数据库一次查询的数量为3000
appScenes去重后的列表为{ott_sug=33, user_search_tudou=1, result_filter=3, show_episode=26, click_sug=79, xx_search_new=179, default_page=356, detail_page=38, show_policy=338, imerge=104, xx_search=3, wait_input=76, tao_film=1, search_rank=213, kubox=1091, da_mai=1, xx_xx=458}
搜索场景个数为:17
业务代码里统计每个场景出现的次数
public static JSONObject jsonObject = new JSONObject();
// 统计场景个数
Object object = jsonObject.getInteger(appScene);
// System.out.println("object为==" + object);
if (object == null) {
jsonObject.put(appScene, 1);
} else {
int sum = (int) object;
jsonObject.put(appScene, sum + 1);
}
System.out.println("第" + i + "条原始日志为:" + record.getString("content")); // i == count
parse2JSON(record.getString("content"), i, currentDay); // i == count
待续