()
| 32 | |
| 33 | |
| 34 | def main(): |
| 35 | cluster = Cluster(['127.0.0.1']) |
| 36 | session = cluster.connect() |
| 37 | |
| 38 | log.info("creating keyspace...") |
| 39 | session.execute(""" |
| 40 | CREATE KEYSPACE IF NOT EXISTS %s |
| 41 | WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '2' } |
| 42 | """ % KEYSPACE) |
| 43 | |
| 44 | log.info("setting keyspace...") |
| 45 | session.set_keyspace(KEYSPACE) |
| 46 | |
| 47 | log.info("creating table...") |
| 48 | session.execute(""" |
| 49 | CREATE TABLE IF NOT EXISTS mytable ( |
| 50 | thekey text, |
| 51 | col1 text, |
| 52 | col2 text, |
| 53 | PRIMARY KEY (thekey, col1) |
| 54 | ) |
| 55 | """) |
| 56 | |
| 57 | query = SimpleStatement(""" |
| 58 | INSERT INTO mytable (thekey, col1, col2) |
| 59 | VALUES (%(key)s, %(a)s, %(b)s) |
| 60 | """, consistency_level=ConsistencyLevel.ONE) |
| 61 | |
| 62 | prepared = session.prepare(""" |
| 63 | INSERT INTO mytable (thekey, col1, col2) |
| 64 | VALUES (?, ?, ?) |
| 65 | """) |
| 66 | |
| 67 | for i in range(10): |
| 68 | log.info("inserting row %d" % i) |
| 69 | session.execute(query, dict(key="key%d" % i, a='a', b='b')) |
| 70 | session.execute(prepared, ("key%d" % i, 'b', 'b')) |
| 71 | |
| 72 | future = session.execute_async("SELECT * FROM mytable") |
| 73 | log.info("key\tcol1\tcol2") |
| 74 | log.info("---\t----\t----") |
| 75 | |
| 76 | try: |
| 77 | rows = future.result() |
| 78 | except Exception: |
| 79 | log.exception("Error reading rows:") |
| 80 | return |
| 81 | |
| 82 | for row in rows: |
| 83 | log.info('\t'.join(row)) |
| 84 | |
| 85 | session.execute("DROP KEYSPACE " + KEYSPACE) |
| 86 | |
| 87 | if __name__ == "__main__": |
| 88 | main() |
no test coverage detected