/
writer.py
670 lines (538 loc) · 22.8 KB
/
writer.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
import abc
import gzip
import io
import logging
import os
import tempfile
import time
import uuid
from contextlib import ExitStack
import msgpack
import numpy as np
import pandas as pd
from tdclient.util import normalized_msgpack
from .spark import fetch_td_spark_context
logger = logging.getLogger(__name__)
def _is_pd_na(x):
is_na = pd.isna(x)
return isinstance(is_na, bool) and is_na
def _is_np_nan(x):
return isinstance(x, float) and np.isnan(x)
def _is_0d_ary(x):
return isinstance(x, np.ndarray) and len(x.shape) == 0
def _is_0d_nan(x):
return _is_0d_ary(x) and x.dtype.kind == "f" and np.isnan(x)
def _isnull(x):
return x is None or _is_np_nan(x) or _is_pd_na(x)
def _isinstance_or_null(x, t):
return _isnull(x) or isinstance(x, t)
def _replace_pd_na(dataframe):
"""Replace np.nan to None to avoid Int64 conversion issue"""
if dataframe.isnull().any().any():
dataframe.replace({np.nan: None}, inplace=True)
def _to_list(ary):
# Return None if None, np.nan, or np.nan in 0-d array given
if ary is None or _is_np_nan(ary) or _is_0d_nan(ary):
return None
# Return Python primitive value if 0-d array given
if _is_0d_ary(ary):
return ary.tolist()
_ary = np.asarray(ary)
# Replace numpy.nan to None which will be converted to NULL on TD
kind = _ary.dtype.kind
if kind == "f":
_ary = np.where(np.isnan(_ary), None, _ary)
elif kind == "U":
_ary = np.where(_ary == "nan", None, _ary)
elif kind == "O":
_ary = np.array([None if _is_np_nan(x) or _is_pd_na(x) else x for x in _ary])
return _ary.tolist()
def _convert_nullable_str(x, t, lower=False):
v = str(x).lower() if lower else str(x)
return v if isinstance(x, t) else None
def _cast_dtypes(dataframe, inplace=True, keep_list=False):
"""Convert dtypes into one of {int, float, str, list} type.
A character code (one of ‘biufcmMOSUV’) identifying the general kind of data.
b boolean
i signed integer
u unsigned integer
f floating-point
c complex floating-point
m timedelta
M datetime
O object
S (byte-)string
U Unicode
V void
"""
df = dataframe if inplace else dataframe.copy()
for column, kind in dataframe.dtypes.apply(lambda dtype: dtype.kind).items():
t = str
if kind in ("i", "u"):
t = "Int64" if df[column].isnull().any() else "int64"
elif kind == "f":
t = float
elif kind in ("b", "O"):
t = object
if df[column].apply(_isinstance_or_null, args=((list, np.ndarray),)).all():
if keep_list:
df[column] = df[column].apply(_to_list)
else:
df[column] = df[column].apply(
_convert_nullable_str, args=((list, np.ndarray),)
)
elif df[column].apply(_isinstance_or_null, args=(bool,)).all():
# Bulk Import API internally handles boolean string as a boolean type,
# and hence "True" ("False") will be stored as "true" ("false"). Align
# to lower case here.
df[column] = df[column].apply(
_convert_nullable_str, args=(bool,), lower=True
)
elif df[column].apply(_isinstance_or_null, args=(str,)).all():
df[column] = df[column].apply(_convert_nullable_str, args=(str,))
else:
t = str
df[column] = df[column].astype(t)
if not inplace:
return df
def _get_schema(dataframe):
column_names, column_types = [], []
for c, t in zip(dataframe.columns, dataframe.dtypes):
# Compare nullable integer type by using pandas function because `t ==
# "Int64"` causes a following warning for some reasons:
# DeprecationWarning: Numeric-style type codes are deprecated and
# will result in an error in the future.
if t == "int64" or pd.core.dtypes.common.is_dtype_equal(t, "Int64"):
presto_type = "bigint"
elif t == "float64":
presto_type = "double"
else:
presto_type = "varchar"
logger.info(
f"column '{c}' has non-numeric. The values are stored as "
"'varchar' type on Treasure Data."
)
column_names.append(c)
column_types.append(presto_type)
return column_names, column_types
class Writer(metaclass=abc.ABCMeta):
def __init__(self):
self.closed = False
@abc.abstractmethod
def write_dataframe(self, dataframe, table, if_exists):
pass
def close(self):
self.closed = True
@staticmethod
def from_string(writer, **kwargs):
writer = writer.lower()
if writer == "bulk_import":
return BulkImportWriter()
elif writer == "insert_into":
return InsertIntoWriter()
elif writer == "spark":
return SparkWriter(**kwargs)
else:
raise ValueError("unknown way to upload data to TD is specified")
class InsertIntoWriter(Writer):
"""A writer module that loads Python data to Treasure Data by issueing
INSERT INTO query in Presto.
"""
def write_dataframe(self, dataframe, table, if_exists):
"""Write a given DataFrame to a Treasure Data table.
Parameters
----------
dataframe : :class:`pandas.DataFrame`
Data loaded to a target table.
table : :class:`pytd.table.Table`
Target table.
if_exists : {'error', 'overwrite', 'append', 'ignore'}
What happens when a target table already exists.
- error: raise an exception.
- overwrite: drop it, recreate it, and insert data.
- append: insert data. Create if does not exist.
- ignore: do nothing.
"""
if self.closed:
raise RuntimeError("this writer is already closed and no longer available")
if isinstance(table, str):
raise TypeError(f"table '{table}' should be pytd.table.Table, not str")
_cast_dtypes(dataframe)
column_names, column_types = _get_schema(dataframe)
self._insert_into(
table,
list(dataframe.itertuples(index=False, name=None)),
column_names,
column_types,
if_exists,
)
def _insert_into(self, table, list_of_tuple, column_names, column_types, if_exists):
"""Write a given lists to a Treasure Data table.
Parameters
----------
table : :class:`pytd.table.Table`
Target table.
list_of_tuple : list of tuples
Data loaded to a target table. Each element is a tuple that
represents single table row.
column_names : list of str
Column names.
column_types : list of str
Column types corresponding to the names. Note that Treasure Data
supports limited amount of types as documented in:
https://docs.treasuredata.com/display/public/PD/Schema+Management
if_exists : {'error', 'overwrite', 'append', 'ignore'}
What happens when a target table already exists.
- error: raise an exception.
- overwrite: drop it, recreate it, and insert data.
- append: insert data. Create if does not exist.
- ignore: do nothing.
"""
if table.exists:
if if_exists == "error":
raise RuntimeError(
f"target table '{table.database}.{table.table}' already exists"
)
elif if_exists == "ignore":
return
elif if_exists == "append":
pass
elif if_exists == "overwrite":
table.delete()
table.create(column_names, column_types)
else:
raise ValueError(f"invalid valud for if_exists: {if_exists}")
else:
table.create(column_names, column_types)
q_insert = self._build_query(
table.database, table.table, list_of_tuple, column_names
)
table.client.query(q_insert, engine="presto")
def _build_query(self, database, table, list_of_tuple, column_names):
"""Translates the given data into an ``INSERT INTO ... VALUES ...``
Presto query.
Parameters
----------
database : str
Target database name.
table : str
Target table name.
list_of_tuple : list of tuples
Data loaded to a target table. Each element is a tuple that
represents single table row.
column_names : list of str
Column names.
"""
rows = []
for tpl in list_of_tuple:
list_of_value_strings = [
(
f"""'{e.replace("'", '"')}'"""
if isinstance(e, str)
else ("null" if pd.isnull(e) else str(e))
)
for e in tpl
]
rows.append(f"({', '.join(list_of_value_strings)})")
return (
f"INSERT INTO {database}.{table} "
f"({', '.join(map(str, column_names))}) "
f"VALUES {', '.join(rows)}"
)
class BulkImportWriter(Writer):
"""A writer module that loads Python data to Treasure Data by using
td-client-python's bulk importer.
"""
def write_dataframe(self, dataframe, table, if_exists, fmt="csv", keep_list=False):
"""Write a given DataFrame to a Treasure Data table.
This method internally converts a given :class:`pandas.DataFrame` into a
temporary CSV/msgpack file, and upload the file to Treasure Data via bulk
import API.
Note:
If you pass a dataframe with ``Int64`` column, the column will be converted
as ``varchar`` on Treasure Data schema due to BulkImport API restriction.
Parameters
----------
dataframe : :class:`pandas.DataFrame`
Data loaded to a target table.
table : :class:`pytd.table.Table`
Target table.
if_exists : {'error', 'overwrite', 'append', 'ignore'}
What happens when a target table already exists.
- error: raise an exception.
- overwrite: drop it, recreate it, and insert data.
- append: insert data. Create if does not exist.
- ignore: do nothing.
fmt : {'csv', 'msgpack'}, default: 'csv'
Format for bulk_import.
- csv
Convert dataframe to temporary CSV file. Stable option but slower
than msgpack option because pytd saves dataframe as temporary CSV file,
then td-client converts it to msgpack.
Types of columns are guessed by ``pandas.read_csv`` and it causes
unintended type conversion e.g., 0-padded string ``"00012"`` into
integer ``12``.
- msgpack
Convert to temporary msgpack.gz file. Fast option but there is a
slight difference on type conversion compared to csv.
keep_list : boolean, default: False
If this argument is True, keep list or numpy.ndarray column as list, which
will be converted array<T> on Treasure Data table.
Each type of element of list will be converted by
``numpy.array(your_list).tolist()``.
If True, ``fmt`` argument will be overwritten with ``msgpack``.
Examples
---------
A dataframe containing list will be treated array<T> in TD.
>>> import pytd
>>> import numpy as np
>>> import pandas as pd
>>> df = pd.DataFrame(
... {
... "a": [[1, 2, 3], [2, 3, 4]],
... "b": [[0, None, 2], [2, 3, 4]],
... "c": [np.array([1, np.nan, 3]), [2, 3, 4]]
... }
... )
>>> client = pytd.Client()
>>> table = pytd.table.Table(client, "mydb", "test")
>>> writer = pytd.writer.BulkImportWriter()
>>> writer.write_dataframe(df, table, if_exists="overwrite", keep_list=True)
In this case, the type of columns will be:
``{"a": array<int>, "b": array<string>, "c": array<string>}``
If you want to set the type after ingestion, you need to run
``tdclient.Client.update_schema`` like:
>>> client.api_client.update_schema(
... "mydb",
... "test",
... [
... ["a", "array<long>", "a"],
... ["b", "array<int>", "b"],
... ["c", "array<int>", "c"],
... ],
... )
Note that ``numpy.nan`` will be converted as a string value as ``"NaN"`` or
``"nan"``, so pytd will convert ``numpy.nan`` to ``None`` only when the
dtype of a ndarray is `float`.
Also, numpy converts integer array including ``numpy.nan`` into float array
because ``numpy.nan`` is a Floating Point Special Value. See also:
https://docs.scipy.org/doc/numpy-1.13.0/user/misc.html#ieee-754-floating-point-special-values
Or, you can use :func:`Client.load_table_from_dataframe` function as well.
>>> client.load_table_from_dataframe(df, "bulk_import", keep_list=True)
"""
if self.closed:
raise RuntimeError("this writer is already closed and no longer available")
if isinstance(table, str):
raise TypeError(f"table '{table}' should be pytd.table.Table, not str")
if "time" not in dataframe.columns: # need time column for bulk import
dataframe["time"] = int(time.time())
# We enforce using "msgpack" format for list since CSV can't handle list.
if keep_list:
fmt = "msgpack"
_cast_dtypes(dataframe, keep_list=keep_list)
with ExitStack() as stack:
if fmt == "csv":
fp = tempfile.NamedTemporaryFile(suffix=".csv", delete=False)
stack.callback(os.unlink, fp.name)
stack.callback(fp.close)
dataframe.to_csv(fp.name)
elif fmt == "msgpack":
_replace_pd_na(dataframe)
fp = io.BytesIO()
fp = self._write_msgpack_stream(dataframe.to_dict(orient="records"), fp)
stack.callback(fp.close)
else:
raise ValueError(
f"unsupported format '{fmt}' for bulk import. "
"should be 'csv' or 'msgpack'"
)
self._bulk_import(table, fp, if_exists, fmt)
stack.close()
def _bulk_import(self, table, file_like, if_exists, fmt="csv"):
"""Write a specified CSV file to a Treasure Data table.
This method uploads the file to Treasure Data via bulk import API.
Parameters
----------
table : :class:`pytd.table.Table`
Target table.
file_like : File like object
Data in this file will be loaded to a target table.
if_exists : str, {'error', 'overwrite', 'append', 'ignore'}
What happens when a target table already exists.
- error: raise an exception.
- overwrite: drop it, recreate it, and insert data.
- append: insert data. Create if does not exist.
- ignore: do nothing.
fmt : str, optional, {'csv', 'msgpack'}, default: 'csv'
File format for bulk import. See also :func:`write_dataframe`
"""
params = None
if table.exists:
if if_exists == "error":
raise RuntimeError(
f"target table '{table.database}.{table.table}' already exists"
)
elif if_exists == "ignore":
return
elif if_exists == "append":
params = {"mode": "append"}
elif if_exists == "overwrite":
table.delete()
table.create()
else:
raise ValueError(f"invalid value for if_exists: {if_exists}")
else:
table.create()
session_name = f"session-{uuid.uuid1()}"
bulk_import = table.client.api_client.create_bulk_import(
session_name, table.database, table.table, params=params
)
try:
logger.info(f"uploading data converted into a {fmt} file")
if fmt == "msgpack":
size = file_like.getbuffer().nbytes
# To skip API._prepare_file(), which recreate msgpack again.
bulk_import.upload_part("part", file_like, size)
else:
bulk_import.upload_file("part", fmt, file_like)
bulk_import.freeze()
except Exception as e:
bulk_import.delete()
raise RuntimeError(f"failed to upload file: {e}")
logger.info("performing a bulk import job")
job = bulk_import.perform(wait=True)
if 0 < bulk_import.error_records:
logger.warning(
f"[job id {job.id}] detected {bulk_import.error_records} error records."
)
if 0 < bulk_import.valid_records:
logger.info(
f"[job id {job.id}] imported {bulk_import.valid_records} records."
)
else:
raise RuntimeError(
f"[job id {job.id}] no records have been imported: {bulk_import.name}"
)
bulk_import.commit(wait=True)
bulk_import.delete()
def _write_msgpack_stream(self, items, stream):
"""Write MessagePack stream
Parameters
----------
items : list of dict
Same format with dataframe.to_dict(orient="records")
Examples:
``[{"time": 12345, "col1": "foo"}, {"time": 12345, "col1": "bar"}]``
stream : File like object
Target file like object which has `write()` function. This object will be
updated in this function.
"""
with gzip.GzipFile(mode="wb", fileobj=stream) as gz:
packer = msgpack.Packer()
for item in items:
try:
mp = packer.pack(item)
except (OverflowError, ValueError):
packer.reset()
mp = packer.pack(normalized_msgpack(item))
gz.write(mp)
stream.seek(0)
return stream
class SparkWriter(Writer):
"""A writer module that loads Python data to Treasure Data.
Parameters
----------
td_spark_path : str, optional
Path to td-spark-assembly-{td-spark-version}_spark{spark-version}.jar.
If not given, seek a path ``TDSparkContextBuilder.default_jar_path()``
by default.
download_if_missing : bool, default: True
Download td-spark if it does not exist at the time of initialization.
spark_configs : dict, optional
Additional Spark configurations to be set via ``SparkConf``'s ``set`` method.
Attributes
----------
td_spark_path : str
Path to td-spark-assembly-{td-spark-version}_spark{spark-version}.jar.
download_if_missing : bool
Download td-spark if it does not exist at the time of initialization.
spark_configs : dict
Additional Spark configurations to be set via ``SparkConf``'s ``set`` method.
td_spark : :class:`td_pyspark.TDSparkContext`
Connection of td-spark
"""
def __init__(
self, td_spark_path=None, download_if_missing=True, spark_configs=None
):
self.td_spark_path = td_spark_path
self.download_if_missing = download_if_missing
self.spark_configs = spark_configs
self.td_spark = None
self.fetched_apikey, self.fetched_endpoint = "", ""
@property
def closed(self):
return self.td_spark is not None and self.td_spark.spark._jsc.sc().isStopped()
def write_dataframe(self, dataframe, table, if_exists):
"""Write a given DataFrame to a Treasure Data table.
This method internally converts a given :class:`pandas.DataFrame` into Spark
DataFrame, and directly writes to Treasure Data's main storage
so-called Plazma through a PySpark session.
Parameters
----------
dataframe : :class:`pandas.DataFrame`
Data loaded to a target table.
table : :class:`pytd.table.Table`
Target table.
if_exists : {'error', 'overwrite', 'append', 'ignore'}
What happens when a target table already exists.
- error: raise an exception.
- overwrite: drop it, recreate it, and insert data.
- append: insert data. Create if does not exist.
- ignore: do nothing.
"""
if self.closed:
raise RuntimeError("this writer is already closed and no longer available")
if if_exists not in ("error", "overwrite", "append", "ignore"):
raise ValueError(f"invalid value for if_exists: {if_exists}")
if isinstance(table, str):
raise TypeError(f"table '{table}' should be pytd.table.Table, not str")
if self.td_spark is None:
self.td_spark = fetch_td_spark_context(
table.client.apikey,
table.client.endpoint,
self.td_spark_path,
self.download_if_missing,
self.spark_configs,
)
self.fetched_apikey, self.fetched_endpoint = (
table.client.apikey,
table.client.endpoint,
)
elif (
table.client.apikey != self.fetched_apikey
or table.client.endpoint != self.fetched_endpoint
):
raise ValueError(
"given Table instance and SparkSession have different apikey"
"and/or endpoint. Create and use a new SparkWriter instance."
)
from py4j.protocol import Py4JJavaError
_cast_dtypes(dataframe)
_replace_pd_na(dataframe)
sdf = self.td_spark.spark.createDataFrame(dataframe)
try:
destination = f"{table.database}.{table.table}"
self.td_spark.write(sdf, destination, if_exists)
except Py4JJavaError as e:
if "API_ACCESS_FAILURE" in str(e.java_exception):
raise PermissionError(
"failed to access to Treasure Data Plazma API."
"Contact customer support to enable access rights."
)
raise RuntimeError(
"failed to load table via td-spark: " + str(e.java_exception)
)
def close(self):
"""Close a PySpark session connected to Treasure Data."""
if self.td_spark is not None:
self.td_spark.spark.stop()