package de.hsma.bdea;

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.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.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.log4j.BasicConfigurator;

public class WordCountVL {

	public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
		private final static IntWritable one = new IntWritable(1);
		private Text word = new Text();

		public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
			String[] woerter = value.toString().split("\\W+");

			for (String wort: woerter) {
				word.set(wort);
				context.write(word, one); 
			}
		}
	}

	public static class IdentityMapper extends Mapper<Text, IntWritable, Text, IntWritable> {
		public void map(Text key, IntWritable value, Context context) throws IOException, InterruptedException {
			context.write(key, value); 
		}
	}

	public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
		private IntWritable result = new IntWritable();

		public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
			int sum = 0;
			for (IntWritable val : values) {
				sum += val.get();
			}
			result.set(sum);
			context.write(key, result);
		}
	}
	
	public static class DoNothingReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
		public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
			context.write(key, values.iterator().next());
		}
	}

	public static void main(String[] args) throws Exception {
		BasicConfigurator.configure(); 					// Log4j Config oder ConfigFile in Resources Folder
		System.setProperty("hadoop.home.dir", "/");  	// zwingend für Hadoop 3.3.0

		Configuration conf = new Configuration();

		Job job = Job.getInstance(conf, "word count");
		job.setJarByClass(WordCountVL.class);
		job.setMapperClass(TokenizerMapper.class);
		job.setCombinerClass(IntSumReducer.class);	// reduce lokal bei den Mappern
		job.setReducerClass(IntSumReducer.class);	// reduce nach Verteilung bei den Reducern
		job.setNumReduceTasks(4);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);

		FileInputFormat.addInputPath(job, new Path("resources/klassiker"));

		String output1 = "resources/wordcount-output1-" + System.currentTimeMillis();
		FileOutputFormat.setOutputPath(job, new Path(output1));
		job.setOutputFormatClass(SequenceFileOutputFormat.class);

		job.waitForCompletion(true);

		// --- Ende Job 1 ---

		job = Job.getInstance(conf, "word count sort");
		job.setJarByClass(WordCountVL.class);
		job.setMapperClass(IdentityMapper.class);
		job.setReducerClass(DoNothingReducer.class);
		job.setNumReduceTasks(1);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);

		FileInputFormat.addInputPath(job, new Path(output1+"/part-r-*"));
		job.setInputFormatClass(SequenceFileInputFormat.class);
		FileOutputFormat.setOutputPath(job, new Path("resources/wordcount-output2-" + System.currentTimeMillis()));

		job.waitForCompletion(true);

	}
}