Skip to content

Commit d98179f

Browse files
authored
Fix #43: Support COPY FROM LOCAL (#386)
1 parent 0bf74b3 commit d98179f

17 files changed

Lines changed: 981 additions & 67 deletions

File tree

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,9 @@ connection.commit()
437437

438438
**Copy** :
439439

440+
There are 2 methods to do copy:
441+
442+
Method 1: "COPY FROM STDIN" sql with Cursor.copy()
440443
```python
441444
cur = connection.cursor()
442445
cur.copy("COPY test_copy (id, name) from stdin DELIMITER ',' ", csv)
@@ -450,6 +453,52 @@ with open("/tmp/binary_file.csv", "rb") as fs:
450453
fs, buffer_size=65536)
451454
```
452455

456+
Method 2: "COPY FROM LOCAL" sql with Cursor.execute()
457+
458+
```python
459+
import sys
460+
import vertica_python
461+
462+
conn_info = {'host': '127.0.0.1',
463+
'user': 'some_user',
464+
'password': 'some_password',
465+
'database': 'a_database',
466+
# False by default
467+
#'disable_copy_local': True,
468+
# Don't support executing COPY LOCAL operations with prepared statements
469+
'use_prepared_statements': False
470+
}
471+
472+
with vertica_python.connect(**conn_info) as connection:
473+
cur = connection.cursor()
474+
475+
# Copy from local file
476+
cur.execute("COPY table(field1, field2) FROM LOCAL"
477+
" 'data_Jan_*.csv','data_Feb_01.csv' DELIMITER ','"
478+
" REJECTED DATA 'path/to/write/rejects.txt'"
479+
" EXCEPTIONS 'path/to/write/exceptions.txt'",
480+
buffer_size=65536
481+
)
482+
print("Rows loaded:", cur.fetchall())
483+
484+
# Copy from local stdin
485+
cur.execute("COPY table(field1, field2) FROM LOCAL STDIN DELIMITER ','", copy_stdin=sys.stdin)
486+
print("Rows loaded:", cur.fetchall())
487+
488+
# Copy from local stdin (compound statements)
489+
with open('f1.csv', 'r') as fs1, open('f2.csv', 'r') as fs2:
490+
cur.execute("COPY tlb1(field1, field2) FROM LOCAL STDIN DELIMITER ',';"
491+
"COPY tlb2(field1, field2) FROM LOCAL STDIN DELIMITER ',';",
492+
copy_stdin=[fs1, fs2], buffer_size=65536)
493+
print("Rows loaded 1:", cur.fetchall())
494+
cur.nextset()
495+
print("Rows loaded 2:", cur.fetchall())
496+
```
497+
When connection option `disable_copy_local` set to True, disables COPY LOCAL operations, including copying data from local files/stdin and using local files to store data and exceptions. You can use this property to prevent users from writing to and copying from files on a Vertica host, including an MC host. Note that this property doesn't apply to `Cursor.copy()`.
498+
499+
The data for copying from/writing to local files is streamed in chunks of `buffer_size` bytes, which defaults to 128 * 2 ** 10.
500+
501+
When executing "COPY FROM LOCAL STDIN", `copy_stdin` should be a file-like object or a list of file-like objects (specifically, any object with a `read()` method).
453502

454503
**Cancel the current database operation** :
455504

vertica_python/os_utils.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright (c) 2020 Micro Focus or one of its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
from __future__ import print_function, division, absolute_import
17+
18+
import errno
19+
import os
20+
21+
22+
def ensure_dir_exists(filepath):
23+
"""Ensure that a directory exists
24+
25+
If it doesn't exist, try to create it and protect against a race condition
26+
if another process is doing the same.
27+
"""
28+
directory = os.path.dirname(filepath)
29+
if directory != '' and not os.path.exists(directory):
30+
try:
31+
os.makedirs(directory)
32+
except OSError as e:
33+
if e.errno != errno.EEXIST:
34+
raise
35+
36+
def check_file_readable(filename):
37+
"""Ensure this is a readable file"""
38+
if not os.path.exists(filename):
39+
raise OSError('{} does not exist'.format(filename))
40+
elif not os.path.isfile(filename):
41+
raise OSError('{} is not a file'.format(filename))
42+
elif not os.access(filename, os.R_OK):
43+
raise OSError('{} is not readable'.format(filename))
44+
45+
def check_file_writable(filename):
46+
"""Ensure this is a writable file. If the file doesn't exist,
47+
ensure its directory is writable.
48+
"""
49+
if os.path.exists(filename):
50+
if not os.path.isfile(filename):
51+
raise OSError('{} is not a file'.format(filename))
52+
if not os.access(filename, os.W_OK):
53+
raise OSError('{} is not writable'.format(filename))
54+
# If target does not exist, check permission on parent dir
55+
ensure_dir_exists(filename)
56+
pdir = os.path.dirname(filename)
57+
if not pdir:
58+
pdir = '.'
59+
if not os.path.isdir(pdir):
60+
raise OSError('{} is not a directory'.format(pdir))
61+
if not os.access(pdir, os.W_OK):
62+
raise OSError('Directory {} is not writable'.format(pdir))
63+

0 commit comments

Comments
 (0)