一、将目标数据拉取到本地
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);
}
}
}