My problem is Nullpointer, and I spent a whole day but end up finding nothing.
Here is my basic class
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
class User implements Writable {
private String UserID;
private String Gender;
private String age;
private String Occupation;
private String Zip_code;
public static final Map<String, String> GenderMap = new HashMap() {
{
put("F", "Female");
put("M", "Male");
}
};
public static final Map<String, String> AgeMap = new HashMap() {
{
put("1", "Under 18");
put("18", "18-24");
put("25", "25-34");
put("35", "35-44");
put("45", "45-49");
put("50", "50-55");
put("56", "56+");
}
};
public static final Map<String, String> OcupMap = new HashMap() {
{
put(" 0", "othe");
put(" 1", "academic/educato");
put(" 2", "artis");
put(" 3", "clerical/admi");
put(" 4", "college/grad stude");
put(" 5", "customer servic");
put(" 6", "doctor/health car");
put(" 7", "executive/manageria");
put(" 8", "farme");
put(" 9", "homemake");
put(" 10", "K-12 studen");
put(" 11", "lawye");
put(" 12", "programme");
put(" 13", "retire");
put(" 14", "sales/marketin");
put(" 15", "scientis");
put(" 16", "self-employe");
put(" 17", "technician/enginee");
put(" 18", "tradesman/craftsma");
put(" 19", "unemploye");
put(" 20", "write");
}
};
public User() {
}
public User(String userID) {
UserID = userID;
}
public User(String userID, String gender, String age, String occupation, String zip_code) {
UserID = userID;
Gender = gender;
this.age = age;
Occupation = occupation;
Zip_code = zip_code;
}
public String getUserID() {
return UserID;
}
public void setUserID(String userID) {
UserID = userID;
}
public void setRest(User a) {
this.Gender = a.Gender;
this.age = a.age;
this.Occupation = a.Occupation;
this.Zip_code = a.Zip_code;
}
@Override
public void write(DataOutput dataOutput) throws IOException {
dataOutput.writeUTF(UserID);
dataOutput.writeUTF(Gender);
dataOutput.writeUTF(age);
dataOutput.writeUTF(Occupation);
dataOutput.writeUTF(Zip_code);
}
@Override
public void readFields(DataInput dataInput) throws IOException {
UserID = dataInput.readUTF();
Gender = dataInput.readUTF();
age = dataInput.readUTF();
Occupation = dataInput.readUTF();
Zip_code = dataInput.readUTF();
}
@Override
public String toString() {
return "UserID=" + UserID +
", Gender='" + Gender + '\'' +
", age='" + age + '\'' +
", Occupation='" + Occupation + '\'' +
", Zip_code='" + Zip_code + "\',";
}
}
class Movie implements Writable {
private String MovieID;
private String Title;
private String Genres;
public Movie() {
}
public Movie(String movieID) {
MovieID = movieID;
}
public Movie(String movieID, String title, String genres) {
MovieID = movieID;
Title = title;
Genres = genres;
}
public void setRest(Movie a) {
Title = a.Title;
Genres = a.Genres;
}
public String getMovieID() {
return MovieID;
}
public void setMovieID(String movieID) {
MovieID = movieID;
}
@Override
public void write(DataOutput dataOutput) throws IOException {
dataOutput.writeUTF(MovieID);
dataOutput.writeUTF(Title);
dataOutput.writeUTF(Genres);
}
@Override
public void readFields(DataInput dataInput) throws IOException {
MovieID = dataInput.readUTF();
Title = dataInput.readUTF();
Genres = dataInput.readUTF();
}
@Override
public String toString() {
return "MovieID=" + MovieID +
", Title='" + Title + '\'' +
", Genres='" + Genres + '\'';
}
}
class Rate implements WritableComparable<Rate> {
private User user = new User();
private Movie movie = new Movie();
private String rate;
private String timestamp;
public Rate() {
}
public Rate(String userid, String movieid, String rate, String timestamp) {
user.setUserID(userid);
movie.setMovieID(movieid);
this.rate = rate;
this.timestamp = timestamp;
}
@Override
public void write(DataOutput dataOutput) throws IOException {
user.write(dataOutput);
movie.write(dataOutput);
dataOutput.writeUTF(rate);
dataOutput.writeUTF(timestamp);
}
@Override
public void readFields(DataInput dataInput) throws IOException {
user.readFields(dataInput);
movie.readFields(dataInput);
timestamp = dataInput.readUTF();
}
@Override
public String toString() {
return "Rate{" + user + movie +
", rate=" + rate +
", timestamp='" + timestamp + '\'' +
'}';
}
public User getUser() {
return user;
}
public Movie getMovie() {
return movie;
}
@Override
public int compareTo(Rate o) {
return 0;
}
}
Here is my main class
public class Homework2 {
private static Map<String, User> users = new HashMap<>();
private static Map<String, Movie> movies = new HashMap<>();
public static class GenderMapper extends Mapper<LongWritable, Text, Rate, NullWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String s = value.toString();
String[] fields = s.split("::");
if (fields.length == 3) {
movies.put(fields[0], new Movie(fields[0], fields[1], fields[2]));
} else if (fields.length == 4) {
Rate rate = new Rate(fields[0], fields[1], fields[2], fields[3]);
System.out.println(rate);
context.write(rate, null);
} else if (fields.length == 5) {
users.put(fields[0], new User(fields[0],
User.GenderMap.get(fields[1]),
User.AgeMap.get(fields[2]),
User.OcupMap.get(fields[3]),
fields[4])
);
}
}
}
public static class GenderReducer extends Reducer<Rate, NullWritable, Rate, NullWritable> {
@Override
protected void reduce(Rate key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
String userId = key.getUser().getUserID();
String movieId = key.getMovie().getMovieID();
key.getUser().setRest(users.get(userId));
key.getMovie().setRest(movies.get(movieId));
context.write(key, null);
}
}
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
Configuration conf = new Configuration();
GenericOptionsParser optionsParser =
new GenericOptionsParser(conf, args);
String[] remainingArgs = optionsParser.getRemainingArgs();
if (remainingArgs.length < 2) {
System.out.println("Usage: MRJobname <in> [<in>...] , <out>");
System.exit(2);
}
Job job = Job.getInstance(conf);
job.setJobName("Homework2");
job.setJarByClass(Homework2.class);
for (int i = 0; i < remainingArgs.length - 1; i++) {
FileInputFormat.addInputPath(
job, new Path(remainingArgs[i]));
}
job.setInputFormatClass(TextInputFormat.class);
job.setMapperClass(GenderMapper.class);
job.setMapOutputKeyClass(Rate.class);
job.setMapOutputValueClass(NullWritable.class);
job.setNumReduceTasks(1); // 1, 0
job.setReducerClass(GenderReducer.class);
job.setOutputKeyClass(Rate.class);
job.setOutputValueClass(NullWritable.class);
FileOutputFormat.setOutputPath(job,
new Path(remainingArgs[remainingArgs.length - 1]));
job.setOutputFormatClass(TextOutputFormat.class);
job.waitForCompletion(true);
boolean result = job.isSuccessful();
System.exit(result ? 0 : 1);
}
}
my input text is like this
movie.txt
1::Toy Story (1995)::Animation|Children's|Comedy
2::Jumanji (1995)::Adventure|Children's|Fantasy
3::Grumpier Old Men (1995)::Comedy|Romance
4::Waiting to Exhale (1995)::Comedy|Drama
5::Father of the Bride Part II (1995)::Comedy
6::Heat (1995)::Action|Crime|Thriller
7::Sabrina (1995)::Comedy|Romance
8::Tom and Huck (1995)::Adventure|Children's
9::Sudden Death (1995)::Action
10::GoldenEye (1995)::Action|Adventure|Thriller
11::American President, The (1995)::Comedy|Drama|Romance
12::Dracula: Dead and Loving It (1995)::Comedy|Horror
13::Balto (1995)::Animation|Children's
14::Nixon (1995)::Drama
15::Cutthroat Island (1995)::Action|Adventure|Romance
16::Casino (1995)::Drama|Thriller
17::Sense and Sensibility (1995)::Drama|Romance
18::Four Rooms (1995)::Thriller
19::Ace Ventura: When Nature Calls (1995)::Comedy
user.txt
1::F::1::10::48067
2::M::56::16::70072
3::M::25::15::55117
4::M::45::7::02460
5::M::25::20::55455
6::F::50::9::55117
7::M::35::1::06810
8::M::25::12::11413
9::M::25::17::61614
10::F::35::1::95370
11::F::25::1::04093
12::M::25::12::32793
13::M::45::1::93304
14::M::35::0::60126
15::M::25::7::22903
16::F::35::0::20670
17::M::50::1::95350
18::F::18::3::95825
rating.txt
1::1193::5::978300760
1::661::3::978302109
1::914::3::978301968
1::3408::4::978300275
1::2355::5::978824291
1::1197::3::978302268
1::1287::5::978302039
1::2804::5::978300719
1::594::4::978302268
1::919::4::978301368
1::595::5::978824268
1::938::4::978301752
1::2398::4::978302281
1::2918::4::978302124
1::1035::5::978301753
1::2791::4::978302188
and each number stands for each different job. that's why I use HashMap to handle them.
I really appreciate what you help me solve this problem.
Thank you guys again. Wish you have a good future