Mapreduce 过程中 使用List保存Text元素被修改

最近测试段代码,发现在reduce过程中使用List保存Text的列表会出现元素替换的问题。大家有没有遇到,大神是否能指出其中的原因。


问题描述:

往List中添加Text元素。例如 添加'PA 、PB、ADD:DB。都是字符串。

然后再遍历List 结果则变为[ADD:DB,ADD:DB,ADD:DB]。三个元素都变为最后一次add的数据。

但是将元素按照String类型加入List,则无次情况。


代码如下(请大神指正错误地方):

@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
    List<Text> list = new ArrayList<Text>();
    //key : uid
    //value ADD:DD 或PD
    System.out.println("=====key " + key + " reduce=====");
    list.clear();
    //System.out.println("key:" + key.toString());
    for (Text value : values) {
        String pid = value.toString();
        //System.out.println(value.toString());
        System.out.print("" + pid + "\t");
        list.add(value);
    }
    System.out.println();
    System.out.println(list.toString());
    System.out.println();
}

结果如:



String 添加:

protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
    List<String> list = new ArrayList<String>();
    //key : uid
    //value ADD:DD 或PD
    System.out.println("=====key " + key + " reduce=====");
    list.clear();
    //System.out.println("key:" + key.toString());
    for (Text value : values) {
        String pid = value.toString();
        //System.out.println(value.toString());
        System.out.print("" + pid + "\t");
        list.add(pid);
    }
    System.out.println();
    System.out.println(list.toString());
    System.out.println();
}

结果是正确的:



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值