FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
testcontainers-python/docs/modules/scylla_example.py at main · mpasternak/testcontainers-python · GitHub
mpasternak
/
testcontainers-python
Public
forked from
testcontainers/testcontainers-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
testcontainers-python
/
docs
/
modules
/
scylla_example.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
153 lines (134 loc) · 5.04 KB
Breadcrumbs
testcontainers-python
/
docs
/
modules
/
scylla_example.py
Copy path
File metadata and controls
153 lines (134 loc) · 5.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import
json
from
datetime
import
datetime
from
cassandra
.
auth
import
PlainTextAuthProvider
from
cassandra
.
cluster
import
Cluster
from
testcontainers
.
community
.
scylla
import
ScyllaContainer
def
basic_example
():
with
ScyllaContainer
()
as
scylla
:
# Get connection parameters
host
=
scylla
.
get_container_host_ip
()
port
=
scylla
.
get_exposed_port
(
scylla
.
port
)
username
=
scylla
.
username
password
=
scylla
.
password
# Create Scylla client
auth_provider
=
PlainTextAuthProvider
(
username
=
username
,
password
=
password
)
cluster
=
Cluster
([
host
],
port
=
port
,
auth_provider
=
auth_provider
)
session
=
cluster
.
connect
()
print
(
"Connected to Scylla"
)
# Create keyspace
session
.
execute
(
"""
CREATE KEYSPACE IF NOT EXISTS test_keyspace
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}
"""
)
print
(
"Created keyspace"
)
# Use keyspace
session
.
set_keyspace
(
"test_keyspace"
)
# Create table
session
.
execute
(
"""
CREATE TABLE IF NOT EXISTS test_table (
id UUID PRIMARY KEY,
name text,
value int,
category text,
created_at timestamp
)
"""
)
print
(
"Created table"
)
# Insert test data
test_data
=
[
{
"id"
:
"550e8400-e29b-41d4-a716-446655440000"
,
"name"
:
"test1"
,
"value"
:
100
,
"category"
:
"A"
,
"created_at"
:
datetime
.
utcnow
(),
},
{
"id"
:
"550e8400-e29b-41d4-a716-446655440001"
,
"name"
:
"test2"
,
"value"
:
200
,
"category"
:
"B"
,
"created_at"
:
datetime
.
utcnow
(),
},
{
"id"
:
"550e8400-e29b-41d4-a716-446655440002"
,
"name"
:
"test3"
,
"value"
:
300
,
"category"
:
"A"
,
"created_at"
:
datetime
.
utcnow
(),
},
]
insert_stmt
=
session
.
prepare
(
"""
INSERT INTO test_table (id, name, value, category, created_at)
VALUES (uuid(), ?, ?, ?, ?)
"""
)
for
data
in
test_data
:
session
.
execute
(
insert_stmt
, (
data
[
"name"
],
data
[
"value"
],
data
[
"category"
],
data
[
"created_at"
]))
print
(
"Inserted test data"
)
# Query data
print
(
"
\n
Query results:"
)
rows
=
session
.
execute
(
"SELECT * FROM test_table WHERE category = 'A' ALLOW FILTERING"
)
for
row
in
rows
:
print
(
json
.
dumps
(
{
"id"
:
str
(
row
.
id
),
"name"
:
row
.
name
,
"value"
:
row
.
value
,
"category"
:
row
.
category
,
"created_at"
:
row
.
created_at
.
isoformat
(),
},
indent
=
2
,
)
)
# Create materialized view
session
.
execute
(
"""
CREATE MATERIALIZED VIEW IF NOT EXISTS test_view AS
SELECT category, name, value, created_at
FROM test_table
WHERE category IS NOT NULL AND name IS NOT NULL
PRIMARY KEY (category, name)
"""
)
print
(
"
\n
Created materialized view"
)
# Query materialized view
print
(
"
\n
Materialized view results:"
)
rows
=
session
.
execute
(
"SELECT * FROM test_view WHERE category = 'A'"
)
for
row
in
rows
:
print
(
json
.
dumps
(
{
"category"
:
row
.
category
,
"name"
:
row
.
name
,
"value"
:
row
.
value
,
"created_at"
:
row
.
created_at
.
isoformat
(),
},
indent
=
2
,
)
)
# Create secondary index
session
.
execute
(
"CREATE INDEX IF NOT EXISTS ON test_table (value)"
)
print
(
"
\n
Created secondary index"
)
# Query using secondary index
print
(
"
\n
Query using secondary index:"
)
rows
=
session
.
execute
(
"SELECT * FROM test_table WHERE value > 150 ALLOW FILTERING"
)
for
row
in
rows
:
print
(
json
.
dumps
(
{
"id"
:
str
(
row
.
id
),
"name"
:
row
.
name
,
"value"
:
row
.
value
,
"category"
:
row
.
category
,
"created_at"
:
row
.
created_at
.
isoformat
(),
},
indent
=
2
,
)
)
# Get table metadata
table_meta
=
session
.
cluster
.
metadata
.
keyspaces
[
"test_keyspace"
].
tables
[
"test_table"
]
print
(
"
\n
Table metadata:"
)
print
(
f"Columns:
{
[
col
.
name
for
col
in
table_meta
.
columns
.
values
()]
}
"
)
print
(
f"Partition key:
{
[
col
.
name
for
col
in
table_meta
.
partition_key
]
}
"
)
print
(
f"Clustering key:
{
[
col
.
name
for
col
in
table_meta
.
clustering_key
]
}
"
)
if
__name__
==
"__main__"
:
basic_example
()
Back
|
FazBrowse Home
|
New Git URL