json格式转换为简单字符串格式Mapreduce清洗实操不创建JavaBean对象
对于我上一个博客通过创建JavaBean对象来将json格式转换成简单字符串格式,我想应该有更简单的办法来改变格式,于是我又写了这个博客,除了Mapper部分有所改变之外,Reducer部分和Driver部分与我的上一个博客相同。
Mapper部分
import net.sf.json.JSONObject;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class WeatherDataMapNew2_1 extends Mapper<LongWritable,Text, Text, NullWritable> {
Text k = new Text();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line =value.toString();
JSONObject obj = JSONObject.fromObject(line);
String[] words = new String[5];
words[2] = obj.getString("city");
words[0] = obj.getString("date");
words[3] = obj.getString("weather");
words[1] = obj.getString("temp");
words[4] = obj.getString("wind");
String stu = new String();
for(int i = 0;i<words.length;i++){
stu+=words[i]+",";
}
stu=stu.substring(0,stu.length()-1);
k.set(stu);
context.write(k,NullWritable.get());
}
}