为什么我的 Reducer 没有读取文件?
Why isn't my Reducer reading in a file?
我有一种方法可以从 .txt 文件创建哈希表,并使用该哈希表将值分配给传递给 Reducer 的值中的单词。这是我尝试这样做的方式:
@Override
public void setup(Context context) throws IOException {
Path pt = new Path("hdfs:/user/jk/sentiwords.txt");
FileSystem fs = FileSystem.get(new Configuration());
BufferedReader br = new BufferedReader(new InputStreamReader(fs.open(pt)));
String line = br.readLine();
while (line!=null) {
String[] split = line.split("\t");
String word = split[0].substring(0, split[0].length() - 2);
double score = Double.parseDouble(split[1]);
int hashCode = word.hashCode();
sentiTable.put(hashCode, score);
line = br.readLine();
System.out.println("Success");
}
}
然后在这个方法中使用它,它在 key/value 对中的每个值上调用:
public double analyzeString(String str) {
double stringScore = 0.0;
String[] strArr = str.replaceAll("[^a-zA-Z ]", "").toLowerCase().split(" ");
for (String segment: strArr) {
int hashedSeg = segment.hashCode();
if (sentiTable.containsKey(hashedSeg)) {
double value = (double) sentiTable.get(hashedSeg);
stringScore += value;
}
}
return stringScore;
}
理想情况下,这应该 return 介于 -1 和 1 之间的数字。实际上,它总是 returns 0。
编辑:
我应该注意到 sentiTable 是在 class 级别创建的。
结果为 0 可能意味着没有从该文件中读取任何内容。我看到两件事可能出了问题:
路径错误:我认为hdfs路径应该以hdfs://...
开头,而不是hdfs:/...
。
路径和文件系统导入错误。确保导入 Hadoop 提供的那些。
您可以随时在设置方法中打印一条消息,以查看是否已找到该文件。
额外:您可能需要重新考虑您的包含检查,因为在大数据中使用字符串的 hashCode 时预计会发生很多冲突。
我有一种方法可以从 .txt 文件创建哈希表,并使用该哈希表将值分配给传递给 Reducer 的值中的单词。这是我尝试这样做的方式:
@Override
public void setup(Context context) throws IOException {
Path pt = new Path("hdfs:/user/jk/sentiwords.txt");
FileSystem fs = FileSystem.get(new Configuration());
BufferedReader br = new BufferedReader(new InputStreamReader(fs.open(pt)));
String line = br.readLine();
while (line!=null) {
String[] split = line.split("\t");
String word = split[0].substring(0, split[0].length() - 2);
double score = Double.parseDouble(split[1]);
int hashCode = word.hashCode();
sentiTable.put(hashCode, score);
line = br.readLine();
System.out.println("Success");
}
}
然后在这个方法中使用它,它在 key/value 对中的每个值上调用:
public double analyzeString(String str) {
double stringScore = 0.0;
String[] strArr = str.replaceAll("[^a-zA-Z ]", "").toLowerCase().split(" ");
for (String segment: strArr) {
int hashedSeg = segment.hashCode();
if (sentiTable.containsKey(hashedSeg)) {
double value = (double) sentiTable.get(hashedSeg);
stringScore += value;
}
}
return stringScore;
}
理想情况下,这应该 return 介于 -1 和 1 之间的数字。实际上,它总是 returns 0。
编辑:
我应该注意到 sentiTable 是在 class 级别创建的。
结果为 0 可能意味着没有从该文件中读取任何内容。我看到两件事可能出了问题:
路径错误:我认为hdfs路径应该以
hdfs://...
开头,而不是hdfs:/...
。路径和文件系统导入错误。确保导入 Hadoop 提供的那些。
您可以随时在设置方法中打印一条消息,以查看是否已找到该文件。
额外:您可能需要重新考虑您的包含检查,因为在大数据中使用字符串的 hashCode 时预计会发生很多冲突。