当前位置: 代码迷 >> JavaScript >> hadoop 地图reduce 中java用gson类库解析json
  详细解决方案

hadoop 地图reduce 中java用gson类库解析json

热度:856   发布时间:2013-11-08 17:52:01.0
hadoop mapreduce 中java用gson类库解析json
废话不说,代码如下:

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类库。
  相关解决方案