【问题标题】:HDFS path in user defined function of Pig LatinPig Latin 用户定义函数中的 HDFS 路径
【发布时间】:2017-08-09 01:31:18
【问题描述】:

我有以下用 Java 语言编写的用户定义函数:

我定义了 FileWriter 但执行后出现错误消息。

程序:

outputFile = new FileWriter("hdfs://NaeNode:9000/input/SG.csv",true); fw = new BufferedWriter(outputFile);

从 UDF 捕获错误:trial.obvious_guess [hdfs:/NaemNode:9000/input/SG.csv(没有这样的文件或目录)]

我该如何解决这个问题,因为我在执行时使用 [pig -x MapReduce fie.pig]

【问题讨论】:

  • 如果我们这样修改路径:hdfs:\\NameNode:9000\\input\\SG.csv,那么错误就会消失,但是Java不能在SG中写入任何东西。 csv 文件。
  • 请避免使用大写字母。这意味着尖叫,这对于论坛来说是非常糟糕的风格。

标签: java hadoop apache-pig user-defined-functions


【解决方案1】:

要存储在 HDFS 中,您必须使用 FSDataOutputStream。

FileSystem.create 返回 FSDataOutputStream。

请看下面的代码。

public static void main(String[] args) throws Exception {
    FSDataOutputStream fout = null;
    try {
        Path path = new Path("hdfs://NaeNode:9000/input/SG.csv");
        String data = "data";
        FileSystem fileSystem = FileSystem.get(new Configuration());
        if (!fileSystem.exists(path)) {
            fout = fs.create(path); 
        } else {
            fout = fs.append(path)
        }
        BufferedWriter bufferedWriter = new BufferedWriter(
            new OutputStreamWriter(fout)
        );
        bufferedWriter.write(data);
        bufferedWriter.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

【讨论】:

  • 我试过上面的代码,数据不一致,也就是说,每组数据读取(包),文件内容总是变化。所以运行udf后,文件中只存储了几行,其余的文件都被覆盖了。
  • 如果文件存在,我将其修改为追加。查看修改后的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多