Batch insert python script. It is using the python driver provided by Datastax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from cassandra.cluster import Cluster | |
from cassandra.query import * | |
from cassandra import * | |
import hashlib | |
import sys | |
KEYSPACE = 'test' | |
TABLE = 'user' | |
BATCH_SIZE = 1000 | |
cluster = Cluster() | |
session = cluster.connect(KEYSPACE) | |
def getHash(val): | |
return hashlib.sha256(str(val)).hexdigest() | |
def batchInsert(start, num): | |
insert_q = session.prepare("insert into test.user (user_id, fname, lname, number) values (?, ?, ?, ?)") | |
for i in range(start, num): | |
batch = BatchStatement() | |
for j in range(1, BATCH_SIZE + 1): | |
hashval = getHash(j) | |
batch.add(insert_q, (i, hashval, hashval, i + j)) | |
try: | |
session.execute(batch) | |
except WriteTimeout as e: | |
print "write timeout occurred." | |
if __name__ == "__main__": | |
start = int(sys.argv[1]) | |
num = int(sys.argv[2]) | |
batchInsert(start, num) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Setup
See: Installation
install dependencies
To install cassandra driver you can use pip