Skip to content

Commit

Permalink
Fix #43: Support COPY FROM LOCAL (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
sitingren authored Jul 14, 2020
1 parent 0bf74b3 commit d98179f
Show file tree
Hide file tree
Showing 17 changed files with 981 additions and 67 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,9 @@ connection.commit()

**Copy** :

There are 2 methods to do copy:

Method 1: "COPY FROM STDIN" sql with Cursor.copy()
```python
cur = connection.cursor()
cur.copy("COPY test_copy (id, name) from stdin DELIMITER ',' ", csv)
Expand All @@ -450,6 +453,52 @@ with open("/tmp/binary_file.csv", "rb") as fs:
fs, buffer_size=65536)
```

Method 2: "COPY FROM LOCAL" sql with Cursor.execute()

```python
import sys
import vertica_python

conn_info = {'host': '127.0.0.1',
'user': 'some_user',
'password': 'some_password',
'database': 'a_database',
# False by default
#'disable_copy_local': True,
# Don't support executing COPY LOCAL operations with prepared statements
'use_prepared_statements': False
}

with vertica_python.connect(**conn_info) as connection:
cur = connection.cursor()

# Copy from local file
cur.execute("COPY table(field1, field2) FROM LOCAL"
" 'data_Jan_*.csv','data_Feb_01.csv' DELIMITER ','"
" REJECTED DATA 'path/to/write/rejects.txt'"
" EXCEPTIONS 'path/to/write/exceptions.txt'",
buffer_size=65536
)
print("Rows loaded:", cur.fetchall())

# Copy from local stdin
cur.execute("COPY table(field1, field2) FROM LOCAL STDIN DELIMITER ','", copy_stdin=sys.stdin)
print("Rows loaded:", cur.fetchall())

# Copy from local stdin (compound statements)
with open('f1.csv', 'r') as fs1, open('f2.csv', 'r') as fs2:
cur.execute("COPY tlb1(field1, field2) FROM LOCAL STDIN DELIMITER ',';"
"COPY tlb2(field1, field2) FROM LOCAL STDIN DELIMITER ',';",
copy_stdin=[fs1, fs2], buffer_size=65536)
print("Rows loaded 1:", cur.fetchall())
cur.nextset()
print("Rows loaded 2:", cur.fetchall())
```
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()`.

The data for copying from/writing to local files is streamed in chunks of `buffer_size` bytes, which defaults to 128 * 2 ** 10.

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).

**Cancel the current database operation** :

Expand Down
63 changes: 63 additions & 0 deletions vertica_python/os_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright (c) 2020 Micro Focus or one of its affiliates.
#
# Licensed 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.


from __future__ import print_function, division, absolute_import

import errno
import os


def ensure_dir_exists(filepath):
"""Ensure that a directory exists
If it doesn't exist, try to create it and protect against a race condition
if another process is doing the same.
"""
directory = os.path.dirname(filepath)
if directory != '' and not os.path.exists(directory):
try:
os.makedirs(directory)
except OSError as e:
if e.errno != errno.EEXIST:
raise

def check_file_readable(filename):
"""Ensure this is a readable file"""
if not os.path.exists(filename):
raise OSError('{} does not exist'.format(filename))
elif not os.path.isfile(filename):
raise OSError('{} is not a file'.format(filename))
elif not os.access(filename, os.R_OK):
raise OSError('{} is not readable'.format(filename))

def check_file_writable(filename):
"""Ensure this is a writable file. If the file doesn't exist,
ensure its directory is writable.
"""
if os.path.exists(filename):
if not os.path.isfile(filename):
raise OSError('{} is not a file'.format(filename))
if not os.access(filename, os.W_OK):
raise OSError('{} is not writable'.format(filename))
# If target does not exist, check permission on parent dir
ensure_dir_exists(filename)
pdir = os.path.dirname(filename)
if not pdir:
pdir = '.'
if not os.path.isdir(pdir):
raise OSError('{} is not a directory'.format(pdir))
if not os.access(pdir, os.W_OK):
raise OSError('Directory {} is not writable'.format(pdir))

Loading

0 comments on commit d98179f

Please sign in to comment.