Vertica分析数据库的官方本土Python客户端.
vertica-python is a native Python client for the Vertica (http://www.vertica.com) database. vertica-python is the replacement of the deprecated Python client vertica_db_client, which was removed since Vertica server version 9.3.
:loudspeaker: 08/14/2018: vertica-python becomes Vertica’s first officially supported open source database client, see the blog here.
Please check out release notes to learn about the latest improvements.
vertica-python has been tested with Vertica 24.4.0 and Python 3.8/3.9/3.10/3.11/3.12/3.13. Feel free to submit issues and/or pull requests (Read up on our contributing guidelines).
To install vertica-python with pip:
# Latest release version
pip install vertica-python
# Latest commit on master branch
pip install git+https://github.com/vertica/vertica-python.git@masterTo install vertica-python from source, run the following command from the root directory:
python setup.py installSource code for vertica-python can be found at:
https://github.com/vertica/vertica-pythonvertica-python has optional Kerberos authentication support for Unix-like systems, which requires you to install the kerberos package:
pip install kerberosNote that kerberos is a python extension module, which means you need to install python-dev. The command depends on the package manager and will look like
sudo [yum|apt-get|etc] install python-devThen see this section for how to config Kerberos for a connection.
:scroll: The basic vertica-python usage is common to all the database adapters implementing the DB-API v2.0 protocol.
The example below shows how to create a Connection object:
…| Connection Option | Description |
|---|---|
| host | The server host of the connection. This can be a host name or an IP address. |
| Default: "localhost" | |
| port | The port of the connection. |
| Default: 5433 | |
| user | The database user name to use to connect to the database. |
| Default: | |
| (for non-OAuth connections) OS login user name | |
| (for OAuth connections) "" | |
| password | The password to use to log into the database. |
| Default: "" | |
| database | The database name. |
| Default: "" | |
| autocommit | See Autocommit. |
| Default: False | |
| backup_server_node | See Connection Failover. |
| Default: [] | |
| binary_transfer | See Data Transfer Format. |
| Default: False (use text format transfer) | |
| connection_load_balance | See Connection Load Balancing. |
| Default: False (disabled) | |
| connection_timeout | The number of seconds (can be a nonnegative floating point number) the client waits for a socket operation (Establishing a TCP connection or read/write operation). |
| Default: None (no timeout) | |
| disable_copy_local | See COPY FROM LOCAL. |
| Default: False | |
| kerberos_host_name | See Kerberos Authentication. |
Default: the value of connection option host |
|
| kerberos_service_name | See Kerberos Authentication. |
| Default: "vertica" | |
| log_level | See Logging. |
| log_path | See Logging. |
| oauth_access_token | See OAuth Authentication. |
| Default: "" | |
| request_complex_types | See SQL Data conversion to Python objects. |
| Default: True | |
| session_label | Sets a label for the connection on the server. This value appears in the client_label column of the v_monitor.sessions system table. |
Default: an auto-generated label with format of vertica-python-{version}-{random_uuid} |
|
| ssl | See TLS/SSL. |
| Default: None (tlsmode="prefer") | |
| tlsmode | Controls whether the connection to the server uses TLS encryption. |
| See TLS/SSL. | |
| Default: "prefer" | |
| tls_cafile | The name of a file containing trusted SSL certificate authority (CA) certificate(s). |
| See TLS/SSL. | |
| tls_certfile | The name of a file containing client's certificate(s). |
| See TLS/SSL. | |
| tls_keyfile | The name of a file containing client's private key. |
| See TLS/SSL. | |
| unicode_error | See UTF-8 encoding issues. |
| Default: 'strict' (throw error on invalid UTF-8 results) | |
| use_prepared_statements | See Passing parameters to SQL queries. |
| Default: False | |
| workload | Sets the workload name associated with this session. Valid values are workload names that already exist in a workload routing rule on the server. If a workload name that doesn't exist is entered, the server will reject it and it will be set to the default. |
| Default: "" | |
| dsn | See Set Properties with Connection String. |
Below are a few important connection topics you may deal with, or you can skip and jump to the next section: Send Queries and Retrieve Results
Another way to set connection properties is passing a connection string to the keyword parameter dsn of vertica_python.connect(dsn='...', **kwargs). The connection string is of the form:
vertica://(user):(password)@(host):(port)/(database)?(arg1=val1&arg2=val2&...)The connection string would be parsed by vertica_python.parse_dsn(connection_str), and the parsing result (a dictionary of keywords and values) would be merged with kwargs. If the same keyword is specified in both the sources, the kwargs value overrides the parsed dsn value. The (arg1=val1&arg2=val2&...) section can handle string/numeric/boolean values, blank and invalid value would be ignored.
…There are two options to control client-server TLS: tlsmode and ssl. If both are set, tlsmode takes precedence.
ssl can be a bool or a ssl.SSLContext object. Here is the value mapping between ssl (exclude ssl.SSLContext) and tlsmode:
tlsmode |
ssl |
Description |
|---|---|---|
| 'disable' | False | only try a non-TLS connection. |
| 'prefer' | (not set) | (Default) first try a TLS connection; if TLS is disabled on the server, then fallback to a non-TLS connection. |
| Note: If TLS is enabled on the server and TLS connection fails, the client rejects the connection. | ||
| 'require' | True | connects using TLS without verifying certificates. If the TLS connection attempt fails, the client rejects the connection. |
| 'verify-ca' | connects using TLS and confirms that the server certificate has been signed by a trusted certificate authority. | |
| 'verify-full' | connects using TLS, confirms that the server certificate has been signed by a trusted certificate authority, and verifies that the host name matches the name provided in the server certificate. |
When tlsmode is 'verify-ca' or 'verify-full', these options take certificate/key files: tls_cafile, tls_certfile and tls_keyfile. Otherwise, these options are ignored.
tlsmode example:
# [TLSMode: require]
import vertica_python
conn_info = {'host': '127.0.0.1',
'user': 'some_user',
'database': 'a_database',
'tlsmode': 'require'}
connection = vertica_python.connect(**conn_info)# [TLSMode: verify-ca]
import vertica_python
conn_info = {'host': '127.0.0.1',
'user': 'some_user',
'database': 'a_database',
'tlsmode': 'verify-ca',
'tls_cafile': '/path/to/ca_file.pem' # CA certificate used to verify server certificate
}
connection = vertica_python.connect(**conn_info)# [TLSMode: verify-full] + Mutual Mode
import vertica_python
conn_info = {'host': '127.0.0.1',
'user': 'some_user',
'database': 'a_database',
'tlsmode': 'verify-full',
'tls_cafile' = '/path/to/ca_file.pem' # CA certificate used to verify server certificate
'tls_certfile' = '/path/to/client.pem', # (for mutual mode) client certificate
'tls_keyfile' = '/path/to/client.key' # (for mutual mode) client private key
}
connection = vertica_python.connect(**conn_info)You can pass an ssl.SSLContext object to ssl to customize the underlying SSL connection options. See more on SSL options here.
Server mode TLS examples:
…Mutual mode TLS example:
…In order to use Kerberos authentication, install dependencies first, and it is the user's responsibility to ensure that an Ticket-Granting Ticket (TGT) is available and valid. Whether a TGT is available can be easily determined by running the klist command. If no TGT is available, then it first must be obtained by running the kinit command or by logging in. You can pass in optional arguments to customize the authentication. The arguments are kerberos_service_name, which defaults to "vertica", and kerberos_host_name, which defaults to the value of argument host. For example,
import vertica_python
conn_info = {'host': '127.0.0.1',
'port': 5433,
'user': 'some_user',
'password': 'some_password',
'database': 'a_database',
# The service name portion of the Vertica Kerberos principal
'kerberos_service_name': 'vertica_krb',
# The instance or host name portion of the Vertica Kerberos principal
'kerberos_host_name': 'vcluster.example.com'}
with vertica_python.connect(**conn_info) as conn:
# do thingsTo authenticate via OAuth, provide an oauth_access_token that authorizes a user to the database.
import vertica_python
conn_info = {'host': '127.0.0.1',
'port': 5433,
'database': 'a_database',
# valid OAuth access token
'oauth_access_token': 'xxxxxx'}
with vertica_python.connect(**conn_info) as conn:
# do thingsLogging is disabled by default if neither log_level or log_path are set. Passing value to at least one of those options to enable logging.
When logging is enabled, the default value of log_level is logging.WARNING. You can find all levels here. And the default value of log_path is 'vertica_python.log', the log file will be in the current execution directory. If log_path is set to '' (empty string) or None, no file handler is set, logs will be processed by root handlers. For example,
…Supply a list of backup hosts to backup_server_node for the client to try if the primary host you specify in the connection parameters (host, port) is unreachable. Each item in the list should be either a host string (using default port 5433) or a (host, port) tuple. A host can be a host name or an IP address.
import vertica_python
conn_info = {'host': 'unreachable.server.com',
'port': 888,
'user': 'some_user',
'password': 'some_password',
'database': 'a_database',
'backup_server_node': ['12暂无开放 Issues,或尚未同步最近议题。