Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. package com.bigdata.exercise1_2;
  2.  
  3.  
  4. import java.io.IOException;
  5. import java.util.Arrays;
  6. import java.util.StringTokenizer;
  7. import org.apache.hadoop.fs.FileSystem;
  8. import org.apache.hadoop.fs.Path;
  9. import org.apache.hadoop.io.IntWritable;
  10. import org.apache.hadoop.io.Text;
  11. import org.apache.hadoop.mapred.JobConf;
  12. import org.apache.hadoop.mapreduce.Job;
  13. import org.apache.hadoop.mapreduce.Mapper;
  14. import org.apache.hadoop.mapreduce.Reducer;
  15. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  16. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  17. import org.apache.hadoop.conf.Configuration;
  18.  
  19.  
  20. public class App {
  21.  
  22. public static class PreverjanjeSodostiLihosti extends Mapper<Object, Text, Text, IntWritable>
  23. {
  24. private final static IntWritable one = new IntWritable(1);
  25. private final static IntWritable zero = new IntWritable(0);
  26.  
  27. private Text text = new Text();
  28. private String word1 = new String();
  29. private String word2 = new String();
  30. private String word3 = new String();
  31.  
  32. public void map(Object key, Text value, Context context) throws IOException, InterruptedException
  33. {
  34. Configuration conf = context.getConfiguration();
  35. conf.setAllowNullValueProperties(true);
  36. word1 = conf.get("word1");
  37. word2 = conf.get("word2");
  38. word3 = conf.get("word3");
  39.  
  40. StringTokenizer itr = new StringTokenizer(value.toString());
  41. while (itr.hasMoreTokens())
  42. {
  43. text.set(itr.nextToken());
  44. if(text.toString().equals(word1)||text.toString().equals(word2)||text.toString().equals(word3))
  45. {
  46. context.write(text, one);
  47. }
  48. else if (!text.toString().equals(word1)||!text.toString().equals(word2)||!text.toString().equals(word3))
  49. {
  50. if(conf.onlyKeyExists(word1))
  51. {
  52. Text text1 = new Text(word1);
  53. context.write(text1, zero);
  54. }
  55. if(conf.onlyKeyExists(word2))
  56. {
  57. Text text2 = new Text(word2);
  58. context.write(text2, zero);
  59. }
  60. if(!word3.equals(null))
  61. {
  62. Text text3 = new Text(word3);
  63. context.write(text3, zero);
  64. }
  65. }
  66. }
  67. }
  68. }
  69.  
  70.  
  71. public static class SestejSodeInLihe extends Reducer<Text,IntWritable,Text,IntWritable>
  72. {
  73. private IntWritable result = new IntWritable();
  74.  
  75. public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException
  76. {
  77. int sum = 0;
  78. for (IntWritable val : values)
  79. {
  80. sum += val.get();
  81. }
  82. result.set(sum);
  83. context.write(key, result);
  84. }
  85. }
  86.  
  87. public static void main(String... args) throws Exception {
  88.  
  89. Configuration conf = new Configuration();
  90. conf.set("mapred.job.queue.name", "default");
  91. conf.set("word1",args[2]);
  92. for(int i = 3;i<args.length;i++)
  93. {
  94. conf.set("word"+(i-1), args[i]);
  95. }
  96.  
  97. // configuration should contain reference to your namenode
  98. FileSystem fs = FileSystem.get(new Configuration());
  99. // true stands for recursively deleting the folder you gave
  100. fs.delete(new Path(args[1]), true);
  101.  
  102.  
  103. Job job = Job.getInstance(conf, "Stetje Sodih in lihih stevil");
  104.  
  105. job.setJarByClass(App.class);
  106. job.setMapperClass(PreverjanjeSodostiLihosti.class);
  107. job.setReducerClass(SestejSodeInLihe.class);
  108. job.setOutputKeyClass(Text.class);
  109. job.setOutputValueClass(IntWritable.class);
  110.  
  111. FileInputFormat.addInputPath(job, new Path(args[0]));
  112. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  113.  
  114. System.exit(job.waitForCompletion(true) ? 0 : 1);
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement