Suraj Adhikari
Suraj Adhikari

Reputation: 11

How do I import CSV in Cassandra using the Python driver?

I need to import CSV file into cassandra using python driver

import csv

with open('place_ratings.csv') as f_in:
   companies = csv.load(f_in)

with open('place_ratings.csv') as f_in:
   companies = csv.reader(f_in)

with open('C:\apache-cassandra-3.11.7\MonashMRDB_datasets\place_ratings.csv') as f_in:
   companies = csv.reader(f_in)

I tried everything above, wil r, rt and almost every option

I still get the error:

  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'user_ratings.csv'

what could be done here?

Upvotes: 1

Views: 441

Answers (1)

Erick Ramirez
Erick Ramirez

Reputation: 16293

The issue appears to be the path to the CSV.

Use a forward slash instead which works for both Unix and Windows. For example:

C:/apache-cassandra-3.11.7/MonashMRDB_datasets/place_ratings.csv

The alternative on Windows is to escape the backslash with another backslash:

C:\\apache-cassandra-3.11.7\\MonashMRDB_datasets\\place_ratings.csv

But the best practice is to use os.path so it's guaranteed to work on any platform:

os.path.join("C:", "apache-cassandra-3.11.7\MonashMRDB_datasets", "place_ratings.csv")

As a side note, writing new code for importing CSVs is reinventing the wheel. I recommend you use the free DataStax Bulk Loader (docs, source) tool (dsbulk). DSBulk lets you load/unload data from/to CSV or JSON. Cheers!

Upvotes: 4

Related Questions