Skip to content

Instantly share code, notes, and snippets.

@vajja
Created February 27, 2015 20:56
Show Gist options
  • Select an option

  • Save vajja/1346c84ad4bb473a3a4b to your computer and use it in GitHub Desktop.

Select an option

Save vajja/1346c84ad4bb473a3a4b to your computer and use it in GitHub Desktop.
user.xml file name which I am playing is attached to the mail
import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;
public class Xmlparsing {
public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
String line = value.toString();
try{
XMLStreamReader reader =
XMLInputFactory.newInstance().createXMLStreamReader
(new ByteArrayInputStream(line.getBytes()));
//StringTokenizer tokenizer=new StringTokenizer(line);
while(reader.hasNext()){
int event=reader.next();
switch(event){
case XMLStreamConstants.START_ELEMENT:
if("employee".equals(reader.getLocalName())){
word.set(reader.getAttributeValue(0));
output.collect(word, one);
}
case XMLStreamConstants.CHARACTERS:
word.set(reader.getText().trim());
output.collect(word,one);
}
}
}
catch(Exception e){
}
/* StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
output.collect(word, one);
}*/
}
}
public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}
public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(Xmlparsing.class);
conf.set("xmlinput.start","<employees>");
conf.set("xmlinput.end","</employees>");
conf.setJobName("wordcount");
// conf.setInputFormatClass(XmlInputFormat.class);
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
conf.setMapperClass(Map.class);
conf.setCombinerClass(Reduce.class);
conf.setReducerClass(Reduce.class);
conf.setInputFormat(TextInputFormat.class);
// conf.setInputFormat(XmlInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
JobClient.runJob(conf);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment