package edu.if4031;

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.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

import java.io.IOException;

public class AuthorCount {

    /**
     * CLASS DEFINITION
     * ----------------
     * Emits (publication_type, 1) for each end tag found.
     */
    public static class AuthorCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

        private final Text publicationType = new Text();
        private final IntWritable publicationCount = new IntWritable(1);

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            String line = value.toString();
            if (line.contains("<author>")) {
                int idx = line.indexOf("<author>");
                int idxlast = line.indexOf("</author>");
                line = line.substring(idx+("<author>").length(),idxlast);
                publicationType.set(line);
                context.write(publicationType, publicationCount);
            }
        }
    }

    /**
     * CLASS DEFINITION
     * ----------------
     * Count the occurrence of a key.
     */
    public static class AuthorCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

        private final 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 void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
        // invalid arguments
        if (otherArgs.length != 2) {
            System.err.println(USAGE);
            System.exit(2);
        }

        Job job = Job.getInstance(conf, JOB_DESCRIPTION);
        job.setJar("authorcount.jar");
        job.setJarByClass(AuthorCount.class);

        job.setMapperClass(AuthorCountMapper.class);
        job.setCombinerClass(AuthorCountReducer.class);
        job.setReducerClass(AuthorCountReducer.class);

        job.setInputFormatClass(TextInputFormat.class);
        TextInputFormat.addInputPath(job, new Path(otherArgs[0]));

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
    public static final String JOB_DESCRIPTION = "dblp author count";
    public static final String USAGE = "Usage: authorcount <in> <out>";
    public static final String[] PUBLICATION_END_TAGS = new String[]{
            "</author>"};
}

@@ -647,23 +647,18 @@
 ts (
-publication_typ
+author_nam
 e, 1
@@ -672,15 +672,14 @@
 ach 
-end tag
+author
  fou
@@ -816,30 +816,25 @@
 al Text 
-publicationTyp
+authorNam
 e = new 
@@ -875,32 +875,19 @@
 ritable 
-publicationCount
+one
  = new I
@@ -898,24 +898,25 @@
 itable(1);%0A%0A
+%0A
         @Ove
@@ -1228,32 +1228,131 @@
 Of(%22%3C/author%3E%22);
+%0A%0A                // authorname start at the end of %3Cauthor%3E and ends before the start of %3C/author%3E
 %0A               
@@ -1381,31 +1381,14 @@
 (idx
-+(%22%3Cauthor%3E%22).length(),
+ + 8, 
 idxl
@@ -1389,24 +1389,25 @@
 , idxlast);%0A
+%0A
             
@@ -1410,30 +1410,25 @@
         
-publicationTyp
+authorNam
 e.set(li
@@ -1466,41 +1466,23 @@
 ite(
-publicationType, publicationCount
+authorName, one
 );%0A 
@@ -1535,32 +1535,32 @@
 LASS DEFINITION%0A
+
      * ---------
@@ -1571,46 +1571,8 @@
 ---%0A
-     * Count the occurrence of a key.%0A
     
@@ -3088,24 +3088,26 @@
 : 1);%0A    %7D%0A
+%0A%0A
     public s
@@ -3240,102 +3240,6 @@
 %3E%22;%0A
-    public static final String%5B%5D PUBLICATION_END_TAGS = new String%5B%5D%7B%0A            %22%3C/author%3E%22%7D;%0A
 %7D%0A

refactor author count