搜索场景个数统计

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

 

 

待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东方狱兔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值