Created
January 17, 2017 08:19
-
-
Save alwqx/28c986a208865d476be4b5a8deea34f9 to your computer and use it in GitHub Desktop.
cassandra replace primary key value
This file contains hidden or 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
""" | |
# table schema | |
CREATE TABLE raw.disk_storage ( | |
machine_id text, | |
date date, | |
timestamp timestamp, | |
available bigint, | |
total bigint, | |
used bigint, | |
PRIMARY KEY ((machine_id, date), timestamp) | |
) | |
# dependency | |
pip install cassandra-driver # https://datastax.github.io/python-driver/ | |
""" | |
from cassandra.cluster import Cluster | |
cluster = Cluster([ip1, ip2, ip3]) | |
s=cluster.connect() | |
s.set_keyspace('ks') | |
c = dict() | |
c["m1"] = "cc28f302-2b93-4267-b8cf-2e77596174d3.sysadmin" | |
c["m2"] = "8c8d7874-1820-4985-b0db-8e2f95f35c7e.sysadmin" | |
c["m3"] = "c28b4ba8-5204-4fae-af3d-ee8b179eca5a.sysadmin" | |
c["m4"] = "ded48033-1d86-4cfa-bab0-5677b2d5d3f0.sysadmin" | |
c["m5"] = "bf370d24-44b7-4fd6-b1a0-7f2d1b14b068.sysadmin" | |
date = ['2017-01-13', '2017-01-14', '2017-01-15', '2017-01-16'] | |
pre_insert = s.prepare("insert into disk_storage (machine_id, date, timestamp, available, total, used) values (?, ?, ?, ?, ?, ?)") | |
pre_delete = s.prepare("delete from disk_storage where machine_id=? and date=? and timestamp=?") | |
def replace_machine_id(row, updated_id): | |
s.execute(pre_insert, (updated_id, row.date, row.timestamp, row.available, row.total, row.used), timeout=3600) | |
s.execute(pre_delete, [row.machine_id, row.date, row.timestamp], timeout=3600) | |
pre_key = s.prepare("select * from disk_storage where machine_id=? and date=?") | |
def get_rows_by_key(machine_id, date): | |
rows = s.execute(pre_key, [machine_id, date], timeout=3600) | |
return rows | |
for k in c.keys(): | |
t = 0 | |
for d in date: | |
rows = get_rows_by_key(k,d) | |
for row in rows: | |
replace_machine_id(row, c[k]) | |
print("done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment