我的任务是从HDFS解析Json对象并在HDFS中写入单独的文件。以下是我的代码。
package com.main;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonMain {
public static class Mapperclass extends Mapper<LongWritable, Text, Text, Text>{
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{
String regId;
String time;
String line = value.toString();
String[] tuple = line.split("\\n");
try{
for(int i=0;i<tuple.length; i++){
JSONObject obj = new JSONObject(tuple[i]);
regId = obj.getString("regId");
time = obj.getString("time");
context.write(new Text(regId), new Text(time));
}
}catch(JSONException e){
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
// TODO Auto-generated method stub
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(JsonMain.class);
job.setMapperClass(Mapperclass.class);
//job.setCombinerClass(IntSumReducer.class);
//job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
Flatjson.txt
{"regId":"TbEtvRH""time":1509073895112}
{"regId":"lWJ2u0j""time":1509073905112}
{"regId":"uB9WG5K""time":1509073915112}
{"regId":"9sO7aqg""time":1509073925113}
{"regId":"hguOaKh""time":1509073935113}
{"regId":"p1CAzYt""time":1509073945113}
{"regId":"quDVMkD""time":1509073955113}
注意:我已将所有依赖项Jar包含在我的项目中。
执行以下命令: hadoop jar JsonMapper.jar com.main.JsonMain /user/cloudera/FlatJson/Flatjson.txt output007
以下是我收到的错误消息。
17/11/01 08:11:12 INFO mapreduce.Job: The url to track the job: http://quickstart.cloudera:8088/proxy/application_1509542757670_0003/
17/11/01 08:11:12 INFO mapreduce.Job: Running job: job_1509542757670_0003
17/11/01 08:13:33 INFO mapreduce.Job: Job job_1509542757670_0003 running in uber mode : false
17/11/01 08:13:33 INFO mapreduce.Job: map 0% reduce 0%
17/11/01 08:15:32 INFO mapreduce.Job: Task Id : attempt_1509542757670_0003_m_000000_0, Status : FAILED
Error: java.lang.ClassNotFoundException: org.json.JSONException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at org.apache.hadoop.conf.Configuration.getClassByNameOrNull(Configuration.java:2138)
&#34; java.lang.ClassNotFoundException:org.json.JSONException&#34; ==&GT;我在我的项目中导入了这个jar。让我知道这有什么不对。
答案 0 :(得分:1)
让我们开始逐步调试您的问题。
答案 1 :(得分:0)
“错误:java.lang.ClassNotFoundException:org.json.JSONException” - &gt;这已经解决了。
之前我在 /home/jar/java-json.jar 路径中使用了jar。
我已将此jar移至“ / usr / lib / hadoop-mapreduce / ”此路径并包含此jar并将此jar添加到其工作的项目中。
cp java-json.jar / usr / lib / hadoop-mapreduce