0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows環境でProtocol Buffersを使ってParquetファイルを生成する(Hadoopなし)

Posted at

目的

本記事では、Windows環境のJava開発環境において、Protocol Buffers を使って Parquet ファイルを生成する方法を紹介します。特に、Hadoop を使わずにローカルファイルとして出力する方法にフォーカスしています。

Parquetファイル作成

スキーマ定義

まず最初に、ProtocolBuffesのスキーマを定義しました。
中身は、Person型で中身は適当に定義します。

syntax = "proto3";

package org.example;

option java_multiple_files = true;
option java_package = "org.example";

message Person {
    message Name {
        string value = 1;
        string value2 = 2;
    }
    int32 id = 1;
    Name name = 2;
    double test1 = 3;
    uint32 test2 = 4;
    float test3 = 5;
}

Javaクラスの生成

次に、protoファイルからprotocコマンドを使用して、クラスを生成します。
使用したprotocのVersionは25.6です。これは、使用する```parquet-profobuf``で依存として存在するProtocol BuffersのVersionが3.25.6のため上記のVersionを使用しました。バージョンの互換性については以下を参考にしてもらえればよいのかなと思います。

> protoc --version
libprotoc 25.6

依存ライブラリの追加

プログラムを書く前に、使用する、ライブラリをpom.xmlに定義します。
こちら先ほども記載した通り、protocprotobuf-javaparquet-protobufのVersionを合わせる必要があるので注意してください。

<dependencies>
    <dependency>
      <groupId>com.google.protobuf</groupId>
      <artifactId>protobuf-java</artifactId>
      <version>3.25.6</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.parquet/parquet-protobuf -->
    <dependency>
      <groupId>org.apache.parquet</groupId>
      <artifactId>parquet-protobuf</artifactId>
      <version>1.15.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.parquet/parquet-common -->
    <dependency>
      <groupId>org.apache.parquet</groupId>
      <artifactId>parquet-common</artifactId>
      <version>1.15.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>3.4.1</version>
    </dependency>

Parquetファイルの出力コード

生成されたクラスとライブラリを使ってParquetファイルを出力します。

public static void main( String[] args ) throws IOException {
        Person p = Person.newBuilder()
                .setId(500000000)
                .setName(Person.Name.newBuilder().setValue("1234").setValue2("5678"))
                .setTest1(10.0000000000)
                .setTest2(1000000000)
                .setTest3(10)
                .build();

        Path path = Path.of("出力するパス");
        var outputPath = new LocalOutputFile(path);

        try(ParquetWriter<Person> writer = ProtoParquetWriter.<Person>builder(outputPath)
                .withMessage(Person.class)
                .withWriteMode(ParquetFileWriter.Mode.OVERWRITE)
                .config(ProtoWriteSupport.PB_SPECS_COMPLIANT_WRITE, "true")
                .build()){
            writer.write(p);
        }
    }

outputPathLocalOutputFileを使用していますが、参考にした記事ではHadoopOutputFile.fromPath(path, new Configuration());outputPathの生成を行っていました。しかし、それだとjava.io.FileNotFoundException: java.io.FileNotFoundException: HADOOP_HOME and hadoop.home.dir are unsetが発生してうまくローカルパスにファイル出力ができませんでした。そのため、LocalOutputFileを使用しています。

出力結果

最後Parquet Viewerで確認したところ下記のように出力ができていました。以下のツールを使用してParquetファイルを表示しています。

image.png

まとめ

Windowsのローカルでもしっかり、Procotol Buffersを使って、Parquetファイルを作成することができました。
また、Protocol Buffersに比べるとファイルサイズは大きい模様でした。

参考

Working with Parquet files in Java using Protocol Buffers
https://www.jeronimo.dev/working-with-parquet-files-in-java-using-protocol-buffers/

How to use Parquet Java without Hadoop
https://blakesmith.me/2024/10/05/how-to-use-parquet-java-without-hadoop.html?utm_source=chatgpt.com

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?