废话不说,代码如下:
class="java" name="code">
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Iterator;
/*import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;*/
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class ParseLogMapper extends Mapper<LongWritable, Text, Text, Text> {
@Override
public void map(LongWritable key, Text value,
Context context)
throws IOException, InterruptedException {
try{
String json;
String line = value.toString();
String[] data = line.split("\\t");
LogColumns logColumns = new LogColumns();
logColumns.reqTime = data[0];
logColumns.reqIp = data[1];
json = data[2];
Type type = new TypeToken<LogJson>() {
}.getType();
Gson gson = new Gson();
LogJson logJson = gson.fromJson(json, type);
logColumns.devMac = logJson.getMac();
logColumns.devType = logJson.getDevice();
logColumns.devVersion = logJson.getVersion();
logColumns.report = "download";
//System.out.println(b.getRows());
//System.out.println(logJson.getAppList().size());
Iterator<AppInfo> i=logJson.getAppList().iterator();
while(i.hasNext()){
AppInfo appInfo = i.next();
logColumns.appName = appInfo.getkTitle();
logColumns.appid = appInfo.getkAppID();
logColumns.actionTime = appInfo.getkDownloadDate();
context.write(new Text(logColumns.toString()), new Text());
}
}catch(Exception e){
}
}
}
附件是gson类库。
- google-gson-2.2.4-release.zip (559 KB)
- 下载次数: 0