User007
User007

Reputation: 113

Parse Flat Json file using MAP Reduce JAVA

My task is parse Json object from HDFS and write in separate file in HDFS. Below is my Code.

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}

Note: I have included all the dependency Jar in my project.

Following command executed: hadoop jar JsonMapper.jar com.main.JsonMain /user/cloudera/FlatJson/Flatjson.txt output007

Following are the error message am getting.

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)

"java.lang.ClassNotFoundException: org.json.JSONException" ==> I have imported this jar in my project. let me know what's wrong in this.

Upvotes: 0

Views: 833

Answers (2)

User007
User007

Reputation: 113

"Error: java.lang.ClassNotFoundException: org.json.JSONException" --> This has been solved.

Previously I had the jar in /home/jar/java-json.jar path.

I have moved this jar to "/usr/lib/hadoop-mapreduce/" this path and included this jar and added this jar to the project it worked.

cp java-json.jar /usr/lib/hadoop-mapreduce

Upvotes: 0

KrazyGautam
KrazyGautam

Reputation: 2692

Lets start to debug your problem in steps .

  1. please do a jar -tvf JsonMapper.jar | grep JSONException and you will see that this class doesnt exist in your jar.
  2. Please do understand including a dependency in your project through a dependency management system like mvn doesn't guarantee its availability in jar.
  3. Please use shaded plugin to include all the jars in the dependency into your shaded fat jar.

Upvotes: 1

Related Questions