Skip to content

Instantly share code, notes, and snippets.

@Vatsal596
Created December 1, 2023 15:09
Show Gist options
  • Save Vatsal596/d47270cd263fb7307d042ede18ce4632 to your computer and use it in GitHub Desktop.
Save Vatsal596/d47270cd263fb7307d042ede18ce4632 to your computer and use it in GitHub Desktop.
package separator;
import java.io.IOException;
import java.util.StringTokenizer;
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.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class hadoopSeparator {
public static class Map extends Mapper<LongWritable, Text, Text, Text>
{
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException
{
String fields[] = value.toString().split(",");
String country = fields[2];
String sales = fields[3];
if(country.length() == 0)
{
context.write(new Text(country), new Text("Missing"));
}
else if (!(Character.isDigit(sales.charAt(0))))
{
context.write(new Text(country), new Text("Invalid"));
}
else
{
context.write(new Text(country), new Text(sales));
}
}
}
public static class Reduce extends Reducer<Text, Text, Text, Text>
{
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException
{
String total="";
for (Text details : values)
{
total = total+" "+details.toString();
}
context.write(key, new Text(total));
}
}
public static void main(String args[]) throws Exception
{
Configuration conf = new Configuration();
Job job=Job.getInstance(conf,"hadoopSeparator");
job.setJarByClass(hadoopSeparator.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
Path outputPath = new Path(args[1]);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
outputPath.getFileSystem(conf).delete(outputPath,true);
System.exit(job.waitForCompletion(true)?0:1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment