gpt4 book ai didi

java - Java Hadoop Mapper 如何发送多个值

转载 作者:可可西里 更新时间:2023-11-01 14:12:32 24 4
gpt4 key购买 nike

我的映射器需要发送以下元组:

<custID,prodID,rate>

我想将 custID 作为键发送给 reducer,并将 prodID 和 rate 作为值一起发送,因为它们是 reduce 阶段所需要的。哪种方法最好?

public void map(Object key, Text value, Context context) 
throws IOException, InterruptedException {

String[] col = value.toString().split(",");
custID.set(col[0]);
data.set(col[1] + "," + col[2]);
context.write(custID, data);
}

public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {

for (Text val : values) {
String[] temp = val.toString().split(",");
Text rate = new Text(temp[1]);
result.set(rate);
context.write(key, result);
}
}

最佳答案

最好的办法是写CustomWritables

这是双倍值(value)。您可以将其更改为文本或字符串

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.Writable;


/**
* @author Unmesha SreeVeni U.B
*
*/
public class TwovalueWritable implements Writable {
private double first;
private double second;

public TwovalueWritable() {
set(first, second);
}
public TwovalueWritable(double first, double second) {
set(first, second);
}
public void set(double first, double second) {
this.first = first;
this.second = second;
}
public double getFirst() {
return first;
}
public double getSecond() {
return second;
}
@Override
public void write(DataOutput out) throws IOException {
out.writeDouble(first);
out.writeDouble(second);
}
@Override
public void readFields(DataInput in) throws IOException {
first = in.readDouble();
second = in.readDouble();
}

/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(first);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(second);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof TwovalueWritable)) {
return false;
}
TwovalueWritable other = (TwovalueWritable) obj;
if (Double.doubleToLongBits(first) != Double
.doubleToLongBits(other.first)) {
return false;
}
if (Double.doubleToLongBits(second) != Double
.doubleToLongBits(other.second)) {
return false;
}
return true;
}
@Override
public String toString() {
return first + "," + second;
}
}

从 mapper 中你可以将它作为 emit as

context.write(key,new TwovalueWritable(prodID,rate));

希望这对您有所帮助。

关于java - Java Hadoop Mapper 如何发送多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15734154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com