04、模拟项目需要的日志

产生日志的代码

public class PrintVisitActionLog {

    private static final Logger log = LoggerFactory.getLogger("liveness");

    private static final SimpleDateFormat DATE_FORMAT =
            new SimpleDateFormat("yyyy-MM-dd");

    public static final SimpleDateFormat TIME_FORMAT =
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    public static void main(String[] args) throws InterruptedException {
        String[] searchKeywords = new String[]{"火锅", "蛋糕", "重庆辣子鸡", "重庆小面",
                "呷哺呷哺", "新辣道鱼火锅", "国贸大厦", "太古商场", "日本料理", "温泉"};
        String date = getTodayDate();
        String[] actions = new String[]{"search", "click", "order", "pay"};
        Random random = new Random();

        while(true) {
            // 模拟1万个用户
            long userid = random.nextInt(10000);
            int sessionTimes = random.nextInt(10);
            Long clickCategoryId = null;
            for(int i = 0; i < sessionTimes; i++) {
                String sessionid = UUID.randomUUID().toString().replace("-", "");
                int vistiTimes = random.nextInt(100);
                for(int j = 0; j < vistiTimes; j++) {
                    long pageid = random.nextInt(10);
                    String actionTime = TIME_FORMAT.format(new Date());
                    String searchKeyword = null;
                    Long clickProductId = null;
                    String orderCategoryIds = null;
                    String orderProductIds = null;
                    String payCategoryIds = null;
                    String payProductIds = null;

                    String action = actions[random.nextInt(4)];
                    if("search".equals(action)) {
                        searchKeyword = searchKeywords[random.nextInt(10)];
                    } else if("click".equals(action)) {
                        if(clickCategoryId == null) {
                            clickCategoryId = Long.valueOf(String.valueOf(random.nextInt(100)));
                        }
                        clickProductId = Long.valueOf(String.valueOf(random.nextInt(100)));
                    } else if("order".equals(action)) {
                        orderCategoryIds = String.valueOf(random.nextInt(100));
                        orderProductIds = String.valueOf(random.nextInt(100));
                    } else if("pay".equals(action)) {
                        payCategoryIds = String.valueOf(random.nextInt(100));
                        payProductIds = String.valueOf(random.nextInt(100));
                    }
                    log.info(date + "\t" + userid + "\t" + sessionid + "\t" +
                            pageid + "\t" + actionTime + "\t" + searchKeyword + "\t" +
                            clickCategoryId + "\t" + clickProductId + "\t" +
                            orderCategoryIds + "\t" + orderProductIds + "\t" +
                            payCategoryIds + "\t" + payProductIds + "\t" +
                            Long.valueOf(String.valueOf(random.nextInt(10))));

                    int sleepTime = random.nextInt(60);
                    Thread.sleep(sleepTime * 1000);
                }

            }
        }
    }
    public static String getTodayDate() {
        return DATE_FORMAT.format(new Date());
    }
}

打包并上传服务器

你可能感兴趣的:(Spark相关项目实战)