You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

db_read_csv_txs.py 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from cassandra.cluster import Cluster
  2. from cassandra.query import BoundStatement, BatchStatement
  3. import csv
  4. def db_insert_csv_txs(config, tx_file, skip=0, limit=-1):
  5. print(" == DB TX INSERTION SCRIPT == ")
  6. print(
  7. f"Attempting Cassandra connection @ {config['cassandra_addresses']}:{config['cassandra_port']}")
  8. cluster = Cluster(config['cassandra_addresses'],
  9. port=config['cassandra_port'])
  10. session = cluster.connect(config['cassandra_keyspace'])
  11. print(f"Connection OK")
  12. statement = session.prepare(
  13. f"INSERT INTO {config['tx_table_name']} (tx_id,address,value,tx_hash,block_id,timestamp) VALUES(?,?,?,?,?,?);")
  14. boundStatement = BoundStatement(statement)
  15. with open(tx_file, newline='') as (tx_csv):
  16. rowreader = csv.reader(tx_csv, dialect="excel")
  17. next(rowreader) # skip header
  18. batchStatement = BatchStatement()
  19. batch_count = 0
  20. for i, row in enumerate(rowreader):
  21. if i < skip:
  22. continue
  23. if i == limit:
  24. break
  25. batchStatement.add(boundStatement.bind(
  26. [int(row[0]), str(row[1]), int(row[2]), str(row[3]), int(row[4]), int(row[5])]))
  27. batch_count += 1
  28. if batch_count > 256:
  29. session.execute(batchStatement)
  30. batchStatement = BatchStatement()
  31. batch_count = 0
  32. session.execute(batchStatement)
  33. print("Done!")
  34. cluster.shutdown()