Anant example-cassandra-cql-copy
License: No License Provided
Language: No Language Provided
We cover how we can use CQL Copy for data operations. A step-by-step demo is shown below to walkthrough how you can do data operations such as exporting a Cassandra table as a CSV and then importing data from a CSV into a Cassandra table.
cd
into it.cd cql-copy
docker run -d -it --name cassandra -v "$(pwd)":/cql-copy cassandra:latest
docker exec -it cassandra bash
ls
docker exec -it cassandra cqlsh
source '/cql-copy/spacecraft_journey_catalog.cql'
In this cql file, we create 2 tables: spacecraft_journey_catalog and duration_by_journey_summary. We also insert 1000 records into spacecraft_journey_catalog. As a sneak-peek of the scenario, we will then export this table as CSV, do some “transformations” on it, and load the “transformed” data CSV into the duration_by_journey_summary table.
use demo ;
describe tables ;
select count(*) from spacecraft_journey_catalog ;
copy spacecraft_journey_catalog (summary, journey_id, end, start) to'../spacecraft_journey_catalog.csv' with header = true ;
ls
docker ps
docker cp container_ID:/spacecraft_journey_catalog.csv .
This will export the CSV to our local cql-copy directory. You can open your file manager to visualize this.
That covers how to export your Cassandra data using CQL COPY TO. Now we will cover how to import data using CQL COPY FROM.
As mentioned before, we will “transform” the CSV that we exported. However, to speed up the demo, I have already updated and calculated the duration in days for you. The “transformed” data is in the CSV file called duration_by_journey_summary.csv. You can see this in the cql-copy directory. Technically, we have already imported the file when we mounted it, but we will show you how to import it from the local directory into the root level of the docker container.
docker cp duration_by_journey_summary.csv container_ID:/
ls
copy duration_by_journey_summary (summary, journey_id, duration_in_days) from '../duration_by_journey_summary.csv' WITH HEADER = TRUE ;
select count(*) from duration_by_journey_summary ;
And with that we have done CQL Copy for data operations by exporting and importing CSV data from and to our Cassandra tables.
Join Our Newsletter!
Sign up below to receive email updates and see what's going on with our company.