map类:
public class MapperJoin extends Mapper<LongWritable, Text, Text, Text> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
FileSplit inputSplit = (FileSplit) context.getInputSplit();
String name = inputSplit.getPath().getName();
String[] split = value.toString().split( “,” );
if (name.equals( “product.txt” )) {
context.write( new Text( split[0] ), value );
}else {
context.write( new Text( split[2] ), value );
}
}
}
reduce类:
public class ReduceJoin extends Reducer<Text, Text, Text, product> {
@Override
protected void reduce(Text key, Iterable
product product = new product();
for (Text value : values) {
if (value.toString().startsWith( “p” )) {
String[] split = value.toString().split( “,” );
product.setName( split[1] );
product.setCid( split[2] );
product.setPrice( Integer.parseInt( split[3] ) );
} else {
String[] split = value.toString().split( “,” );
product.setOid( split[0] );
product.setDate( split[1] );
product.setCount( Integer.parseInt( split[3] ) );
}
}
context.write( key, product);
}
}
javaBean:
public class product implements Writable {
private String name;
private String cid;
private Integer price;
private String oid;
private String date;
private Integer count;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public String getOid() {
return oid;
}
public void setOid(String oid) {
this.oid = oid;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public product() {
}
public product(String name, String cid, Integer price, String oid, String date, Integer count) {
this.name = name;
this.cid = cid;
this.price = price;
this.oid = oid;
this.date = date;
this.count = count;
}
/**
* Serialize the fields of this object to <code>out</code>.
*
* @param out <code>DataOuput</code> to serialize this object into.
* @throws IOException
*/
@Override
public void write(DataOutput out) throws IOException {
out.writeUTF( name );
out.writeUTF( cid );
out.writeInt( price );
out.writeUTF( oid );
out.writeUTF( date );
out.writeInt( count );
}
@Override
public String toString() {
return
name + '\t' +
cid + '\t' +
price +"\t"+
oid + '\t' +
date + '\t' +
count ;
}
/**
* Deserialize the fields of this object from <code>in</code>.
*
* <p>For efficiency, implementations should attempt to re-use storage in the
* existing object where possible.</p>
*
* @param in <code>DataInput</code> to deseriablize this object from.
* @throws IOException
*/
@Override
public void readFields(DataInput in) throws IOException {
this.name = in.readUTF();
this.cid = in.readUTF();
this.price = in.readInt();
this.oid = in.readUTF();
this.date = in.readUTF();
this.count = in.readInt();
}
}
驱动类:
public class JoinJobMain extends Configured implements Tool {
/**
* Execute the command with the given arguments.
*
* @param args command specific arguments.
* @return exit code.
* @throws Exception
*/
@Override
public int run(String[] args) throws Exception {
Job sum = Job.getInstance( new Configuration(), "join" );
sum.setJarByClass( JoinJobMain.class );
sum.setInputFormatClass( TextInputFormat.class );
TextInputFormat.addInputPath( sum, new Path( "C:\\Users\\q\\Desktop\\file" ) );
sum.setMapperClass( MapperJoin.class );
sum.setMapOutputKeyClass( Text.class );
sum.setMapOutputValueClass( Text.class );
sum.setReducerClass( ReduceJoin.class );
sum.setOutputKeyClass( Text.class );
sum.setOutputValueClass( product.class );
sum.setOutputFormatClass( TextOutputFormat.class );
Path path = new Path( "C:\\Users\\q\\Desktop\\file_out" );
TextOutputFormat.setOutputPath( sum,path );
boolean b = sum.waitForCompletion( true );
return b?0:1;
}
public static void main(String[] args) throws Exception {
int run = ToolRunner.run( new Configuration(), new JoinJobMain(), args );
System.exit( run );
}
}