Convert CSV To Parquet Or Avro
Introduction
Converting from CSV to parquet or avro reduces the size of the file and allows for faster read and write speeds. With Bacalhau, you can convert your CSV files stored on ipfs or on the web without the need to download files and install dependencies locally.
In this example tutorial we will convert a CSV file from a URL to parquet format and save the converted parquet file to IPFS
TD;LR
Converting CSV stored in public storage with Bacalhau
Prerequisites
To get started, you need to install the Bacalhau client, see more information here
Running CSV to Arvo or Parquet Locally
Installing dependencies
Install the following dependencies
Run the following commands:
Viewing the parquet file
Containerize Script with Docker
:::info You can skip this section entirely and directly go to running on Bacalhau :::
To build your own docker container, create a Dockerfile
, which contains instructions to build your image.
:::info See more information on how to containerize your script/apphere :::
Build the container
We will run docker build
command to build the container;
Before running the command replace;
hub-user with your docker hub username, If you don’t have a docker hub account follow these instructions to create docker account, and use the username of the account you created
repo-name with the name of the container, you can name it anything you want
tag this is not required but you can use the latest tag
In our case:
Push the container
Next, upload the image to the registry. This can be done by using the Docker hub username, repo name or tag.
In our case:
Running a Bacalhau Job
To submit a job, we are going to either mount the script from an IPFS or from an URL.
Mounting the CSV File from IPFS
With the command below, we are gmounting the CSV file for transactions from IPFS
Structure of the command
Let's look closely at the command above:
bacalhau docker run
: call to bacalhau-i ipfs://QmTAQMGiSv9xocaB4PUCT5nSBHrf9HZrYj21BAZ5nMTY2W
: CIDs to use on the job. Mounts them at '/inputs' in the execution.jsacex/csv-to-arrow-or-parque
: the name and the tag of the docker image we are using../inputs/movies.csv
: path to input dataset../outputs/movies.parquet parquet
: path to the outputpython3 src/converter.py
: execute the script
Mounting the CSV File from a URL
To mount the CSV file from a URL
Structure of the command
Let's look closely at the command above:
bacalhau docker run
: call to bacalhau-i https://raw.githubusercontent.com/bacalhau-project/csv_to_avro_or_parquet/master/movies.csv
: URL: path of the input data volumes downloaded from a URL sourcejsacex/csv-to-arrow-or-parque
: the name and the tag of the docker image we are using../inputs/movies.csv
: path to the input dataset../outputs/movies.parquet parquet
: path to the outputpython3 src/converter.py
: execute the script
When a job is submitted, Bacalhau prints out the related job_id
. We store that in an environment variable so that we can reuse it later on.
Checking the State of your Jobs
Job status: You can check the status of the job using
bacalhau list
.
:::note Replace the {JOB_ID}
with your generated ID. :::
When it says Published
or Completed
, that means the job is done, and we can get the results.
Job information: You can find out more information about your job by using
bacalhau describe
.
Job download: You can download your job results directly by using
bacalhau get
. Alternatively, you can choose to create a directory to store your results. In the command below, we created a directory and downloaded our job output to be stored in that directory.
Viewing your Job Output
To view the file, run the following command:
Alternatively, you can do this.
Need Support?
For questions, and feedback, please reach out in our forum