为什么我的减速机不能读取文件?

ruyhziif  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(348)

我有一个方法,可以从一个.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");
    }
}

然后在该方法中使用它,该方法对键/值对中的每个值进行调用:

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;
}

理想情况下,它应该返回一个介于-1和1之间的数字。实际上,它总是返回0。
编辑:
我应该注意到sentitable是在类级别创建的。

56lgkhnf

56lgkhnf1#

结果得到0可能意味着没有从该文件中读取任何内容。我看到两件事可能出了问题:
错误路径:我认为hdfs路径应该从 hdfs://... ,而不是 hdfs:/... .
路径和文件系统的导入错误。确保导入hadoop提供的那些。
您始终可以在setup方法中打印一条消息,以查看是否找到该文件。
额外:您可能需要重新考虑包含检查,因为在大数据中使用字符串的hashcode时会发生许多冲突。

相关问题