A very common use case when working with Hadoop is to store and query simple files (CSV, TSV, …); then to get better performance and efficient storage convert these files into more efficient format, for example Apache Parquet.
- Columnar format
Let’s take a concrete example, you can find many interesting Open Data sources that distribute data as CSV files- or equivalent format-. So you can store them into your distributed file system and use them in your applications/jobs/analytics queries. This is not the most efficient way especially when we know that these data won’t move that often. So instead of simply storing the CSV let’s copy this information into Parquet.
How to convert CSV files into Parquet files?
You can use code to achieve this, as you can see in the ConvertUtils sample/test class. You can use a simpler way with Apache Drill. Drill allows you save the result of a query as Parquet files.
The following steps will show you how to do convert a simple CSV into a Parquet file using Drill.
- Apache Drill : Standalone Apache Drill or using Apache Drill Sandbox from MapR
- Some CSV Files: for example Passenger Dataset from SFO Air Traffic Statistics
Querying the CSV file
Let’s execute a basic query:
1 2 3 4 5 6 7
As you can see, by default Drill processes each line as an array of columns, all values being simple String. So if you need to do some operations with these values (projection or where clause) you must use the column index, and cast the value to the proper type. You can see a simple example below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
We are now ready to create our Parquet files using the “Create Table As Select” (aka CTAS)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
That’s it! You have now a Parquet file, a single file in our case since our dataset is really small. Apache Drill will create multiples files for the tables depending of the size and configuration your environment.
I invite you to read this Chapter in the Apache Drill documentation to learn more about Drill and Parquet.
Query Parquet Files
Now that you have created your Parquet files you can use them in any of your Hadoop processes, but you can also use them in Drill, as follow:
In this article you have learned how to convert a CSV file using an Apache Drill query.
You can do that with any source supported by Drill, for example from JSON to Parquet, or even a complex join query between multiple data sources. You can also chose a different output format for example JSON, or a CSV.