db_read_csv_txs.py 1.1KB

12345678910111213141516171819202122232425262728293031
  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):
  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. with open(tx_file, newline='') as tx_csv:
  13. rowreader = csv.reader(tx_csv, dialect="excel")
  14. next(rowreader) # skip header
  15. statement = session.prepare(
  16. f"INSERT INTO {config['tx_table_name']} (tx_id,address,value,tx_hash,block_id,timestamp) VALUES(?,?,?,?,?,?);")
  17. boundStatement = BoundStatement(statement)
  18. batchStatement = BatchStatement()
  19. for row in rowreader:
  20. batchStatement.add(boundStatement.bind(
  21. [int(row[0]), str(row[1]), int(row[2]), str(row[3]), int(row[4]), int(row[5])]))
  22. session.execute(batchStatement)
  23. print("Done!")
  24. cluster.shutdown()