【问题标题】:ParquetWriter constructor is not visibleParquetWriter 构造函数不可见
【发布时间】:2019-05-19 06:01:26
【问题描述】:

我正在尝试创建 ParquetWriter 类的对象,它接受参数(OutputFile、Mode、WriteSupport、CompressionCodecName、int、boolean、Configuration、int、ParquetProperties)。 但是这个构造函数在我使用的 API 中有默认的访问修饰符。我无法访问它。

我已经包含了来自 maven 的 parquet 库

compile group: 'org.apache.parquet', name: 'parquet-hadoop', version: '1.10.1'

我什至尝试扩展该类,但仍然出现错误构造函数不可见

public class MyParquetWriter  extends ParquetWriter{

    MyParquetWriter(OutputFile file, Mode mode, WriteSupport writeSupport, CompressionCodecName compressionCodecName,
            int rowGroupSize, boolean validating, Configuration conf, int maxPaddingSize,
            ParquetProperties encodingProps) throws IOException {
        super(file, mode, writeSupport, compressionCodecName, rowGroupSize, validating, conf, maxPaddingSize, encodingProps);

    }
}

如何在我的项目中使用这个构造函数?

【问题讨论】:

标签: java constructor parquet


【解决方案1】:

我查看了 ParquetWriter 类的实现,所有构造函数都被标记为已弃用。
您应该做的是使用在 ParquetWriter 中作为嵌套类提供的 inteded Builder 类对其进行实例化。

这样您还可以确保您的代码与未来的版本兼容。

有关如何使用构建器的更多信息,请参阅这篇文章:
https://dzone.com/articles/design-patterns-the-builder-pattern

编辑: 我在类似情况下一直在做的是编写一个 Wrapper 类,它(在这种情况下)将使用 Builder 来初始化一个私有 ParquetWriter 实例:

public class MyParquetWriterWrapper implements Closeable {
    private final ParquetWriter parquetWriter;

    public MyParquetWriterWrapper(Path file, WriteSupport writeSupport, CompressionCodecName compressionCodecName, int blockSize, int pageSize) throws IOException {
        ParquetWriter.Builder parquetWriterbuilder = new ParquetWriter.Builder() {
            @Override
            protected ParquetWriter.Builder self() {
                return this;
            }

            @Override
            protected WriteSupport getWriteSupport(org.apache.hadoop.conf.Configuration conf) {
                return writeSupport;
            }
        };

        parquetWriterbuilder.withCompressionCodec(compressionCodecName);
        parquetWriterbuilder.withPageSize(pageSize);
        // ... + other properties which you want to be set

        parquetWriter = parquetWriterbuilder.build(); // building the parquetWriter instance
    }

    public ParquetWriter unwrap() {
        return this.parquetWriter;
    }

    @Override
    public void close() throws IOException {
        parquetWriter.close();
    }

Wrapper 不会覆盖 ParquetWriter 的方法,而是简单地转发调用:

public void write(T object) throws IOException {
    // some code before writing...
    this.parquetWriter.write(object);
    // some code after writing...
}

正如this question 中所指出的,扩展一个具体的类(尤其是在不受您控制的情况下)通常不被认为是最佳实践。从接口继承会更好,但是 ParquetWriter 只使用 Closeable,这不会让你走得太远......

【讨论】:

  • 我读到了 builder class 。但我仍然无法访问 ParquetWriter 构造函数,该构造函数有助于 OutputFile 以在 hadoop 中写入 parquet 文件
  • 我希望我能做到,但无法扩展具体类 Closeable
  • 我的错:应该是实现 Closeable 而不是 extends Closable... 我相应地编辑了我的答案
猜你喜欢
  • 1970-01-01
  • 2014-10-21
  • 1970-01-01
  • 2014-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-21
相关资源
最近更新 更多