-
Notifications
You must be signed in to change notification settings - Fork 13.7k
/
cassandra_to_gcs.py
393 lines (339 loc) · 15.4 KB
/
cassandra_to_gcs.py
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""This module contains operator for copying data from Cassandra to Google Cloud Storage in JSON format."""
from __future__ import annotations
import json
from base64 import b64encode
from datetime import datetime
from decimal import Decimal
from tempfile import NamedTemporaryFile
from typing import TYPE_CHECKING, Any, Iterable, NewType, Sequence
from uuid import UUID
from cassandra.util import Date, OrderedMapSerializedKey, SortedSet, Time
from airflow.exceptions import AirflowException
from airflow.models import BaseOperator
from airflow.providers.apache.cassandra.hooks.cassandra import CassandraHook
from airflow.providers.google.cloud.hooks.gcs import GCSHook
if TYPE_CHECKING:
from airflow.utils.context import Context
NotSetType = NewType("NotSetType", object)
NOT_SET = NotSetType(object())
class CassandraToGCSOperator(BaseOperator):
"""
Copy data from Cassandra to Google Cloud Storage in JSON format.
Note: Arrays of arrays are not supported.
:param cql: The CQL to execute on the Cassandra table.
:param bucket: The bucket to upload to.
:param filename: The filename to use as the object name when uploading
to Google Cloud Storage. A {} should be specified in the filename
to allow the operator to inject file numbers in cases where the
file is split due to size.
:param schema_filename: If set, the filename to use as the object name
when uploading a .json file containing the BigQuery schema fields
for the table that was dumped from MySQL.
:param approx_max_file_size_bytes: This operator supports the ability
to split large table dumps into multiple files (see notes in the
filename param docs above). This param allows developers to specify the
file size of the splits. Check https://cloud.google.com/storage/quotas
to see the maximum allowed file size for a single object.
:param cassandra_conn_id: Reference to a specific Cassandra hook.
:param gzip: Option to compress file for upload
:param gcp_conn_id: (Optional) The connection ID used to connect to Google Cloud.
:param impersonation_chain: Optional service account to impersonate using short-term
credentials, or chained list of accounts required to get the access_token
of the last account in the list, which will be impersonated in the request.
If set as a string, the account must grant the originating account
the Service Account Token Creator IAM role.
If set as a sequence, the identities from the list must grant
Service Account Token Creator IAM role to the directly preceding identity, with first
account from the list granting this role to the originating account (templated).
:param query_timeout: (Optional) The amount of time, in seconds, used to execute the Cassandra query.
If not set, the timeout value will be set in Session.execute() by Cassandra driver.
If set to None, there is no timeout.
:param encode_uuid: (Optional) Option to encode UUID or not when upload from Cassandra to GCS.
Default is to encode UUID.
"""
template_fields: Sequence[str] = (
"cql",
"bucket",
"filename",
"schema_filename",
"impersonation_chain",
)
template_ext: Sequence[str] = (".cql",)
ui_color = "#a0e08c"
def __init__(
self,
*,
cql: str,
bucket: str,
filename: str,
schema_filename: str | None = None,
approx_max_file_size_bytes: int = 1900000000,
gzip: bool = False,
cassandra_conn_id: str = "cassandra_default",
gcp_conn_id: str = "google_cloud_default",
impersonation_chain: str | Sequence[str] | None = None,
query_timeout: float | None | NotSetType = NOT_SET,
encode_uuid: bool = True,
**kwargs,
) -> None:
super().__init__(**kwargs)
self.cql = cql
self.bucket = bucket
self.filename = filename
self.schema_filename = schema_filename
self.approx_max_file_size_bytes = approx_max_file_size_bytes
self.cassandra_conn_id = cassandra_conn_id
self.gcp_conn_id = gcp_conn_id
self.gzip = gzip
self.impersonation_chain = impersonation_chain
self.query_timeout = query_timeout
self.encode_uuid = encode_uuid
# Default Cassandra to BigQuery type mapping
CQL_TYPE_MAP = {
"BytesType": "STRING",
"DecimalType": "FLOAT",
"UUIDType": "STRING",
"BooleanType": "BOOL",
"ByteType": "INTEGER",
"AsciiType": "STRING",
"FloatType": "FLOAT",
"DoubleType": "FLOAT",
"LongType": "INTEGER",
"Int32Type": "INTEGER",
"IntegerType": "INTEGER",
"InetAddressType": "STRING",
"CounterColumnType": "INTEGER",
"DateType": "TIMESTAMP",
"SimpleDateType": "DATE",
"TimestampType": "TIMESTAMP",
"TimeUUIDType": "STRING",
"ShortType": "INTEGER",
"TimeType": "TIME",
"DurationType": "INTEGER",
"UTF8Type": "STRING",
"VarcharType": "STRING",
}
def execute(self, context: Context):
hook = CassandraHook(cassandra_conn_id=self.cassandra_conn_id)
query_extra = {}
if self.query_timeout is not NOT_SET:
query_extra["timeout"] = self.query_timeout
cursor = hook.get_conn().execute(self.cql, **query_extra)
# If a schema is set, create a BQ schema JSON file.
if self.schema_filename:
self.log.info("Writing local schema file")
schema_file = self._write_local_schema_file(cursor)
# Flush file before uploading
schema_file["file_handle"].flush()
self.log.info("Uploading schema file to GCS.")
self._upload_to_gcs(schema_file)
schema_file["file_handle"].close()
counter = 0
self.log.info("Writing local data files")
for file_to_upload in self._write_local_data_files(cursor):
# Flush file before uploading
file_to_upload["file_handle"].flush()
self.log.info("Uploading chunk file #%d to GCS.", counter)
self._upload_to_gcs(file_to_upload)
self.log.info("Removing local file")
file_to_upload["file_handle"].close()
counter += 1
# Close all sessions and connection associated with this Cassandra cluster
hook.shutdown_cluster()
def _write_local_data_files(self, cursor):
"""
Take a cursor, and writes results to a local file.
:return: A dictionary where keys are filenames to be used as object
names in GCS, and values are file handles to local files that
contain the data for the GCS objects.
"""
file_no = 0
tmp_file_handle = NamedTemporaryFile(delete=True)
file_to_upload = {
"file_name": self.filename.format(file_no),
"file_handle": tmp_file_handle,
}
for row in cursor:
row_dict = self.generate_data_dict(row._fields, row)
content = json.dumps(row_dict).encode("utf-8")
tmp_file_handle.write(content)
# Append newline to make dumps BigQuery compatible.
tmp_file_handle.write(b"\n")
if tmp_file_handle.tell() >= self.approx_max_file_size_bytes:
file_no += 1
yield file_to_upload
tmp_file_handle = NamedTemporaryFile(delete=True)
file_to_upload = {
"file_name": self.filename.format(file_no),
"file_handle": tmp_file_handle,
}
yield file_to_upload
def _write_local_schema_file(self, cursor):
"""
Take a cursor, and writes the BigQuery schema for the results to a local file system.
:return: A dictionary where key is a filename to be used as an object
name in GCS, and values are file handles to local files that
contains the BigQuery schema fields in .json format.
"""
schema = []
tmp_schema_file_handle = NamedTemporaryFile(delete=True)
for name, type_ in zip(cursor.column_names, cursor.column_types):
schema.append(self.generate_schema_dict(name, type_))
json_serialized_schema = json.dumps(schema).encode("utf-8")
tmp_schema_file_handle.write(json_serialized_schema)
schema_file_to_upload = {
"file_name": self.schema_filename,
"file_handle": tmp_schema_file_handle,
}
return schema_file_to_upload
def _upload_to_gcs(self, file_to_upload):
"""Upload a file (data split or schema .json file) to Google Cloud Storage."""
hook = GCSHook(
gcp_conn_id=self.gcp_conn_id,
impersonation_chain=self.impersonation_chain,
)
hook.upload(
bucket_name=self.bucket,
object_name=file_to_upload.get("file_name"),
filename=file_to_upload.get("file_handle").name,
mime_type="application/json",
gzip=self.gzip,
)
def generate_data_dict(self, names: Iterable[str], values: Any) -> dict[str, Any]:
"""Generate data structure that will be stored as file in GCS."""
return {n: self.convert_value(v) for n, v in zip(names, values)}
def convert_value(self, value: Any | None) -> Any | None:
"""Convert value to BQ type."""
if not value or isinstance(value, (str, int, float, bool, dict)):
return value
elif isinstance(value, bytes):
return b64encode(value).decode("ascii")
elif isinstance(value, UUID):
if self.encode_uuid:
return b64encode(value.bytes).decode("ascii")
else:
return str(value)
elif isinstance(value, (datetime, Date)):
return str(value)
elif isinstance(value, Decimal):
return float(value)
elif isinstance(value, Time):
return str(value).split(".")[0]
elif isinstance(value, (list, SortedSet)):
return self.convert_array_types(value)
elif hasattr(value, "_fields"):
return self.convert_user_type(value)
elif isinstance(value, tuple):
return self.convert_tuple_type(value)
elif isinstance(value, OrderedMapSerializedKey):
return self.convert_map_type(value)
else:
raise AirflowException(f"Unexpected value: {value}")
def convert_array_types(self, value: list[Any] | SortedSet) -> list[Any]:
"""Map convert_value over array."""
return [self.convert_value(nested_value) for nested_value in value]
def convert_user_type(self, value: Any) -> dict[str, Any]:
"""
Convert a user type to RECORD that contains n fields, where n is the number of attributes.
Each element in the user type class will be converted to its corresponding data type in BQ.
"""
names = value._fields
values = [self.convert_value(getattr(value, name)) for name in names]
return self.generate_data_dict(names, values)
def convert_tuple_type(self, values: tuple[Any]) -> dict[str, Any]:
"""
Convert a tuple to RECORD that contains n fields.
Each field will be converted to its corresponding data type in bq and
will be named 'field_<index>', where index is determined by the order
of the tuple elements defined in cassandra.
"""
names = [f"field_{i}" for i in range(len(values))]
return self.generate_data_dict(names, values)
def convert_map_type(self, value: OrderedMapSerializedKey) -> list[dict[str, Any]]:
"""
Convert a map to a repeated RECORD that contains two fields: 'key' and 'value'.
Each will be converted to its corresponding data type in BQ.
"""
converted_map = []
for k, v in zip(value.keys(), value.values()):
converted_map.append({"key": self.convert_value(k), "value": self.convert_value(v)})
return converted_map
@classmethod
def generate_schema_dict(cls, name: str, type_: Any) -> dict[str, Any]:
"""Generate BQ schema."""
field_schema: dict[str, Any] = {}
field_schema.update({"name": name})
field_schema.update({"type_": cls.get_bq_type(type_)})
field_schema.update({"mode": cls.get_bq_mode(type_)})
fields = cls.get_bq_fields(type_)
if fields:
field_schema.update({"fields": fields})
return field_schema
@classmethod
def get_bq_fields(cls, type_: Any) -> list[dict[str, Any]]:
"""Convert non simple type value to BQ representation."""
if cls.is_simple_type(type_):
return []
# In case of not simple type
names: list[str] = []
types: list[Any] = []
if cls.is_array_type(type_) and cls.is_record_type(type_.subtypes[0]):
names = type_.subtypes[0].fieldnames
types = type_.subtypes[0].subtypes
elif cls.is_record_type(type_):
names = type_.fieldnames
types = type_.subtypes
if types and not names and type_.cassname == "TupleType":
names = [f"field_{i}" for i in range(len(types))]
elif types and not names and type_.cassname == "MapType":
names = ["key", "value"]
return [cls.generate_schema_dict(n, t) for n, t in zip(names, types)]
@staticmethod
def is_simple_type(type_: Any) -> bool:
"""Check if type is a simple type."""
return type_.cassname in CassandraToGCSOperator.CQL_TYPE_MAP
@staticmethod
def is_array_type(type_: Any) -> bool:
"""Check if type is an array type."""
return type_.cassname in ["ListType", "SetType"]
@staticmethod
def is_record_type(type_: Any) -> bool:
"""Check the record type."""
return type_.cassname in ["UserType", "TupleType", "MapType"]
@classmethod
def get_bq_type(cls, type_: Any) -> str:
"""Convert type to equivalent BQ type."""
if cls.is_simple_type(type_):
return CassandraToGCSOperator.CQL_TYPE_MAP[type_.cassname]
elif cls.is_record_type(type_):
return "RECORD"
elif cls.is_array_type(type_):
return cls.get_bq_type(type_.subtypes[0])
else:
raise AirflowException("Not a supported type_: " + type_.cassname)
@classmethod
def get_bq_mode(cls, type_: Any) -> str:
"""Convert type to equivalent BQ mode."""
if cls.is_array_type(type_) or type_.cassname == "MapType":
return "REPEATED"
elif cls.is_record_type(type_) or cls.is_simple_type(type_):
return "NULLABLE"
else:
raise AirflowException("Not a supported type_: " + type_.cassname)