在我的上一篇博客中,我是将一个天气数据处理为了一个json格式的数据
具体的博客内容如下
https://blog.youkuaiyun.com/qq_45683188/article/details/108415084
那我们想一想,能不能就是我们再把json格式的数据转化为普通类型的数据,听起来还是比较让人感到这样是不是有病啊,为什么不用原来的数据,偏要转过来又转过去,对于开发而言,这样做确实有病,但是对于我们这些正在学习的海洋里学习的小白来说,更能掌握一个新的知识,好了废话少说,上正菜了
对于这个将json格式转化为普通的字符串的数据来说,用的依赖肯定不是json_lib的依赖了
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>’
以上是需要新加的依赖
关于这个json的一个大致用法,大家可以参照
https://www.cnblogs.com/sunzn/archive/2013/02/12/2910241.html
这个网址
我就是将json格式的内容转化为javabean格式的代码,然后重写javabean中的toString‘方法
现在上代码
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 ReverseMap 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 json=JSONObject.fromObject (line);
//将字符串转化为JavaBean
Weather weather=(Weather) JSONObject.toBean (json,Weather.class);
k.set (weather.toString ());
context.write (k,NullWritable.get ());
}
}
首先就是我的mapper阶段的代码
然后就是我的javabean这个类的代码
public class Weather {
private String date;
private String temp;
private String city;
private String wind;
public Weather(){
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getWind() {
return wind;
}
public void setWind(String wind) {
this.wind = wind;
}
@Override
public String toString() {
return this.date+","+this.temp+","+this.city+","+this.wind+",";
}
}
其中就是重写的toString的方法
剩下的就是我的reduce阶段的代码,和driver阶段的代码了
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class ReversReduce extends Reducer<Text, NullWritable,Text,NullWritable> {
@Override
protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
context.write (key,NullWritable.get ());
}
}
以上是reduce阶段的代码
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
public class WeatherDriver {
public static void main(String[] args) {
args=new String[2];
args[0]="D:\\LIB\\weather.txt";
args[1]="D:\\LIB\\output";
Configuration configuration=new Configuration ();
try {
Job job=Job.getInstance (configuration);
job.setJarByClass (WeatherDriver.class);
job.setMapperClass (ReverseMap.class);
job.setReducerClass (ReversReduce.class);
job.setMapOutputKeyClass (Text.class);
job.setMapOutputValueClass (NullWritable.class);
job.setOutputKeyClass (Text.class);
job.setOutputValueClass (NullWritable.class);
Path path = new Path(args[1]);
FileSystem fileSystem = path.getFileSystem(configuration);
if (fileSystem.exists(path)) {
fileSystem.delete(path, true);
}
FileInputFormat.setInputPaths (job,new Path (args[0]));
FileOutputFormat.setOutputPath (job,new Path (args[1]));
boolean a=job.waitForCompletion (true);
System.out.println (a );
} catch (IOException e) {
e.printStackTrace ( );
} catch (InterruptedException e) {
e.printStackTrace ( );
} catch (ClassNotFoundException e) {
e.printStackTrace ( );
}
}
}
以上为driver阶段的代码
最后就是说说你写这个程序,需要注意什么,这里其实也没有太多可以说的,对于我个人而言,我最想讲的就是,一定要注意你的导包问题,导错包找错方法真的很难受啊