Home:ALL Converter>Read local Parquet file without Hadoop Path API

Read local Parquet file without Hadoop Path API

Ask Time:2020-01-28T05:40:53         Author:Ben Watson

Json Formatter

I'm trying to read a local Parquet file, however the only APIs I can find are tightly coupled with Hadoop, and require a Hadoop Path as input (even for pointing to a local file).

ParquetReader<GenericRecord> reader = AvroParquetReader.<GenericRecord>builder(file).build();
GenericRecord nextRecord = reader.read();

is the most popular answer in how to read a parquet file, in a standalone java code?, but requires a Hadoop Path and has now been deprecated for a mysterious InputFile instead. The only implementation of InputFile I can find is HadoopInputFile, so again no help.

In Avro this is a simple:

DatumReader<GenericRecord> datumReader = new GenericDatumReader<>();
this.dataFileReader = new DataFileReader<>(file, datumReader);

(where file is java.io.File). What's the Parquet equivalent?

I am asking for no Hadoop Path dependency in the answers, because Hadoop drags in bloat and jar hell, and it seems silly to require it for reading local files.

To further explain the backstory, I maintain a small IntelliJ plugin that allows users to drag-and-drop Avro files into a pane for viewing in a table. This plugin is currently 5MB. If I include Parquet and Hadoop dependencies, it bloats to over 50MB, and doesn't even work.


POST-ANSWER ADDENDUM

Now that I have it working (thanks to the accepted answer), here is my working solution that avoids all the annoying errors that can be dragged in by depending heavily on the Hadoop Path API:

Author:Ben Watson,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/59939309/read-local-parquet-file-without-hadoop-path-api
Jörn Horstmann :

Unfortunately the java parquet implementation is not independent of some hadoop libraries. There is an existing issue in their bugtracker to make it easy to read and write parquet files in java without depending on hadoop but there does not seem to be much progress on it. The InputFile interface was added to add a bit of decoupling, but a lot of the classes that implement the metadata part of parquet and also all compression codecs live inside the hadoop dependency.\n\nI found another implementation of InputFile in the smile library, this might be more efficient than going through the hadoop filesystem abstraction, but does not solve the dependency problem.\n\nAs other answers already mention, you can create an hadoop Path for a local file and use that without problems.\n\njava.io.File file = ...\nnew org.apache.hadoop.fs.Path(file.toURI())\n\n\nThe dependency tree that is pulled in by hadoop can be reduced a lot by defining some exclusions. I'm using the following to reduce the bloat (using gradle syntax):\n\ncompile(\"org.apache.hadoop:hadoop-common:3.1.0\") {\n exclude(group: 'org.slf4j')\n exclude(group: 'org.mortbay.jetty')\n exclude(group: 'javax.servlet.jsp')\n exclude(group: 'com.sun.jersey')\n exclude(group: 'log4j')\n exclude(group: 'org.apache.curator')\n exclude(group: 'org.apache.zookeeper')\n exclude(group: 'org.apache.kerby')\n exclude(group: 'com.google.protobuf')\n}\n",
2020-02-04T23:29:48
mazaneicha :

parquet-tools utility seems like a good place to start. It does have some Hadoop dependencies, but works as well with local files as with HDFS (depending on defaultFS in Configuration). If you have licensing restrictions (tools are Apache V2, as everything else), you can probably just review the source for one of the content-printing commands (cat, head, or dump) for inspiration.\n\nThe closest thing to your Avro example would be using ParquetFileReader, I guess.\n\n Configuration conf = new Configuration();\n Path path = new Path(\"/parquet/file/path\");\n ParquetMetadata footer = ParquetFileReader.readFooter(conf, path, ParquetMetadataConverter.NO_FILTER);\n ParquetFileReader reader = new ParquetFileReader(conf, path, footer);\n",
2020-01-27T22:29:43
Long Vu :

If the need for not using Hadoop is really unavoidable, you can try Spark and run it in a local version. A quick start guide can be find here: https://spark.apache.org/docs/latest/index.html. For downloading, you can download at this link: https://archive.apache.org/dist/spark/ (find a version you like, there is always a build without hadoop. Unfortunately, the size of compressed version is still around 10-15M). You will also able to find some Java example at examples/src/main.\n\nAfter that, you can read the file in as a Spark Dataframe like this\n\nimport org.apache.spark.api.java.*;\nimport org.apache.spark.api.java.function.*; \n\nSparkSession spark = SparkSession.builder().appName(\"Reducing dependecy by adding more dependencies\").master(\"local[*]\").getOrCreate();\n DataFrame parquet = sqlContext.read().parquet(\"C:/files/myfile.csv.parquet\");\n parquet.show(20);\n\n\nThis solution do satisfy the original conditions in the question. However, it doesn't devoid from the fact that it's like beating around the bush (but hell yeah it's funny). Still, it might helps to open a new possible way to tackle this.",
2020-02-05T02:56:47
Martin :

Here is a complete sample application, also using the LocalInputFile.java class that is part of the solution above, to read a parquet file with minimum dependencies:\nhttps://github.com/GeoscienceAustralia/wit_tooling/tree/main/examples/java/parquet-reader\nIn contrast to the other example solutions, this project also avoids the Avro dependency.",
2022-02-03T14:48:56
yy