hdfs中的数据离线校核

一、将目标数据拉取到本地

1、将目标数据筛选后转到Liunx系统文件中

hdfs dfs -text /backup/dataflow/20190822/* | grep "mwapp" > mwapp.log
## 1、先查询指定日期下的所有数据,2、通过管道筛选出含有指定字符的内容,3、将数据重定向到log文件

less mwapp.log
## 查看文件的前一部分

2、将中转的Linux系统文件中的数据拉到本地(win)

gzip mwapp.log
## 由于源数据比较大,将其压缩

sz mwapp.log.gz
## 将压缩后的文件下载到本地

二、通过编写脚本计算指标

1、计算UV

import net.sf.json.JSONArray;
import net.sf.json.JSONException;

import java.io.*;
import java.util.HashSet;
import java.util.Set;


public class LogSize_uv {

    public static void main(String[] args) {

        Set<String> set = new HashSet<String>();
        /* 读取数据 */
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(
                    "D:\\java_job\\logsize001\\logsizefile12.log")), "UTF-8"));

            String lineTxt;
            while ((lineTxt = br.readLine()) != null) {

                JSONArray jsonArray = JSONArray.fromObject(lineTxt);

                //遍历jsonarray中的每一个json
                for (int i = 0; i < jsonArray.size(); i++) {
                    // 得到每一个json
                    String platform = jsonArray.getJSONObject(i).getString("platform");
                    String app_name = jsonArray.getJSONObject(i).getString("app_name");
                    String event_type = jsonArray.getJSONObject(i).getString("event_type");
                    String event;
                    String distinct_id;
                    try {
                        event = jsonArray.getJSONObject(i).getString("event");
                        distinct_id = jsonArray.getJSONObject(i).getString("distinct_id");
                    } catch (JSONException e) {
                        event = "null";
                        distinct_id = "";
                    }

                    platform = platform.toUpperCase();
                    app_name = app_name.toUpperCase();
                    event_type = event_type.toUpperCase();
                    event = event.toUpperCase();
                    if (platform.equals("IOS") && app_name.equals("MWAPP") && event_type.equals("PAGE") && event.equals("HOMECONTROLLER")) {
                        // 将用户id存入集合中
                        set.add(distinct_id);
//                        System.out.println(distinct_id);
                    }
                }
            }
            br.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        int UVCount = set.size();
//        for (String str : (Iterable<String>) set) {
//            System.out.println(str);
//        }

        System.out.println(UVCount);
    }
}

2、计算PV

import net.sf.json.JSONArray;
import net.sf.json.JSONException;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.File;

public class LogSize {
    /**
     * json字符串-数组类型与JSONArray之间的转换
     * @param lineTxt
     */
    public static int testJSONArray(String lineTxt){

        JSONArray jsonArray = JSONArray.fromObject(lineTxt);

        //遍历jsonarray中的每一个json
        int Count = 0;
        for (int i = 0; i < jsonArray.size(); i++) {
            // 得到每一个json
            String platform = jsonArray.getJSONObject(i).getString("platform");
            String app_name = jsonArray.getJSONObject(i).getString("app_name");
            String event_type = jsonArray.getJSONObject(i).getString("event_type");
            String event;
            try{
                event = jsonArray.getJSONObject(i).getString("event"); }
            catch (JSONException e){
//                System.err.println("JSONException :" + e);
                event = "null";
            }

            platform = platform.toUpperCase();
            app_name = app_name.toUpperCase();
            event_type = event_type.toUpperCase();
            event = event.toUpperCase();
            if (platform.equals("IOS") && app_name.equals("MWAPP") && event_type.equals("PAGE") && event.equals("HOMECONTROLLER")) {
                Count += 1;
            }
        }
//        System.out.println(Count);
        return Count;
    }

    public static void main(String[] args) {

        /* 读取数据 */
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(
                    "D:\\java_job\\logsize001\\logsizefile12.log")), "UTF-8"));

            String lineTxt;
            int Sum = 0;
            while ((lineTxt = br.readLine()) != null) {
//                System.out.print(lineTxt);
                int count = testJSONArray(lineTxt);
                Sum += count;
            }
            br.close();
            System.out.println(Sum);
        } catch (Exception e) {
            System.err.println("read errors :" + e);
        }
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值