forked from aws/aws-sdk-pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_write.py
More file actions
759 lines (712 loc) · 29.9 KB
/
Copy path_write.py
File metadata and controls
759 lines (712 loc) · 29.9 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
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
"""Amazon Redshift Write Module (PRIVATE)."""
from __future__ import annotations
import logging
from typing import TYPE_CHECKING, Literal, get_args
import boto3
import awswrangler.pandas as pd
from awswrangler import _databases as _db_utils
from awswrangler import _utils, exceptions, s3
from awswrangler._config import apply_configs
from ._connect import _validate_connection
from ._utils import (
_add_new_table_columns,
_create_table,
_does_table_exist,
_get_rsh_columns_types,
_make_s3_auth_string,
_upsert,
)
if TYPE_CHECKING:
try:
import redshift_connector
except ImportError:
pass
else:
redshift_connector = _utils.import_optional_dependency("redshift_connector")
_logger: logging.Logger = logging.getLogger(__name__)
_ToSqlModeLiteral = Literal["append", "overwrite", "upsert"]
_ToSqlOverwriteModeLiteral = Literal["drop", "cascade", "truncate", "delete"]
_ToSqlDistStyleLiteral = Literal["AUTO", "EVEN", "ALL", "KEY"]
_ToSqlSortStyleLiteral = Literal["COMPOUND", "INTERLEAVED"]
_CopyFromFilesDataFormatLiteral = Literal["parquet", "orc", "csv"]
def _copy(
cursor: "redshift_connector.Cursor",
path: str,
table: str,
serialize_to_json: bool,
data_format: _CopyFromFilesDataFormatLiteral = "parquet",
iam_role: str | None = None,
aws_access_key_id: str | None = None,
aws_secret_access_key: str | None = None,
aws_session_token: str | None = None,
boto3_session: boto3.Session | None = None,
schema: str | None = None,
manifest: bool | None = False,
sql_copy_extra_params: list[str] | None = None,
column_names: list[str] | None = None,
) -> None:
if schema is None:
table_name: str = f'"{table}"'
else:
table_name = f'"{schema}"."{table}"'
if data_format not in ["parquet", "orc"] and serialize_to_json:
raise exceptions.InvalidArgumentCombination(
"You can only use SERIALIZETOJSON with data_format='parquet' or 'orc'."
)
auth_str: str = _make_s3_auth_string(
iam_role=iam_role,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
aws_session_token=aws_session_token,
boto3_session=boto3_session,
)
ser_json_str: str = " SERIALIZETOJSON" if serialize_to_json else ""
column_names_str: str = f"({','.join(column_names)})" if column_names else ""
sql = (
f"COPY {table_name} {column_names_str}\nFROM '{path}' {auth_str}\nFORMAT AS {data_format.upper()}{ser_json_str}"
)
if manifest:
sql += "\nMANIFEST"
if sql_copy_extra_params:
for param in sql_copy_extra_params:
sql += f"\n{param}"
cursor.execute(sql)
@_utils.check_optional_dependency(redshift_connector, "redshift_connector")
@apply_configs
def to_sql(
df: pd.DataFrame,
con: "redshift_connector.Connection",
table: str,
schema: str,
mode: _ToSqlModeLiteral = "append",
overwrite_method: _ToSqlOverwriteModeLiteral = "drop",
index: bool = False,
dtype: dict[str, str] | None = None,
diststyle: _ToSqlDistStyleLiteral = "AUTO",
distkey: str | None = None,
sortstyle: _ToSqlSortStyleLiteral = "COMPOUND",
sortkey: list[str] | None = None,
primary_keys: list[str] | None = None,
varchar_lengths_default: int = 256,
varchar_lengths: dict[str, int] | None = None,
use_column_names: bool = False,
lock: bool = False,
chunksize: int = 200,
commit_transaction: bool = True,
precombine_key: str | None = None,
add_new_columns: bool = False,
) -> None:
"""Write records stored in a DataFrame into Redshift.
Note
----
For large DataFrames (1K+ rows) consider the function **wr.redshift.copy()**.
Parameters
----------
df
Pandas DataFrame https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html
con
Use redshift_connector.connect() to use "
"credentials directly or wr.redshift.connect() to fetch it from the Glue Catalog.
table
Table name
schema
Schema name
mode
Append, overwrite or upsert.
overwrite_method
Drop, cascade, truncate, or delete. Only applicable in overwrite mode.
- "drop" - ``DROP ... RESTRICT`` - drops the table. Fails if there are any views that depend on it.
- "cascade" - ``DROP ... CASCADE`` - drops the table, and all views that depend on it.
- "truncate" - ``TRUNCATE ...`` - truncates the table, but immediately commits current transaction &
starts a new one, hence the overwrite happens in two transactions and is not atomic.
- "delete" - ``DELETE FROM ...`` - deletes all rows from the table. Slow relative to the other methods.
index
True to store the DataFrame index as a column in the table,
otherwise False to ignore it.
dtype
Dictionary of columns names and Redshift types to be casted.
Useful when you have columns with undetermined or mixed data types.
(e.g. {'col name': 'VARCHAR(10)', 'col2 name': 'FLOAT'})
diststyle
Redshift distribution styles. Must be in ["AUTO", "EVEN", "ALL", "KEY"].
https://docs.aws.amazon.com/redshift/latest/dg/t_Distributing_data.html
distkey
Specifies a column name or positional number for the distribution key.
sortstyle
Sorting can be "COMPOUND" or "INTERLEAVED".
https://docs.aws.amazon.com/redshift/latest/dg/t_Sorting_data.html
sortkey
List of columns to be sorted.
primary_keys
Primary keys.
varchar_lengths_default
The size that will be set for all VARCHAR columns not specified with varchar_lengths.
varchar_lengths
Dict of VARCHAR length by columns. (e.g. {"col1": 10, "col5": 200}).
use_column_names
If set to True, will use the column names of the DataFrame for generating the INSERT SQL Query.
E.g. If the DataFrame has two columns `col1` and `col3` and `use_column_names` is True, data will only be
inserted into the database columns `col1` and `col3`.
lock
True to execute LOCK command inside the transaction to force serializable isolation.
chunksize
Number of rows which are inserted with each SQL query. Defaults to inserting 200 rows per query.
commit_transaction
Whether to commit the transaction. True by default.
precombine_key
When there is a primary_key match during upsert, this column will change the upsert method,
comparing the values of the specified column from source and target, and keeping the
larger of the two. Will only work when mode = upsert.
add_new_columns
If True, it automatically adds the new DataFrame columns into the target table.
Examples
--------
Writing to Redshift using a Glue Catalog Connections
>>> import awswrangler as wr
>>> with wr.redshift.connect("MY_GLUE_CONNECTION") as con:
... wr.redshift.to_sql(
... df=df,
... table="my_table",
... schema="public",
... con=con,
... )
"""
if df.empty is True:
raise exceptions.EmptyDataFrame("DataFrame cannot be empty.")
_validate_connection(con=con)
autocommit_temp: bool = con.autocommit
con.autocommit = False
try:
with con.cursor() as cursor:
if add_new_columns and _does_table_exist(cursor=cursor, schema=schema, table=table):
redshift_columns_types = _get_rsh_columns_types(
df=df,
path=None,
index=index,
dtype=dtype,
varchar_lengths_default=varchar_lengths_default,
varchar_lengths=varchar_lengths,
)
_add_new_table_columns(
cursor=cursor, schema=schema, table=table, redshift_columns_types=redshift_columns_types
)
created_table, created_schema = _create_table(
df=df,
path=None,
con=con,
cursor=cursor,
table=table,
schema=schema,
mode=mode,
overwrite_method=overwrite_method,
index=index,
dtype=dtype,
diststyle=diststyle,
sortstyle=sortstyle,
distkey=distkey,
sortkey=sortkey,
primary_keys=primary_keys,
varchar_lengths_default=varchar_lengths_default,
varchar_lengths=varchar_lengths,
lock=lock,
)
if index:
df.reset_index(level=df.index.names, inplace=True)
column_names = [f'"{column}"' for column in df.columns]
column_placeholders: str = ", ".join(["%s"] * len(column_names))
schema_str = f'"{created_schema}".' if created_schema else ""
insertion_columns = ""
if use_column_names:
insertion_columns = f"({', '.join(column_names)})"
placeholder_parameter_pair_generator = _db_utils.generate_placeholder_parameter_pairs(
df=df, column_placeholders=column_placeholders, chunksize=chunksize
)
for placeholders, parameters in placeholder_parameter_pair_generator:
sql: str = f'INSERT INTO {schema_str}"{created_table}" {insertion_columns} VALUES {placeholders}'
_logger.debug("Executing insert query:\n%s", sql)
cursor.executemany(sql, (parameters,))
if table != created_table: # upsert
_upsert(
cursor=cursor,
schema=schema,
table=table,
temp_table=created_table,
primary_keys=primary_keys,
precombine_key=precombine_key,
column_names=column_names,
)
if commit_transaction:
con.commit()
except Exception as ex:
con.rollback()
_logger.error(ex)
raise
finally:
con.autocommit = autocommit_temp
@_utils.check_optional_dependency(redshift_connector, "redshift_connector")
def copy_from_files( # noqa: PLR0913
path: str,
con: "redshift_connector.Connection",
table: str,
schema: str,
iam_role: str | None = None,
aws_access_key_id: str | None = None,
aws_secret_access_key: str | None = None,
aws_session_token: str | None = None,
data_format: _CopyFromFilesDataFormatLiteral = "parquet",
redshift_column_types: dict[str, str] | None = None,
parquet_infer_sampling: float = 1.0,
mode: _ToSqlModeLiteral = "append",
overwrite_method: _ToSqlOverwriteModeLiteral = "drop",
diststyle: _ToSqlDistStyleLiteral = "AUTO",
distkey: str | None = None,
sortstyle: _ToSqlSortStyleLiteral = "COMPOUND",
sortkey: list[str] | None = None,
primary_keys: list[str] | None = None,
varchar_lengths_default: int = 256,
varchar_lengths: dict[str, int] | None = None,
serialize_to_json: bool = False,
path_suffix: str | None = None,
path_ignore_suffix: str | list[str] | None = None,
use_threads: bool | int = True,
lock: bool = False,
commit_transaction: bool = True,
manifest: bool | None = False,
sql_copy_extra_params: list[str] | None = None,
boto3_session: boto3.Session | None = None,
s3_additional_kwargs: dict[str, str] | None = None,
precombine_key: str | None = None,
column_names: list[str] | None = None,
add_new_columns: bool = False,
) -> None:
"""Load files from S3 to a Table on Amazon Redshift (Through COPY command).
https://docs.aws.amazon.com/redshift/latest/dg/r_COPY.html
Note
----
If the table does not exist yet,
it will be automatically created for you
using the Parquet/ORC/CSV metadata to
infer the columns data types.
If the data is in the CSV format,
the Redshift column types need to be
specified manually using ``redshift_column_types``.
Note
----
In case of `use_threads=True` the number of threads
that will be spawned will be gotten from os.cpu_count().
Parameters
----------
path
S3 prefix (e.g. s3://bucket/prefix/)
con
Use redshift_connector.connect() to use "
"credentials directly or wr.redshift.connect() to fetch it from the Glue Catalog.
table
Table name
schema
Schema name
iam_role
AWS IAM role with the related permissions.
aws_access_key_id
The access key for your AWS account.
aws_secret_access_key
The secret key for your AWS account.
aws_session_token
The session key for your AWS account. This is only needed when you are using temporary credentials.
data_format
Data format to be loaded.
Supported values are Parquet, ORC, and CSV.
Default is Parquet.
redshift_column_types
Dictionary with keys as column names and values as Redshift column types.
Only used when ``data_format`` is CSV.
e.g. ```{'col1': 'BIGINT', 'col2': 'VARCHAR(256)'}```
parquet_infer_sampling
Random sample ratio of files that will have the metadata inspected.
Must be `0.0 < sampling <= 1.0`.
The higher, the more accurate.
The lower, the faster.
mode
Append, overwrite or upsert.
overwrite_method
Drop, cascade, truncate, or delete. Only applicable in overwrite mode.
"drop" - ``DROP ... RESTRICT`` - drops the table. Fails if there are any views that depend on it.
"cascade" - ``DROP ... CASCADE`` - drops the table, and all views that depend on it.
"truncate" - ``TRUNCATE ...`` - truncates the table, but immediately commits current
transaction & starts a new one, hence the overwrite happens in two transactions and is not atomic.
"delete" - ``DELETE FROM ...`` - deletes all rows from the table. Slow relative to the other methods.
diststyle
Redshift distribution styles. Must be in ["AUTO", "EVEN", "ALL", "KEY"].
https://docs.aws.amazon.com/redshift/latest/dg/t_Distributing_data.html
distkey
Specifies a column name or positional number for the distribution key.
sortstyle
Sorting can be "COMPOUND" or "INTERLEAVED".
https://docs.aws.amazon.com/redshift/latest/dg/t_Sorting_data.html
sortkey
List of columns to be sorted.
primary_keys
Primary keys.
varchar_lengths_default
The size that will be set for all VARCHAR columns not specified with varchar_lengths.
varchar_lengths
Dict of VARCHAR length by columns. (e.g. {"col1": 10, "col5": 200}).
serialize_to_json
Should awswrangler add SERIALIZETOJSON parameter into the COPY command?
SERIALIZETOJSON is necessary to load nested data
https://docs.aws.amazon.com/redshift/latest/dg/ingest-super.html#copy_json
path_suffix
Suffix or List of suffixes to be scanned on s3 for the schema extraction
(e.g. [".gz.parquet", ".snappy.parquet"]).
Only has effect during the table creation.
If None, will try to read all files. (default)
path_ignore_suffix
Suffix or List of suffixes for S3 keys to be ignored during the schema extraction.
(e.g. [".csv", "_SUCCESS"]).
Only has effect during the table creation.
If None, will try to read all files. (default)
use_threads
True to enable concurrent requests, False to disable multiple threads.
If enabled os.cpu_count() will be used as the max number of threads.
If integer is provided, specified number is used.
lock
True to execute LOCK command inside the transaction to force serializable isolation.
commit_transaction
Whether to commit the transaction. True by default.
manifest
If set to true path argument accepts a S3 uri to a manifest file.
sql_copy_extra_params
Additional copy parameters to pass to the command. For example: ["STATUPDATE ON"]
boto3_session
The default boto3 session will be used if **boto3_session** is ``None``.
s3_additional_kwargs
Forwarded to botocore requests.
e.g. s3_additional_kwargs={'ServerSideEncryption': 'aws:kms', 'SSEKMSKeyId': 'YOUR_KMS_KEY_ARN'}
precombine_key
When there is a primary_key match during upsert, this column will change the upsert method,
comparing the values of the specified column from source and target, and keeping the
larger of the two. Will only work when mode = upsert.
column_names
List of column names to map source data fields to the target columns.
add_new_columns
If True, it automatically adds the new DataFrame columns into the target table.
Examples
--------
>>> import awswrangler as wr
>>> with wr.redshift.connect("MY_GLUE_CONNECTION") as con:
... wr.redshift.copy_from_files(
... path="s3://bucket/my_parquet_files/",
... con=con,
... table="my_table",
... schema="public",
... iam_role="arn:aws:iam::XXX:role/XXX"
... )
"""
_logger.debug("Copying objects from S3 path: %s", path)
data_format = data_format.lower() # type: ignore[assignment]
if data_format not in get_args(_CopyFromFilesDataFormatLiteral):
raise exceptions.InvalidArgumentValue(f"The specified data_format {data_format} is not supported.")
autocommit_temp: bool = con.autocommit
con.autocommit = False
try:
with con.cursor() as cursor:
if add_new_columns and _does_table_exist(cursor=cursor, schema=schema, table=table):
redshift_columns_types = _get_rsh_columns_types(
df=None,
path=path,
index=False,
dtype=None,
varchar_lengths_default=varchar_lengths_default,
varchar_lengths=varchar_lengths,
parquet_infer_sampling=parquet_infer_sampling,
path_suffix=path_suffix,
path_ignore_suffix=path_ignore_suffix,
use_threads=use_threads,
boto3_session=boto3_session,
s3_additional_kwargs=s3_additional_kwargs,
data_format=data_format,
redshift_column_types=redshift_column_types,
manifest=manifest,
)
_add_new_table_columns(
cursor=cursor, schema=schema, table=table, redshift_columns_types=redshift_columns_types
)
created_table, created_schema = _create_table(
df=None,
path=path,
data_format=data_format,
parquet_infer_sampling=parquet_infer_sampling,
path_suffix=path_suffix,
path_ignore_suffix=path_ignore_suffix,
con=con,
cursor=cursor,
table=table,
schema=schema,
mode=mode,
overwrite_method=overwrite_method,
redshift_column_types=redshift_column_types,
diststyle=diststyle,
sortstyle=sortstyle,
distkey=distkey,
sortkey=sortkey,
primary_keys=primary_keys,
varchar_lengths_default=varchar_lengths_default,
varchar_lengths=varchar_lengths,
index=False,
dtype=None,
manifest=manifest,
use_threads=use_threads,
boto3_session=boto3_session,
s3_additional_kwargs=s3_additional_kwargs,
lock=lock,
)
_copy(
cursor=cursor,
path=path,
table=created_table,
schema=created_schema,
iam_role=iam_role,
data_format=data_format,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
aws_session_token=aws_session_token,
boto3_session=boto3_session,
serialize_to_json=serialize_to_json,
sql_copy_extra_params=sql_copy_extra_params,
manifest=manifest,
column_names=column_names,
)
if table != created_table: # upsert
_upsert(
cursor=cursor,
schema=schema,
table=table,
temp_table=created_table,
primary_keys=primary_keys,
precombine_key=precombine_key,
column_names=column_names,
)
if commit_transaction:
con.commit()
except Exception as ex:
con.rollback()
_logger.error(ex)
raise
finally:
con.autocommit = autocommit_temp
@_utils.validate_distributed_kwargs(
unsupported_kwargs=["boto3_session", "s3_additional_kwargs"],
)
@_utils.check_optional_dependency(redshift_connector, "redshift_connector")
def copy( # noqa: PLR0913
df: pd.DataFrame,
path: str,
con: "redshift_connector.Connection",
table: str,
schema: str,
iam_role: str | None = None,
aws_access_key_id: str | None = None,
aws_secret_access_key: str | None = None,
aws_session_token: str | None = None,
index: bool = False,
dtype: dict[str, str] | None = None,
mode: _ToSqlModeLiteral = "append",
overwrite_method: _ToSqlOverwriteModeLiteral = "drop",
diststyle: _ToSqlDistStyleLiteral = "AUTO",
distkey: str | None = None,
sortstyle: _ToSqlSortStyleLiteral = "COMPOUND",
sortkey: list[str] | None = None,
primary_keys: list[str] | None = None,
varchar_lengths_default: int = 256,
varchar_lengths: dict[str, int] | None = None,
serialize_to_json: bool = False,
keep_files: bool = False,
use_threads: bool | int = True,
lock: bool = False,
commit_transaction: bool = True,
sql_copy_extra_params: list[str] | None = None,
boto3_session: boto3.Session | None = None,
s3_additional_kwargs: dict[str, str] | None = None,
max_rows_by_file: int | None = 10_000_000,
precombine_key: str | None = None,
use_column_names: bool = False,
add_new_columns: bool = False,
) -> None:
"""Load Pandas DataFrame as a Table on Amazon Redshift using parquet files on S3 as stage.
This is a **HIGH** latency and **HIGH** throughput alternative to `wr.redshift.to_sql()` to load large
DataFrames into Amazon Redshift through the ** SQL COPY command**.
This strategy has more overhead and requires more IAM privileges
than the regular `wr.redshift.to_sql()` function, so it is only recommended
to inserting +1K rows at once.
https://docs.aws.amazon.com/redshift/latest/dg/r_COPY.html
Note
----
If the table does not exist yet,
it will be automatically created for you
using the Parquet metadata to
infer the columns data types.
Note
----
In case of `use_threads=True` the number of threads
that will be spawned will be gotten from os.cpu_count().
Parameters
----------
df
Pandas DataFrame.
path
S3 path to write stage files (e.g. s3://bucket_name/any_name/).
Note: This path must be empty.
con
Use redshift_connector.connect() to use "
"credentials directly or wr.redshift.connect() to fetch it from the Glue Catalog.
table
Table name
schema
Schema name
iam_role
AWS IAM role with the related permissions.
aws_access_key_id
The access key for your AWS account.
aws_secret_access_key
The secret key for your AWS account.
aws_session_token
The session key for your AWS account. This is only needed when you are using temporary credentials.
index
True to store the DataFrame index in file, otherwise False to ignore it.
dtype
Dictionary of columns names and Athena/Glue types to be casted.
Useful when you have columns with undetermined or mixed data types.
Only takes effect if dataset=True.
(e.g. {'col name': 'bigint', 'col2 name': 'int'})
mode
Append, overwrite or upsert.
overwrite_method
Drop, cascade, truncate, or delete. Only applicable in overwrite mode.
"drop" - ``DROP ... RESTRICT`` - drops the table. Fails if there are any views that depend on it.
"cascade" - ``DROP ... CASCADE`` - drops the table, and all views that depend on it.
"truncate" - ``TRUNCATE ...`` - truncates the table, but immediately commits current
transaction & starts a new one, hence the overwrite happens in two transactions and is not atomic.
"delete" - ``DELETE FROM ...`` - deletes all rows from the table. Slow relative to the other methods.
diststyle
Redshift distribution styles. Must be in ["AUTO", "EVEN", "ALL", "KEY"].
https://docs.aws.amazon.com/redshift/latest/dg/t_Distributing_data.html
distkey
Specifies a column name or positional number for the distribution key.
sortstyle
Sorting can be "COMPOUND" or "INTERLEAVED".
https://docs.aws.amazon.com/redshift/latest/dg/t_Sorting_data.html
sortkey
List of columns to be sorted.
primary_keys
Primary keys.
varchar_lengths_default
The size that will be set for all VARCHAR columns not specified with varchar_lengths.
varchar_lengths
Dict of VARCHAR length by columns. (e.g. {"col1": 10, "col5": 200}).
serialize_to_json
Should awswrangler add SERIALIZETOJSON parameter into the COPY command?
SERIALIZETOJSON is necessary to load nested data
https://docs.aws.amazon.com/redshift/latest/dg/ingest-super.html#copy_json
keep_files
Should keep stage files?
use_threads
True to enable concurrent requests, False to disable multiple threads.
If enabled os.cpu_count() will be used as the max number of threads.
If integer is provided, specified number is used.
lock
True to execute LOCK command inside the transaction to force serializable isolation.
commit_transaction
Whether to commit the transaction. True by default.
sql_copy_extra_params
Additional copy parameters to pass to the command. For example: ["STATUPDATE ON"]
boto3_session
The default boto3 session will be used if **boto3_session** is ``None``.
s3_additional_kwargs
Forwarded to botocore requests.
e.g. s3_additional_kwargs={'ServerSideEncryption': 'aws:kms', 'SSEKMSKeyId': 'YOUR_KMS_KEY_ARN'}
max_rows_by_file
Max number of rows in each file.
(e.g. 33554432, 268435456)
precombine_key
When there is a primary_key match during upsert, this column will change the upsert method,
comparing the values of the specified column from source and target, and keeping the
larger of the two. Will only work when mode = upsert.
use_column_names
If set to True, will use the column names of the DataFrame for generating the INSERT SQL Query.
E.g. If the DataFrame has two columns `col1` and `col3` and `use_column_names` is True, data will only be
inserted into the database columns `col1` and `col3`.
add_new_columns
If True, it automatically adds the new DataFrame columns into the target table.
Examples
--------
>>> import awswrangler as wr
>>> import pandas as pd
>>> with wr.redshift.connect("MY_GLUE_CONNECTION") as con:
... wr.redshift.copy(
... df=pd.DataFrame({'col': [1, 2, 3]}),
... path="s3://bucket/my_parquet_files/",
... con=con,
... table="my_table",
... schema="public",
... iam_role="arn:aws:iam::XXX:role/XXX",
... )
"""
path = path[:-1] if path.endswith("*") else path
path = path if path.endswith("/") else f"{path}/"
column_names = [f'"{column}"' for column in df.columns] if use_column_names else []
if s3.list_objects(path=path, boto3_session=boto3_session, s3_additional_kwargs=s3_additional_kwargs):
raise exceptions.InvalidArgument(
f"The received S3 path ({path}) is not empty. "
"Please, provide a different path or use wr.s3.delete_objects() to clean up the current one."
)
try:
s3.to_parquet(
df=df,
path=path,
index=index,
dataset=True,
mode="append",
dtype=dtype,
use_threads=use_threads,
boto3_session=boto3_session,
s3_additional_kwargs=s3_additional_kwargs,
max_rows_by_file=max_rows_by_file,
)
copy_from_files(
path=path,
con=con,
table=table,
schema=schema,
iam_role=iam_role,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
aws_session_token=aws_session_token,
mode=mode,
overwrite_method=overwrite_method,
diststyle=diststyle,
distkey=distkey,
sortstyle=sortstyle,
sortkey=sortkey,
primary_keys=primary_keys,
varchar_lengths_default=varchar_lengths_default,
varchar_lengths=varchar_lengths,
serialize_to_json=serialize_to_json,
use_threads=use_threads,
lock=lock,
commit_transaction=commit_transaction,
boto3_session=boto3_session,
s3_additional_kwargs=s3_additional_kwargs,
sql_copy_extra_params=sql_copy_extra_params,
precombine_key=precombine_key,
column_names=column_names,
add_new_columns=add_new_columns,
)
finally:
if keep_files is False:
_logger.debug("Deleting objects in S3 path: %s", path)
s3.delete_objects(
path=path,
use_threads=use_threads,
boto3_session=boto3_session,
s3_additional_kwargs=s3_additional_kwargs,
)