1

我是第一次在 Hadoop 中使用自定义数据类型。这是我的代码:

自定义数据类型:

public class TwitterData implements Writable {

private Long id;
private String text;
private Long createdAt;

public TwitterData(Long id, String text, Long createdAt) {
    super();
    this.id = id;
    this.text = text;
    this.createdAt = createdAt;
}

public TwitterData() {
    this(new Long(0L), new String(), new Long(0L));
}

@Override
public void readFields(DataInput in) throws IOException {
    System.out.println("In readFields...");
    id = in.readLong();
    text = in.readLine();
    createdAt = in.readLong();
}

@Override
public void write(DataOutput out) throws IOException {
    System.out.println("In write...");
    out.writeLong(id);
    out.writeChars(text);
    out.writeLong(createdAt);
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public Long getCreatedAt() {
    return createdAt;
}

public void setCreatedAt(Long createdAt) {
    this.createdAt = createdAt;
}
}

映射器:

public class Map extends Mapper<Object, BSONObject, Text, TwitterData>{

@Override
public void map(Object key, BSONObject value, Context context) throws IOException, InterruptedException {
    BSONObject user = (BSONObject) value.get("user");
    String location = (String) user.get("location");

    TwitterData twitterData = new TwitterData((Long) value.get("id"),
            (String) value.get("text"), (Long) value.get("createdAt"));

    if(location.toLowerCase().indexOf("india") != -1) {
        context.write(new Text("India"), twitterData);
    } else {
        context.write(new Text("Other"), twitterData);
    }
}
}

主要工作代码:

job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(TwitterData.class);

我在映射过程之后抛出了这个异常。我很惊讶为什么它会显示这个错误。谁能帮帮我吗。提前致谢。

4

1 回答 1

2

你写字符,你读行。这是两个不同的序列化过程。

你需要做的是这样做:

@Override
public void readFields(DataInput in) throws IOException {
    id = in.readLong();
    text = in.readUTF();
    createdAt = in.readLong();
}

@Override
public void write(DataOutput out) throws IOException {
    out.writeLong(id);
    out.writeUTF(text);
    out.writeLong(createdAt);
}
于 2013-03-07T08:24:29.243 回答