reduceJoin

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 values, Context context) throws IOException, InterruptedException {
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 );
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值