Java端模拟日志数据

package com.xxxx.appclient;

import java.io.UnsupportedEncodingException;

import java.util.Random;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONArray;

import com.alibaba.fastjson.JSONObject;

import com.xxxx.bean.*;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

/**

* 日志行为数据模拟

*/

public class AppMain {

private final static Loggerlogger = LoggerFactory.getLogger(AppMain.class);

    private static Randomrand =new Random();

    // 设备id

    private static int s_mid =0;

    // 用户id

    private static int s_uid =0;

    // 商品id

    private static int s_goodsid =0;

    public static void main(String[] args) {

// 参数一:控制发送每条的延时时间,默认是0

        Long delay = args.length >0 ? Long.parseLong(args[0]) :0L;

        // 参数二:循环遍历次数

        int loop_len = args.length >1 ? Integer.parseInt(args[1]) :1000;

        // 生成数据

        generateLog(delay, loop_len);

    }

private static void generateLog(Long delay, int loop_len) {

for (int i =0; i < loop_len; i++) {

int flag =rand.nextInt(2);

            switch (flag) {

case (0):

//应用启动

                    AppStart appStart =generateStart();

                    String jsonString = JSON.toJSONString(appStart);

                    //控制台打印

                    logger.info(jsonString);

break;

                case (1):

JSONObject json =new JSONObject();

                    json.put("ap", "app");

                    json.put("cm", generateComFields());

                    JSONArray eventsArray =new JSONArray();

                    // 事件日志

                    // 商品点击,展示

                    if (rand.nextBoolean()) {

eventsArray.add(generateDisplay());

                        json.put("et", eventsArray);

                    }

// 商品详情页

                    if (rand.nextBoolean()) {

eventsArray.add(generateNewsDetail());

                        json.put("et", eventsArray);

                    }

// 商品列表页

                    if (rand.nextBoolean()) {

eventsArray.add(generateNewList());

                        json.put("et", eventsArray);

                    }

// 广告

                    if (rand.nextBoolean()) {

eventsArray.add(generateAd());

                        json.put("et", eventsArray);

                    }

// 消息通知

                    if (rand.nextBoolean()) {

eventsArray.add(generateNotification());

                        json.put("et", eventsArray);

                    }

// 用户后台活跃

                    if (rand.nextBoolean()) {

eventsArray.add(generateBackground());

                        json.put("et", eventsArray);

                    }

//故障日志

                    if (rand.nextBoolean()) {

eventsArray.add(generateError());

                        json.put("et", eventsArray);

                    }

// 用户评论

                    if (rand.nextBoolean()) {

eventsArray.add(generateComment());

                        json.put("et", eventsArray);

                    }

// 用户收藏

                    if (rand.nextBoolean()) {

eventsArray.add(generateFavorites());

                        json.put("et", eventsArray);

                    }

// 用户点赞

                    if (rand.nextBoolean()) {

eventsArray.add(generatePraise());

                        json.put("et", eventsArray);

                    }

//时间

                    long millis = System.currentTimeMillis();

                    //控制台打印

                    logger.info(millis +"|" + json.toJSONString());

break;

            }

// 延迟

            try {

Thread.sleep(delay);

            }catch (InterruptedException e) {

e.printStackTrace();

            }

}

}

/**

    * 公共字段设置

    */

    private static JSONObjectgenerateComFields() {

AppBase appBase =new AppBase();

        //设备id

        appBase.setMid(s_mid +"");

        s_mid++;

        // 用户id

        appBase.setUid(s_uid +"");

        s_uid++;

        // 程序版本号 5,6等

        appBase.setVc("" +rand.nextInt(20));

        //程序版本名v1.1.1

        appBase.setVn("1." +rand.nextInt(4) +"." +rand.nextInt(10));

        // 安卓系统版本

        appBase.setOs("8." +rand.nextInt(3) +"." +rand.nextInt(10));

        // 语言es,en,pt

        int flag =rand.nextInt(3);

        switch (flag) {

case (0):

appBase.setL("es");

break;

            case (1):

appBase.setL("en");

break;

            case (2):

appBase.setL("pt");

break;

        }

// 渠道号  从哪个渠道来的

        appBase.setSr(getRandomChar(1));

        // 区域

        flag =rand.nextInt(2);

        switch (flag) {

case 0:

appBase.setAr("BR");

            case 1:

appBase.setAr("MX");

        }

// 手机品牌 ba ,手机型号 md,就取2位数字了

        flag =rand.nextInt(3);

        switch (flag) {

case 0:

appBase.setBa("Sumsung");

                appBase.setMd("sumsung-" +rand.nextInt(20));

break;

            case 1:

appBase.setBa("Huawei");

                appBase.setMd("Huawei-" +rand.nextInt(20));

break;

            case 2:

appBase.setBa("HTC");

                appBase.setMd("HTC-" +rand.nextInt(20));

break;

        }

// 嵌入sdk的版本

        appBase.setSv("V2." +rand.nextInt(10) +"." +rand.nextInt(10));

        // gmail

        appBase.setG(getRandomCharAndNumr(8) +"@gmail.com");

        // 屏幕宽高hw

        flag =rand.nextInt(4);

        switch (flag) {

case 0:

appBase.setHw("640*960");

break;

            case 1:

appBase.setHw("640*1136");

break;

            case 2:

appBase.setHw("750*1134");

break;

            case 3:

appBase.setHw("1080*1920");

break;

        }

// 客户端产生日志时间

        long millis = System.currentTimeMillis();

        appBase.setT("" + (millis -rand.nextInt(99999999)));

        // 手机网络模式3G,4G,WIFI

        flag =rand.nextInt(3);

        switch (flag) {

case 0:

appBase.setNw("3G");

break;

            case 1:

appBase.setNw("4G");

break;

            case 2:

appBase.setNw("WIFI");

break;

        }

// 拉丁美洲 西经34°46′至西经117°09;北纬32°42′至南纬53°54′

        // 经度

        appBase.setLn((-34 -rand.nextInt(83) -rand.nextInt(60) /10.0) +"");

        // 纬度

        appBase.setLa((32 -rand.nextInt(85) -rand.nextInt(60) /10.0) +"");

        return (JSONObject) JSON.toJSON(appBase);

    }

/**

    * 商品展示事件

    */

    private static JSONObjectgenerateDisplay() {

AppDisplay appDisplay =new AppDisplay();

        boolean boolFlag =rand.nextInt(10) <7;

        // 动作:曝光商品=1,点击商品=2,

        if (boolFlag) {

appDisplay.setAction("1");

        }else {

appDisplay.setAction("2");

        }

// 商品id

        String goodsId =s_goodsid +"";

        s_goodsid++;

        appDisplay.setGoodsid(goodsId);

        // 顺序  设置成6条吧

        int flag =rand.nextInt(6);

        appDisplay.setPlace("" + flag);

        // 曝光类型

        flag =1 +rand.nextInt(2);

        appDisplay.setExtend1("" + flag);

        // 分类

        flag =1 +rand.nextInt(100);

        appDisplay.setCategory("" + flag);

        JSONObject jsonObject = (JSONObject) JSON.toJSON(appDisplay);

        return packEventJson("display", jsonObject);

    }

/**

    * 商品详情页

    */

    private static JSONObjectgenerateNewsDetail() {

AppNewsDetail appNewsDetail =new AppNewsDetail();

        // 页面入口来源

        int flag =1 +rand.nextInt(3);

        appNewsDetail.setEntry(flag +"");

        // 动作

        appNewsDetail.setAction("" + (rand.nextInt(4) +1));

        // 商品id

        appNewsDetail.setGoodsid(s_goodsid +"");

        // 商品来源类型

        flag =1 +rand.nextInt(3);

        appNewsDetail.setShowtype(flag +"");

        // 商品样式

        flag =rand.nextInt(6);

        appNewsDetail.setShowtype("" + flag);

        // 页面停留时长

        flag =rand.nextInt(10) *rand.nextInt(7);

        appNewsDetail.setNews_staytime(flag +"");

        // 加载时长

        flag =rand.nextInt(10) *rand.nextInt(7);

        appNewsDetail.setLoading_time(flag +"");

        // 加载失败码

        flag =rand.nextInt(10);

        switch (flag) {

case 1:

appNewsDetail.setType1("102");

break;

            case 2:

appNewsDetail.setType1("201");

break;

            case 3:

appNewsDetail.setType1("325");

break;

            case 4:

appNewsDetail.setType1("433");

break;

            case 5:

appNewsDetail.setType1("542");

break;

            default:

appNewsDetail.setType1("");

break;

        }

// 分类

        flag =1 +rand.nextInt(100);

        appNewsDetail.setCategory("" + flag);

        JSONObject eventJson = (JSONObject) JSON.toJSON(appNewsDetail);

        return packEventJson("newsdetail", eventJson);

    }

/**

    * 商品列表

    */

    private static JSONObjectgenerateNewList() {

AppLoading appLoading =new AppLoading();

        // 动作

        int flag =rand.nextInt(3) +1;

        appLoading.setAction(flag +"");

        // 加载时长

        flag =rand.nextInt(10) *rand.nextInt(7);

        appLoading.setLoading_time(flag +"");

        // 失败码

        flag =rand.nextInt(10);

        switch (flag) {

case 1:

appLoading.setType1("102");

break;

            case 2:

appLoading.setType1("201");

break;

            case 3:

appLoading.setType1("325");

break;

            case 4:

appLoading.setType1("433");

break;

            case 5:

appLoading.setType1("542");

break;

            default:

appLoading.setType1("");

break;

        }

// 页面  加载类型

        flag =1 +rand.nextInt(2);

        appLoading.setLoading_way("" + flag);

        // 扩展字段1

        appLoading.setExtend1("");

        // 扩展字段2

        appLoading.setExtend2("");

        // 用户加载类型

        flag =1 +rand.nextInt(3);

        appLoading.setType("" + flag);

        JSONObject jsonObject = (JSONObject) JSON.toJSON(appLoading);

        return packEventJson("loading", jsonObject);

    }

/**

    * 广告相关字段

    */

    private static JSONObjectgenerateAd() {

AppAd appAd =new AppAd();

        // 入口

        int flag =rand.nextInt(3) +1;

        appAd.setEntry(flag +"");

        // 动作

        flag =rand.nextInt(5) +1;

        appAd.setAction(flag +"");

        // 内容类型类型

        flag =rand.nextInt(6)+1;

        appAd.setContentType(flag+"");

        // 展示样式

        flag =rand.nextInt(120000)+1000;

        appAd.setDisplayMills(flag+"");

        flag=rand.nextInt(1);

        if(flag==1){

appAd.setContentType(flag+"");

            flag =rand.nextInt(6);

            appAd.setItemId(flag+"");

        }else{

appAd.setContentType(flag+"");

            flag =rand.nextInt(1)+1;

            appAd.setActivityId(flag+"");

        }

JSONObject jsonObject = (JSONObject) JSON.toJSON(appAd);

        return packEventJson("ad", jsonObject);

    }

/**

    * 启动日志

    */

    private static AppStartgenerateStart() {

AppStart appStart =new AppStart();

        //设备id

        appStart.setMid(s_mid +"");

        s_mid++;

        // 用户id

        appStart.setUid(s_uid +"");

        s_uid++;

        // 程序版本号 5,6等

        appStart.setVc("" +rand.nextInt(20));

        //程序版本名v1.1.1

        appStart.setVn("1." +rand.nextInt(4) +"." +rand.nextInt(10));

        // 安卓系统版本

        appStart.setOs("8." +rand.nextInt(3) +"." +rand.nextInt(10));

        //设置日志类型

        appStart.setEn("start");

        //    语言es,en,pt

        int flag =rand.nextInt(3);

        switch (flag) {

case (0):

appStart.setL("es");

break;

            case (1):

appStart.setL("en");

break;

            case (2):

appStart.setL("pt");

break;

        }

// 渠道号  从哪个渠道来的

        appStart.setSr(getRandomChar(1));

        // 区域

        flag =rand.nextInt(2);

        switch (flag) {

case 0:

appStart.setAr("BR");

            case 1:

appStart.setAr("MX");

        }

// 手机品牌 ba ,手机型号 md,就取2位数字了

        flag =rand.nextInt(3);

        switch (flag) {

case 0:

appStart.setBa("Sumsung");

                appStart.setMd("sumsung-" +rand.nextInt(20));

break;

            case 1:

appStart.setBa("Huawei");

                appStart.setMd("Huawei-" +rand.nextInt(20));

break;

            case 2:

appStart.setBa("HTC");

                appStart.setMd("HTC-" +rand.nextInt(20));

break;

        }

// 嵌入sdk的版本

        appStart.setSv("V2." +rand.nextInt(10) +"." +rand.nextInt(10));

        // gmail

        appStart.setG(getRandomCharAndNumr(8) +"@gmail.com");

        // 屏幕宽高hw

        flag =rand.nextInt(4);

        switch (flag) {

case 0:

appStart.setHw("640*960");

break;

            case 1:

appStart.setHw("640*1136");

break;

            case 2:

appStart.setHw("750*1134");

break;

            case 3:

appStart.setHw("1080*1920");

break;

        }

// 客户端产生日志时间

        long millis = System.currentTimeMillis();

        appStart.setT("" + (millis -rand.nextInt(99999999)));

        // 手机网络模式3G,4G,WIFI

        flag =rand.nextInt(3);

        switch (flag) {

case 0:

appStart.setNw("3G");

break;

            case 1:

appStart.setNw("4G");

break;

            case 2:

appStart.setNw("WIFI");

break;

        }

// 拉丁美洲 西经34°46′至西经117°09;北纬32°42′至南纬53°54′

        // 经度

        appStart.setLn((-34 -rand.nextInt(83) -rand.nextInt(60) /10.0) +"");

        // 纬度

        appStart.setLa((32 -rand.nextInt(85) -rand.nextInt(60) /10.0) +"");

        // 入口

        flag =rand.nextInt(5) +1;

        appStart.setEntry(flag +"");

        // 开屏广告类型

        flag =rand.nextInt(2) +1;

        appStart.setOpen_ad_type(flag +"");

        // 状态

        flag =rand.nextInt(10) >8 ?2 :1;

        appStart.setAction(flag +"");

        // 加载时长

        appStart.setLoading_time(rand.nextInt(20) +"");

        // 失败码

        flag =rand.nextInt(10);

        switch (flag) {

case 1:

appStart.setDetail("102");

break;

            case 2:

appStart.setDetail("201");

break;

            case 3:

appStart.setDetail("325");

break;

            case 4:

appStart.setDetail("433");

break;

            case 5:

appStart.setDetail("542");

break;

            default:

appStart.setDetail("");

break;

        }

// 扩展字段

        appStart.setExtend1("");

        return appStart;

    }

/**

    * 消息通知

    */

    private static JSONObjectgenerateNotification() {

AppNotification appNotification =new AppNotification();

        int flag =rand.nextInt(4) +1;

        // 动作

        appNotification.setAction(flag +"");

        // 通知id

        flag =rand.nextInt(4) +1;

        appNotification.setType(flag +"");

        // 客户端弹时间

        appNotification.setAp_time((System.currentTimeMillis() -rand.nextInt(99999999)) +"");

        // 备用字段

        appNotification.setContent("");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(appNotification);

        return packEventJson("notification", jsonObject);

    }

/**

    * 后台活跃

    */

    private static JSONObjectgenerateBackground() {

AppActive_background appActive_background =new AppActive_background();

        // 启动源

        int flag =rand.nextInt(3) +1;

        appActive_background.setActive_source(flag +"");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(appActive_background);

        return packEventJson("active_background", jsonObject);

    }

/**

    * 错误日志数据

    */

    private static JSONObjectgenerateError() {

AppErrorLog appErrorLog =new AppErrorLog();

        String[] errorBriefs = {"at cn.lift.dfdf.web.AbstractBaseController.validInbound(AbstractBaseController.java:72)", "at cn.lift.appIn.control.CommandUtil.getInfo(CommandUtil.java:67)"};        //错误摘要

        String[] errorDetails = {"java.lang.NullPointerException\\n    " +"at cn.lift.appIn.web.AbstractBaseController.validInbound(AbstractBaseController.java:72)\\n " +"at cn.lift.dfdf.web.AbstractBaseController.validInbound", "at cn.lift.dfdfdf.control.CommandUtil.getInfo(CommandUtil.java:67)\\n " +"at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\\n" +" at java.lang.reflect.Method.invoke(Method.java:606)\\n"};        //错误详情

        //错误摘要

        appErrorLog.setErrorBrief(errorBriefs[rand.nextInt(errorBriefs.length)]);

        //错误详情

        appErrorLog.setErrorDetail(errorDetails[rand.nextInt(errorDetails.length)]);

        JSONObject jsonObject = (JSONObject) JSON.toJSON(appErrorLog);

        return packEventJson("error", jsonObject);

    }

/**

    * 为各个事件类型的公共字段(时间、事件类型、Json数据)拼接

    */

    private static JSONObjectpackEventJson(String eventName, JSONObject jsonObject) {

JSONObject eventJson =new JSONObject();

        eventJson.put("ett", (System.currentTimeMillis() -rand.nextInt(99999999)) +"");

        eventJson.put("en", eventName);

        eventJson.put("kv", jsonObject);

        return eventJson;

    }

/**

    * 获取随机字母组合

    *

    * @param length 字符串长度

    */

    private static StringgetRandomChar(Integer length) {

StringBuilder str =new StringBuilder();

        Random random =new Random();

        for (int i =0; i < length; i++) {

// 字符串

            str.append((char) (65 + random.nextInt(26)));// 取得大写字母

        }

return str.toString();

    }

/**

    * 获取随机字母数字组合

    * @param length 字符串长度

    */

    private static StringgetRandomCharAndNumr(Integer length) {

StringBuilder str =new StringBuilder();

        Random random =new Random();

        for (int i =0; i < length; i++) {

boolean b = random.nextBoolean();

            if (b) {// 字符串

                // int choice = random.nextBoolean() ? 65 : 97; 取得65大写字母还是97小写字母

                str.append((char) (65 + random.nextInt(26)));// 取得大写字母

            }else {// 数字

                str.append(String.valueOf(random.nextInt(10)));

            }

}

return str.toString();

    }

/**

    * 收藏

    */

    private static JSONObjectgenerateFavorites() {

AppFavorites favorites =new AppFavorites();

        favorites.setCourse_id(rand.nextInt(10));

        favorites.setUserid(rand.nextInt(10));

        favorites.setAdd_time((System.currentTimeMillis() -rand.nextInt(99999999)) +"");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(favorites);

        return packEventJson("favorites", jsonObject);

    }

/**

    * 点赞

    */

    private static JSONObjectgeneratePraise() {

AppPraise praise =new AppPraise();

        praise.setId(rand.nextInt(10));

        praise.setUserid(rand.nextInt(10));

        praise.setTarget_id(rand.nextInt(10));

        praise.setType(rand.nextInt(4) +1);

        praise.setAdd_time((System.currentTimeMillis() -rand.nextInt(99999999)) +"");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(praise);

        return packEventJson("praise", jsonObject);

    }

/**

    * 评论

    */

    private static JSONObjectgenerateComment() {

AppComment comment =new AppComment();

        comment.setComment_id(rand.nextInt(10));

        comment.setUserid(rand.nextInt(10));

        comment.setP_comment_id(rand.nextInt(5));

        comment.setContent(getCONTENT());

        comment.setAddtime((System.currentTimeMillis() -rand.nextInt(99999999)) +"");

        comment.setOther_id(rand.nextInt(10));

        comment.setPraise_count(rand.nextInt(1000));

        comment.setReply_count(rand.nextInt(200));

        JSONObject jsonObject = (JSONObject) JSON.toJSON(comment);

        return packEventJson("comment", jsonObject);

    }

/**

    * 生成单个汉字

    */

    private static char getRandomChar() {

String str ="";

        int hightPos; //

        int lowPos;

        Random random =new Random();

        //随机生成汉子的两个字节

        hightPos = (176 + Math.abs(random.nextInt(39)));

        lowPos = (161 + Math.abs(random.nextInt(93)));

        byte[] b =new byte[2];

        b[0] = (Integer.valueOf(hightPos)).byteValue();

        b[1] = (Integer.valueOf(lowPos)).byteValue();

        try {

str =new String(b, "GBK");

        }catch (UnsupportedEncodingException e) {

e.printStackTrace();

            System.out.println("错误");

        }

return str.charAt(0);

    }

/**

    * 拼接成多个汉字

    */

    private static StringgetCONTENT() {

StringBuilder str =new StringBuilder();

        for (int i =0; i

str.append(getRandomChar());

        }

return str.toString();

    }

}


下面是bean对象,一个类对应一张表:

你可能感兴趣的:(Java端模拟日志数据)