Repository navigation
Expand file tree
/
Copy pathlarge_partition.py
More file actions
executable file
·68 lines (54 loc) · 2.05 KB
/
Copy pathlarge_partition.py
File metadata and controls
executable file
·68 lines (54 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
import random
import string
from cassandra import ConsistencyLevel
from cassandra.concurrent import execute_concurrent_with_args
from cassandra.cluster import Cluster, ExecutionProfile
from cassandra.policies import DCAwareRoundRobinPolicy, TokenAwarePolicy
################################
# Initial Connection #
################################
keyspace = "masterclass"
table = "large_partition"
nodes = ['127.0.0.1']
profiles = {'datacenter1': ExecutionProfile(load_balancing_policy=TokenAwarePolicy(DCAwareRoundRobinPolicy(local_dc='datacenter1')),
consistency_level=ConsistencyLevel.QUORUM)}
cluster = Cluster(contact_points=nodes, protocol_version=3, execution_profiles=profiles)
session = cluster.connect()
################################
# Schema Creation #
################################
ks_stmt = f"""
CREATE KEYSPACE IF NOT EXISTS {keyspace} WITH
replication = {{'class': 'SimpleStrategy', 'replication_factor': 1}}
"""
tbl_stmt = f"""
CREATE TABLE IF NOT EXISTS {table} (
pk int,
ck int,
data blob,
PRIMARY KEY(pk, ck)
)
"""
session.execute(ks_stmt)
session.set_keyspace(keyspace)
session.execute(tbl_stmt)
################################
# Ingestion start #
################################
pstmt = f"INSERT INTO {table} (pk, ck, data) VALUES (1, ?, ?)"
pstmt = session.prepare(query=pstmt)
# 10K payload * 1e7 ~= 100G (uncompressed)
record_count = int(1e7)
payload_count = int(5e4)
print(f"Generating {payload_count} blob entries. This should take a few seconds ...")
parms = []
blobs = []
for i in range(int(5e4)):
data = ''.join(random.choices(string.ascii_letters, k=10000)).encode('ascii')
blobs.append(data)
print("Creating parameter list...")
for i in range(record_count):
parms.append((i, blobs[i % payload_count]))
print("Fire in the hole!")
execute_concurrent_with_args(session, pstmt, parms, concurrency=75)