I encountered an int with a value that could not be converted to value, and I used a try catch to resolve
Str22
Str11
Str33
Str14
Str47
Str25
Str39
Use \ t to separate and get the result
str11,4
STR2 2,5
STR3 3,9
STR4 7
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
I map,reduce here are all separate classes, with a custom key
Package com.kane.mr;
Import Java.io.DataInput;
Import Java.io.DataOutput;
Import java.io.IOException;
Import org.apache.hadoop.io.WritableComparable;
Import com.j_spaces.obf.fi; STR2 2//STR1 1//STR3 3//str1 4//STR4 7//STR2 5//str3 9 Public classes Intpair implements WRITABLECOMPARABLE<INTPA
ir>{public String Getfirstkey () {return firstkey;}
public void Setfirstkey (String firstkey) {this.firstkey = Firstkey;}
public int Getsecondkey () {return secondkey;}
public void Setsecondkey (int secondkey) {this.secondkey = Secondkey;}
Private String firstkey;//str1 Private int secondkey;//1 @Override public void write (DataOutput out) throws IOException {
Out.writeutf (Firstkey);
Out.writeint (Secondkey); @Override public void ReadFields (Datainput in) throws IOException {Firstkey=in.readutf (); Secondkey=in.readin
T (); //Here is the comparison, the other is its own this class, the key is sorted @Override public int compareTo (Intpair o) {/int First=o.getfirstkey (). CompareTo (This.firstkey); if (first!=0) {//Return I//}/else {//return O.getsecondkey ()-this.secondkey;//} return O.getfirstkey ().
CompareTo (This.getfirstkey ());
}} package com.kane.mr;
Import java.io.IOException;
Import org.apache.hadoop.io.IntWritable;
Import Org.apache.hadoop.io.Text;
Import Org.apache.hadoop.mapreduce.Mapper;
public class Sortmapper extends mapper<object,text,intpair,intwritable>{public intpair intpair=new Intpair ();
Public intwritable intwritable=new intwritable (0); @Override protected void Map (Object key, Text value,//str1 1 context) throws IOException, Interruptedexception {/
/string[] Values=value.tostring (). Split ("T");
System.out.println (value);
int intvalue; try {intvalue = Integer.parseint (value.tostring ());} catch (NumberFormatException e) {intvalue=6;}
When value is read without a try catch, it cannot be converted to INT-type Intpair.setfirstkey (key.tostring ());
Intpair.setsecondkey (Intvalue);Intwritable.set (Intvalue);
Context.write (Intpair, intwritable);//Key (STR2 2) 2}} package com.kane.mr;
Import java.io.IOException;
Import Java.util.Iterator;
Import org.apache.hadoop.io.IntWritable;
Import Org.apache.hadoop.io.Text;
Import Org.apache.hadoop.mapreduce.Reducer; public class Sortreducer extends Reducer<intpair, intwritable, text,text>{@Override protected void reduce
(Intpair key, iterable<intwritable> values, context) throws IOException, Interruptedexception {
StringBuffer combinevalue=new StringBuffer ();
Iterator<intwritable> Itr=values.iterator (); while (Itr.hasnext ()) {int Value=itr.next (). get (); Combinevalue.append (value+ ",");} context.write (New Text (
Key.getfirstkey ()), New Text (Combinevalue.tostring ()));
}} package com.kane.mr;
Import org.apache.hadoop.io.IntWritable;
Import Org.apache.hadoop.mapreduce.Partitioner; public class Partiontest extends PArtitioner<intpair, intwritable>{@Override public int getpartition (Intpair key, intwritable value, int numpart
itions) {//reduce number return (Key.getfirstkey (). Hashcode () &integer.max_value%numpartitions);
} package com.kane.mr;
Import org.apache.hadoop.io.WritableComparable;
Import Org.apache.hadoop.io.WritableComparator;
public class Textcomparator extends writablecomparator{public textcomparator () {super (intpair.class,true);}
@Override public int Compare (writablecomparable A, writablecomparable b) {Intpair o1= (Intpair) A;
Intpair o2= (intpair) b;
Return O1.getfirstkey (). CompareTo (O2.getfirstkey ());
}} package com.kane.mr;
Import org.apache.hadoop.io.WritableComparable;
Import Org.apache.hadoop.io.WritableComparator; @SuppressWarnings ("Rawtypes") public class Textintcompartor extends writablecomparator{protected Textintcompart
or () {super (intpair.class,true);} @Override PublIC int Compare (writablecomparable A, writablecomparable b) {Intpair o1= (Intpair) A;
Intpair o2= (intpair) b;
int First=o1.getfirstkey (). CompareTo (O2.getfirstkey ());
if (first!=0) {return a.} else {return O1.getsecondkey ()-o2.getsecondkey ();}}
} package com.kane.mr;
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.lib.input.FileInputFormat;
Import Org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
Import Org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
Import Org.apache.hadoop.util.GenericOptionsParser;
public class Sortmain {public static void main (string[] args) throws exception{Configuration conf = new Configuration ();
string[] Otherargs = new Genericoptionsparser (conf, args). Getremainingargs (); if (otherargs.length!= 2){System.err.println ("Usage:wordcount <in> <out>");
System.exit (2);
Job Job = new Job (conf, "Sort");
Job.setjarbyclass (Sortmain.class); Job.setinputformatclass (keyvaluetextinputformat.class); The format entered is the key (middle \ t separated) value Job.setmapperclass (
Sortmapper.class);
Job.setcombinerclass (Intsumreducer.class);
Job.setreducerclass (Sortreducer.class);
Job.setmapoutputkeyclass (Intpair.class);
Job.setmapoutputvalueclass (Intwritable.class);
Job.setsortcomparatorclass (Textintcompartor.class); Job.setgroupingcomparatorclass (Textcomparator.class),//with key GROUP by Job.setpartitionerclass (Partiontest.class)
;
Job.setoutputkeyclass (Text.class);
Job.setoutputvalueclass (Text.class);
Fileinputformat.addinputpath (Job, New Path (otherargs[0)),//input parameter, corresponding to the first parameter in the back of the Hadoop jar corresponding class runtime
Fileoutputformat.setoutputpath (Job, New Path (otherargs[1));//output parameter System.exit (Job.waitforcompletion (true) 0:1); }
}
Export the jar pack to the Hadoop, then speak Sort.txt into the HDFs, then use the Hadoop jar Kanetest/sort.jar com.kane.mr.soetmain/kane/sort.txt/kane/ Output command execution