Repository navigation
Expand file tree
/
Copy pathquery.py
More file actions
155 lines (135 loc) · 5.63 KB
/
Copy pathquery.py
File metadata and controls
155 lines (135 loc) · 5.63 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
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
154
155
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
from datetime import date
import json
from ssl import SSLContext, PROTOCOL_TLSv1_2, CERT_REQUIRED
from cassandra.cluster import (
Cluster,
ExecutionProfile,
EXEC_PROFILE_DEFAULT,
DCAwareRoundRobinPolicy,
)
from cassandra import ConsistencyLevel
from cassandra.query import SimpleStatement
from cassandra_sigv4.auth import SigV4AuthProvider
# snippet-start:[python.example_code.keyspaces.QueryManager.class]
class QueryManager:
"""
Manages queries to an Amazon Keyspaces (for Apache Cassandra) keyspace.
Queries are secured by TLS and authenticated by using the Signature V4 (SigV4)
AWS signing protocol. This is more secure than sending username and password
with a plain-text authentication provider.
This example downloads a default certificate to secure TLS, or lets you specify
your own.
This example uses a table of movie data to demonstrate basic queries.
"""
DEFAULT_CERT_FILE = "sf-class2-root.crt"
CERT_URL = f"https://certs.secureserver.net/repository/sf-class2-root.crt"
def __init__(self, cert_file_path, boto_session, keyspace_name):
"""
:param cert_file_path: The path and file name of the certificate used for TLS.
:param boto_session: A Boto3 session. This is used to acquire your AWS credentials.
:param keyspace_name: The name of the keyspace to connect.
"""
self.cert_file_path = cert_file_path
self.boto_session = boto_session
self.ks_name = keyspace_name
self.cluster = None
self.session = None
def __enter__(self):
"""
Creates a session connection to the keyspace that is secured by TLS and
authenticated by SigV4.
"""
ssl_context = SSLContext(PROTOCOL_TLSv1_2)
ssl_context.load_verify_locations(self.cert_file_path)
ssl_context.verify_mode = CERT_REQUIRED
auth_provider = SigV4AuthProvider(self.boto_session)
contact_point = f"cassandra.{self.boto_session.region_name}.amazonaws.com"
exec_profile = ExecutionProfile(
consistency_level=ConsistencyLevel.LOCAL_QUORUM,
load_balancing_policy=DCAwareRoundRobinPolicy(),
)
self.cluster = Cluster(
[contact_point],
ssl_context=ssl_context,
auth_provider=auth_provider,
port=9142,
execution_profiles={EXEC_PROFILE_DEFAULT: exec_profile},
protocol_version=4,
)
self.cluster.__enter__()
self.session = self.cluster.connect(self.ks_name)
return self
def __exit__(self, *args):
"""
Exits the cluster. This shuts down all existing session connections.
"""
self.cluster.__exit__(*args)
def add_movies(self, table_name, movie_file_path):
"""
Gets movies from a JSON file and adds them to a table in the keyspace.
:param table_name: The name of the table.
:param movie_file_path: The path and file name of a JSON file that contains movie data.
"""
with open(movie_file_path, "r") as movie_file:
movies = json.loads(movie_file.read())
stmt = self.session.prepare(
f"INSERT INTO {table_name} (year, title, release_date, plot) VALUES (?, ?, ?, ?);"
)
for movie in movies[:20]:
self.session.execute(
stmt,
parameters=[
movie["year"],
movie["title"],
date.fromisoformat(movie["info"]["release_date"].partition("T")[0]),
movie["info"]["plot"],
],
)
def get_movies(self, table_name, watched=None):
"""
Gets the title and year of the full list of movies from the table.
:param table_name: The name of the movie table.
:param watched: When specified, the returned list of movies is filtered to
either movies that have been watched or movies that have not
been watched. Otherwise, all movies are returned.
:return: A list of movies in the table.
"""
if watched is None:
stmt = SimpleStatement(f"SELECT title, year from {table_name}")
params = None
else:
stmt = SimpleStatement(
f"SELECT title, year from {table_name} WHERE watched = %s ALLOW FILTERING"
)
params = [watched]
return self.session.execute(stmt, parameters=params).all()
def get_movie(self, table_name, title, year):
"""
Gets a single movie from the table, by title and year.
:param table_name: The name of the movie table.
:param title: The title of the movie.
:param year: The year of the movie's release.
:return: The requested movie.
"""
return self.session.execute(
SimpleStatement(
f"SELECT * from {table_name} WHERE title = %s AND year = %s"
),
parameters=[title, year],
).one()
def watched_movie(self, table_name, title, year):
"""
Updates a movie as having been watched.
:param table_name: The name of the movie table.
:param title: The title of the movie.
:param year: The year of the movie's release.
"""
self.session.execute(
SimpleStatement(
f"UPDATE {table_name} SET watched=true WHERE title = %s AND year = %s"
),
parameters=[title, year],
)
# snippet-end:[python.example_code.keyspaces.QueryManager.class]