gpt4 book ai didi

java - 如何在 Hadoop 中序列化 List 集合对象?

转载 作者:行者123 更新时间:2023-12-02 18:30:47 26 4
gpt4 key购买 nike

有没有办法在 Hadoop 中序列化 java 集合?
Writable接口(interface)仅适用于 Java 原语。我有以下类属性。

private String keywords;
private List<Status> tweets;
private long queryTime = 0;

public TweetStatus(String keys, List<Status> tweets, long queryTime){
this.keywords = keys;
this.tweets = tweets;
this.queryTime = queryTime;
}

我如何序列化 List目的?

最佳答案

The Writable interface is for Java primitives only.



对。基本上,您需要将对象分解为可以序列化的对象序列。

因此,从第一个原则开始,要序列化列表,您需要序列化列表的大小,然后序列化列表的每个元素。这样,当您需要反序列化时,您就知道需要反序列化多少元素。

这样的事情应该让你写(双关语!)轨道:
class TweetStatusWritable implements Writable {
private String keywords;
private List<Status> tweets;
private long queryTime;

// add getters for the above three fields

public void readFields(DataInput in) {
this.keywords = in.readUTF();
int size = in.readInt();
this.tweets = new List<Status>();
for(int i = 0; i < size; i++) {
Status status = // deserialize an instance of Status
tweets.add(status);
}
this.queryTime = in.readLong();
}

public void write(DataOutput out) {
out.writeUTF(this.keywords);
out.writeInt(this.tweets.size());
for(int i = 0; i < this.tweets.size(); i++) {
// serialize tweets[i] onto out
}
out.writeLong(queryTime);
}

public TweetStatusWritable(
String keywords,
List<Status> tweets,
long queryTime
) {
this.keywords = keywords;
this.tweets = tweets;
this.queryTime = queryTime;
}
}

关于java - 如何在 Hadoop 中序列化 List 集合对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17220884/

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