gpt4 book ai didi

java - 我如何从 Hadoop 中的 map 程序输出具有列表等数据结构的自定义类

转载 作者:可可西里 更新时间:2023-11-01 16:31:21 27 4
gpt4 key购买 nike

我是 Hadoop 和 Map Reduce 编程的新手。我有一个数据集,其中包含 943 个用户对电影的评分。每个用户最多评价了 20 部电影。现在我希望我的 Mapper 的输出是用户 ID 和一个自定义类,它将有两个列表用于电影(用户评分的电影 ID)和评级(每部电影的评级)。但是我不确定在这种情况下如何从 Map 方法输出这些值。代码片段如下:-

public class UserRatings implements WritableComparable{
private List<String> movieId;
private List<String> movieRatings;
public List<String> getMovieRatings() {
return movieRatings;
}

public void setMovieRatings(List<String> movieRatings) {
this.movieRatings = movieRatings;
}

public List<String> getMovieId() {
return movieId;
}

public void setMovieId(List<String> movieId) {
this.movieId = movieId;
}

@Override
public int compareTo(Object o) {
return 0;
}

@Override
public void write(DataOutput dataOutput) throws IOException {
dataOutput.write
}

@Override
public void readFields(DataInput dataInput) throws IOException {

}

}

这是 map 方法

public class GenreMapper extends Mapper<LongWritable,Text,Text,IntWritable> {

public void map(LongWritable key, Text value,Context context) throws IOException, InterruptedException{
// Logic for parsing the file and exracting the data. Can be ignored...
String[] input = value.toString().split("\t");
Map<String,UserRatings> mapData = new HashMap<String,UserRatings>();
for(int i=0;i<input.length;i++){
List<String> tempList = new ArrayList<String>();
UserRatings userRatings = new UserRatings();
tempList.add(input[3]);
List<String> tempMovieId = new ArrayList<String>();
tempMovieId.add(input[1]);
for(int j=4;j<input.length;j++){
if(input[i].contentEquals(input[j])){
tempMovieId.add(input[j+1]);
tempList.add(input[j+3]);
j = j+4;
}

}
userRatings.setMovieId(tempMovieId);
userRatings.setMovieRatings(tempList);
mapData.put(input[i],userRatings);
}
// context.write();

}

最佳答案

我认为您错过了映射器函数的要点。映射器不应在其输出中发出列表。 mapper 的关键点是生成 reducer 将捕获的元组,并根据键进行必要的计算以产生良好的输出,因此 mapper 的输出格式应尽可能简单。

在这种情况下,我认为正确的方法是在映射器上发出一对键值对:

user_id, custom_class

自定义类必须只有一个 movie_id 和一个评级,而不是一个列表。更具体地说,我需要知道你想要这个 map reduce cicle 的最终结果是什么。请注意,如果需要,您可以在第一次的结果上运行第二次 map reduce。

关于java - 我如何从 Hadoop 中的 map 程序输出具有列表等数据结构的自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30198717/

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