### Example: Basic Table Monitoring Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table-monitor.md A comprehensive example demonstrating how to set up a GPUdbTableMonitor to track INSERT, UPDATE, and DELETE events on a table, with custom event handlers. ```APIDOC ## Example: Basic Table Monitoring ```python import gpudb import threading from queue import Queue # Setup connection db = gpudb.GPUdb(host='http://localhost:9191') # Create table columns = [ ['id', 'int'], ['name', 'string', 'char32'], ['value', 'double'] ] record_type = gpudb.GPUdbRecordType(columns) db.create_table(table_name='events', record_type=record_type) # Define event handlers def handle_insert(record): print(f"[INSERT] {record}") def handle_update(count): print(f"[UPDATE] {count} records") def handle_delete(count): print(f"[DELETE] {count} records") def handle_error(msg): print(f"[ERROR] {msg}") # Create callbacks callbacks = [ gpudb.GPUdbTableMonitor.Callback( callback_type=gpudb.GPUdbTableMonitor.Callback.Type.INSERT_DECODED, event_callback=handle_insert, error_callback=handle_error ), gpudb.GPUdbTableMonitor.Callback( callback_type=gpudb.GPUdbTableMonitor.Callback.Type.UPDATED, event_callback=handle_update, error_callback=handle_error ), gpudb.GPUdbTableMonitor.Callback( callback_type=gpudb.GPUdbTableMonitor.Callback.Type.DELETED, event_callback=handle_delete, error_callback=handle_error ) ] # Create and start monitor options = gpudb.GPUdbTableMonitor.Options() monitor = gpudb.GPUdbTableMonitor.Client( db=db, table_name='events', callback_list=callbacks, options=options ) # Run monitor in background thread monitor_thread = threading.Thread(target=monitor.start) monitor_thread.daemon = True monitor_thread.start() # Insert data while monitoring import time for i in range(5): db.insert_records( table_name='events', records={'id': i, 'name': f'Event {i}', 'value': i * 10.5} ) time.sleep(1) # Stop monitoring monitor.stop() monitor_thread.join() ``` ``` -------------------------------- ### Initialize and Start GPUdbTableMonitor Client Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table-monitor.md This snippet demonstrates how to set up callback handlers for insert and delete events, create a GPUdbTableMonitor client instance, and start the monitoring process. Ensure you have a GPUdb client instance (`db`) and a table name defined. ```python from gpudb import GPUdbTableMonitor # Define callback handlers def on_insert(record): print(f"Inserted: {record}") def on_delete(count): print(f"Deleted {count} records") def on_error(error_msg): print(f"Error: {error_msg}") # Create callbacks insert_callback = GPUdbTableMonitor.Callback( callback_type=GPUdbTableMonitor.Callback.Type.INSERT_DECODED, event_callback=on_insert, error_callback=on_error ) delete_callback = GPUdbTableMonitor.Callback( callback_type=GPUdbTableMonitor.Callback.Type.DELETED, event_callback=on_delete, error_callback=on_error ) # Create monitor client monitor = GPUdbTableMonitor.Client( db=db, table_name='my_table', callback_list=[insert_callback, delete_callback], options=None ) # Start monitoring (or stop with monitor.stop()) monitor.start() ``` -------------------------------- ### Example: Large File Upload with Progress Tracking Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-file-handler.md Shows how to upload a large file using MultipartOperation and track its progress. The example includes a loop to periodically check and print upload status until completion. ```python import gpudb from gpudb import MultipartOperation import time db = gpudb.GPUdb(host='http://localhost:9191') # Setup for large file operation = MultipartOperation( db=db, file_path='/data/large_dataset.bin', table_name='bulk_data', chunk_size=10485760 # 10MB chunks ) # Start upload try: # Upload in background or with progress tracking while not operation.is_complete(): progress = operation.get_progress() print(f"Upload progress: {progress['percent']}% ({progress['bytes_uploaded']}/{progress['total_bytes']})") time.sleep(2) result = operation.upload() print(f"Upload complete: {result}") except gpudb.GPUdbException as e: print(f"Upload failed: {e}") ``` -------------------------------- ### Complete Table Operations Example Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table.md A comprehensive example demonstrating the full lifecycle of table operations, including database connection, record type definition, table creation, and instantiation. ```python import gpudb # Create a GPUdb connection db = gpudb.GPUdb(host='http://localhost:9191') # Define table structure columns = [ ['user_id', 'int'], ['username', 'string', 'char32'], ['email', 'string', 'char64'], ['age', 'int'], ['score', 'double'] ] record_type = gpudb.GPUdbRecordType(columns) # Create table object users = gpudb.GPUdbTable( record_type=record_type, name='users', db=db ) ``` -------------------------------- ### Install Kinetica Python API Source: https://github.com/kineticadb/kinetica-api-python/blob/master/README.md Install the Kinetica Python API package from the root directory of the repository. ```bash pip3 install . ``` -------------------------------- ### GPUdbTableMonitor.Client.start() Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table-monitor.md Starts the real-time table monitoring process. This method is blocking and will continue until the monitoring is stopped. ```APIDOC ## GPUdbTableMonitor.Client.start() Starts the table monitoring process. ### Method `start()` ### Description Blocks until monitoring completes or stop() is called. ``` -------------------------------- ### Filtered Bulk Export Example Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-multihead-io.md Demonstrates exporting records from a table based on specified filter criteria using `RecordRetriever`. It shows how to get the total count of matching records and fetch them in batches. ```python import gpudb from gpudb import RecordRetriever db = gpudb.GPUdb(host='http://localhost:9191') # Export records matching criteria retriever = RecordRetriever( db=db, table_name='my_table', expression="category = 'important' AND value > 50" ) # Get all matching records total = retriever.get_count() print(f"Exporting {total} records...") # Fetch in batches batch_size = 10000 for offset in range(0, total, batch_size): batch = retriever.get_records( offset=offset, limit=batch_size ) for record in batch: # Process or save record print(f"{record['id']}: {record['value']}") ``` -------------------------------- ### Initialize GPUdbSqlContext Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-sql.md Instantiate GPUdbSqlContext with a GPUdb client instance. This is the starting point for building SQL queries programmatically. ```python from gpudb import GPUdbSqlContext context = GPUdbSqlContext(db) ``` -------------------------------- ### Aggregation Query Example Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-sql.md Shows how to build and execute an aggregation query using the query builder. ```APIDOC ## Build Aggregation Query ### Description This example demonstrates constructing an aggregation query using the `GPUdbSqlContext`, including selecting aggregated fields, filtering, ordering, and limiting the results. ### Method POST (Implicit via `db.get_records` after query build) ### Endpoint (Not explicitly defined, involves internal query building and execution) ### Parameters (Parameters are defined within the query building methods like `from_table`, `where`, `select`, `order_by`, `limit`) ### Request Example (Code example provided in source text demonstrates programmatic construction) ### Response #### Success Response (200) - **table_name** (str) - The name of the temporary table created for the aggregated results. - **records** (list) - A list of aggregated records fetched from the results table. ``` -------------------------------- ### Example: Queue-Based Monitoring Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table-monitor.md Illustrates how to use a Queue for inter-thread communication when monitoring tables, suitable for multi-threaded applications. ```APIDOC ## Example: Queue-Based Monitoring For multi-threaded applications, use a Queue to decouple the monitor from event processing: ```python import gpudb from queue import Queue import threading db = gpudb.GPUdb(host='http://localhost:9191') # Create table columns = [['id', 'int'], ['data', 'string', 'char64']] record_type = gpudb.GPUdbRecordType(columns) db.create_table(table_name='data_table', record_type=record_type) # Shared queue for inter-thread communication event_queue = Queue() # Event handlers that enqueue events def on_insert(record): event_queue.put(('insert', record)) def on_update(count): event_queue.put(('update', count)) def on_error(msg): event_queue.put(('error', msg)) # Create monitor with queue-based callbacks callbacks = [ gpudb.GPUdbTableMonitor.Callback( callback_type=gpudb.GPUdbTableMonitor.Callback.Type.INSERT_DECODED, event_callback=on_insert, error_callback=on_error ), gpudb.GPUdbTableMonitor.Callback( callback_type=gpudb.GPUdbTableMonitor.Callback.Type.UPDATED, event_callback=on_update, error_callback=on_error ) ] monitor = gpudb.GPUdbTableMonitor.Client( db=db, table_name='data_table', callback_list=callbacks ) # Run monitor in background monitor_thread = threading.Thread(target=monitor.start) monitor_thread.daemon = True monitor_thread.start() ``` ``` -------------------------------- ### GPUdb Python API Connection Setup Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb.md Demonstrates how to establish a connection to a GPUdb instance using the Python client library. Includes setting authentication credentials and connection options. ```python import gpudb from collections import OrderedDict # Setup connection options = gpudb.GPUdb.Options() options.username = "user" options.password = "password" db = gpudb.GPUdb(host='http://localhost:9191', options=options) ``` -------------------------------- ### SQL Iterator Example Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-sql.md Illustrates how to execute a raw SQL statement and iterate through the results using `GPUdbSqlIterator`. ```APIDOC ## Execute SQL and Iterate Results ### Description This example demonstrates executing a direct SQL statement and processing the results efficiently using `GPUdbSqlIterator`, which handles fetching records in batches. ### Method POST (Implicit via `GPUdbSqlIterator` execution) ### Endpoint (Not explicitly defined, involves direct SQL execution) ### Parameters - **db** (GPUdb) - Required - An initialized `GPUdb` client instance. - **sql_statement** (str) - Required - The SQL query to execute. ### Request Example (Code example provided in source text demonstrates iterator setup and usage) ### Response #### Success Response (200) - **records** (list) - A batch of records retrieved from the query. - **iterator.has_more_records()** (bool) - Returns true if more records are available. - **iterator.get_records()** (list) - Fetches the next batch of records. - **iterator.get_offset()** (int) - Returns the total number of records processed so far. ``` -------------------------------- ### Example: CSV File Import Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-file-handler.md Demonstrates a complete workflow for importing a CSV file into a GPUdb table. Includes setting up the database connection, creating a table, and executing the import using GPUdbFileHandler. ```python import gpudb from gpudb import GPUdbFileHandler, OpMode # Setup connection db = gpudb.GPUdb(host='http://localhost:9191') # Create table for import columns = [ ['id', 'int'], ['name', 'string', 'char64'], ['value', 'double'], ['timestamp', 'datetime'] ] record_type = gpudb.GPUdbRecordType(columns) db.create_table(table_name='imported_data', record_type=record_type) # Import CSV file handler = GPUdbFileHandler( db=db, file_path='/data/records.csv', table_name='imported_data', mode=OpMode.INSERT ) try: response = handler.execute() print(f"Imported {response['count']} records") except gpudb.GPUdbException as e: print(f"Import failed: {e}") ``` -------------------------------- ### Initialize FailbackOptions Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/configuration.md Instantiate FailbackOptions to configure high-availability cluster failback. This example shows setting retry, timeout, and attempt limits. ```python from gpudb import FailbackOptions fb_opts = FailbackOptions() fb_opts.retry_failback = True fb_opts.failback_timeout_seconds = 1800 # 30 minutes fb_opts.max_attempts = 3 options = GPUdb.Options() options.failback_options = fb_opts db = GPUdb(host='http://primary:9191', options=options) ``` -------------------------------- ### Kinetica Filter Expression Examples Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/types.md Provides examples of filter expressions using SQL WHERE clause syntax for querying data. ```python # SQL WHERE clause format str = "age > 18 AND status = 'active'" ``` ```python str = "date >= '2024-01-01' AND amount > 100" ``` -------------------------------- ### Query Builder Example Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-sql.md Demonstrates how to construct a complex SQL query programmatically using the GPUdb SQL query builder. ```APIDOC ## Build and Execute Complex Query ### Description This example shows how to use the `GPUdbSqlContext` to build a query with joins, filtering, selection, ordering, and limiting, then execute it and fetch results. ### Method POST (Implicit via `db.get_records` after query build) ### Endpoint (Not explicitly defined, involves internal query building and execution) ### Parameters (Parameters are defined within the query building methods like `from_table`, `join`, `where`, `select`, `order_by`, `limit`) ### Request Example (Code example provided in source text demonstrates programmatic construction) ### Response #### Success Response (200) - **table_name** (str) - The name of the temporary table created for the query results. - **records** (list) - A list of records fetched from the results table. ``` -------------------------------- ### Create Python Virtual Environment Source: https://github.com/kineticadb/kinetica-api-python/blob/master/README.md Create and activate a Python virtual environment to manage dependencies and avoid installation conflicts. ```bash python3 -m venv .venv source .venv/bin/activate ``` -------------------------------- ### Basic Table Monitoring Example Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table-monitor.md Set up a GPUdbTableMonitor client to listen for INSERT, UPDATE, and DELETE events on a table. Events are handled by specific callback functions. The monitor runs in a background thread. ```python import gpudb import threading from queue import Queue # Setup connection db = gpudb.GPUdb(host='http://localhost:9191') # Create table columns = [ ['id', 'int'], ['name', 'string', 'char32'], ['value', 'double'] ] record_type = gpudb.GPUdbRecordType(columns) db.create_table(table_name='events', record_type=record_type) # Define event handlers def handle_insert(record): print(f"[INSERT] {record}") def handle_update(count): print(f"[UPDATE] {count} records") def handle_delete(count): print(f"[DELETE] {count} records") def handle_error(msg): print(f"[ERROR] {msg}") # Create callbacks callbacks = [ gpudb.GPUdbTableMonitor.Callback( callback_type=gpudb.GPUdbTableMonitor.Callback.Type.INSERT_DECODED, event_callback=handle_insert, error_callback=handle_error ), gpudb.GPUdbTableMonitor.Callback( callback_type=gpudb.GPUdbTableMonitor.Callback.Type.UPDATED, event_callback=handle_update, error_callback=handle_error ), gpudb.GPUdbTableMonitor.Callback( callback_type=gpudb.GPUdbTableMonitor.Callback.Type.DELETED, event_callback=handle_delete, error_callback=handle_error ) ] # Create and start monitor options = gpudb.GPUdbTableMonitor.Options() monitor = gpudb.GPUdbTableMonitor.Client( db=db, table_name='events', callback_list=callbacks, options=options ) # Run monitor in background thread monitor_thread = threading.Thread(target=monitor.start) monitor_thread.daemon = True monitor_thread.start() # Insert data while monitoring import time for i in range(5): db.insert_records( table_name='events', records={'id': i, 'name': f'Event {i}', 'value': i * 10.5} ) time.sleep(1) # Stop monitoring monitor.stop() monitor_thread.join() ``` -------------------------------- ### High-Volume Data Load Example Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-multihead-io.md Demonstrates loading one million records into a time-series table using `GPUdbIngestor`. It includes table creation, data generation, and handling potential `InsertionException`s. ```python import gpudb from gpudb import GPUdbIngestor # Setup db = gpudb.GPUdb(host='http://localhost:9191') # Create table columns = [ ['id', 'long'], ['timestamp', 'datetime'], ['value', 'double'], ['category', 'string', 'char32'] ] record_type = gpudb.GPUdbRecordType(columns) db.create_table(table_name='time_series', record_type=record_type) # Create ingestor for bulk loading ingestor = GPUdbIngestor( db=db, table_name='time_series', batch_size=50000 # Flush every 50k records ) # Load data from generator def generate_records(count): import datetime base_time = datetime.datetime(2024, 1, 1) for i in range(count): yield { 'id': i, 'timestamp': base_time + datetime.timedelta(minutes=i), 'value': 100.0 + (i % 50), 'category': ['A', 'B', 'C'][i % 3] } # Ingest 1 million records try: for record in generate_records(1000000): ingestor.insert_records(record) # Flush remaining records ingestor.flush() print(f"Successfully loaded {ingestor.get_count()} records") except gpudb.InsertionException as e: print(f"Insertion failed: {e}") print(f"Inserted {e.get_count()} before failure") print(f"Failed indices: {e.get_failed_indices()}") ``` -------------------------------- ### Example: Data Export to File Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-file-handler.md Illustrates exporting data from a GPUdb table to a CSV file. This involves initializing GPUdbFileHandler with the EXPORT mode and executing the operation. ```python import gpudb from gpudb import GPUdbFileHandler, OpMode db = gpudb.GPUdb(host='http://localhost:9191') # Export table to CSV handler = GPUdbFileHandler( db=db, file_path='/data/export.csv', table_name='my_table', mode=OpMode.EXPORT ) response = handler.execute() print(f"Exported {response['count']} records to /data/export.csv") ``` -------------------------------- ### Minimal Kinetica Python API Example Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/README.md Connects to a Kinetica server, creates a table, inserts records, and queries them. Ensure the Kinetica server is running and accessible. ```python import gpudb # Connect to server db = gpudb.GPUdb(host='http://localhost:9191') # Create table columns = [['id', 'int'], ['name', 'string', 'char32']] record_type = gpudb.GPUdbRecordType(columns) db.create_table(table_name='users', record_type=record_type) # Insert records db.insert_records(table_name='users', records={ 'id': 1, 'name': 'Alice' }) # Query records response = db.get_records(table_name='users') for record in response['records']: print(record) ``` -------------------------------- ### Batch Processing Pattern Example Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-multihead-io.md Illustrates a common pattern for batch processing data: loading raw data into a staging table, processing it, and then loading the transformed data into a final table using `GPUdbIngestor`. ```python import gpudb from gpudb import GPUdbIngestor db = gpudb.GPUdb(host='http://localhost:9191') # Setup source and destination tables db.create_table( table_name='staging', record_type=gpudb.GPUdbRecordType([ ['raw_id', 'int'], ['raw_value', 'string', 'char64'] ]) ) db.create_table( table_name='processed', record_type=gpudb.GPUdbRecordType([ ['id', 'long'], ['value', 'double'], ['processed_at', 'datetime'] ]) ) # Load staging data staging_ingestor = GPUdbIngestor(db, 'staging', batch_size=100000) for i in range(1000): staging_ingestor.insert_records({ 'raw_id': i, 'raw_value': f'value_{i}' }) staging_ingestor.flush() # Process and load to final table import datetime processed_ingestor = GPUdbIngestor(db, 'processed', batch_size=100000) staging_records = db.get_records(table_name='staging')['records'] for record in staging_records: processed_record = { 'id': record['raw_id'], 'value': float(record['raw_value'].split('_')[1]), 'processed_at': datetime.datetime.now().isoformat() } processed_ingestor.insert_records(processed_record) processed_ingestor.flush() print(f"Processed {processed_ingestor.get_count()} records") ``` -------------------------------- ### Log Detailed Error Information Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/errors.md Example of logging exceptions with full traceback information for easier debugging. ```python import logging logger = logging.getLogger(__name__) try: db.insert_records(table_name, records) except Exception as e: logger.error(f"Operation failed: {e}", exc_info=True) raise ``` -------------------------------- ### GPUdbTableMonitor.Client Constructor Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table-monitor.md Initializes the GPUdbTableMonitor client to start monitoring a specific table. It requires a GPUdb client instance, the table name, and a list of callback handlers for different event types. Optional configuration options can also be provided. ```APIDOC ## GPUdbTableMonitor.Client Constructor Initializes the table monitor client. ### Parameters - **db** (GPUdb) - Required - GPUdb client instance - **table_name** (str) - Required - Name of table to monitor - **callback_list** (list[Callback]) - Required - List of Callback objects - **options** (Options) - Optional - Monitor options **Throws:** `GPUdbException` if connection fails or parameters invalid ``` -------------------------------- ### GPUdbRecordType Instance Methods Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-record.md Provides methods to retrieve column definitions, check for column existence, and get key information from a GPUdbRecordType. ```APIDOC ## GPUdbRecordType Instance Methods ### get_columns() #### Description Returns the list of column definitions. #### Code Example ```python columns = record_type.get_columns() for col in columns: print(f"{col.name}: {col.column_type}") ``` #### Returns list of `GPUdbRecordColumn` objects ### get_column(column_name) #### Description Gets a specific column definition by name. #### Code Example ```python id_col = record_type.get_column('id') print(f"ID column type: {id_col.column_type}") ``` #### Parameters - **column_name** (str) - Required - Name of the column #### Returns `GPUdbRecordColumn` or None ### get_column_index(column_name) #### Description Gets the index of a column by name. #### Code Example ```python idx = record_type.get_column_index('name') ``` #### Parameters - **column_name** (str) - Required - Column name #### Returns int - Column index, or -1 if not found ### has_column(column_name) #### Description Checks if a column exists. #### Code Example ```python if record_type.has_column('email'): print("Email column exists") ``` #### Parameters - **column_name** (str) - Required - Column name #### Returns bool ### get_primary_key() #### Description Returns the primary key column name(s). #### Code Example ```python pk = record_type.get_primary_key() print(f"Primary key: {pk}") ``` #### Returns str or list of str ### get_shard_key() #### Description Returns the shard key column name(s). #### Code Example ```python shard_key = record_type.get_shard_key() ``` #### Returns str or list of str ### get_column_count() #### Description Returns the number of columns. #### Code Example ```python num_cols = record_type.get_column_count() ``` #### Returns int ### to_json() #### Description Converts the type to JSON representation. #### Code Example ```python json_str = record_type.to_json() ``` #### Returns str - JSON representation ``` -------------------------------- ### Start Table Monitoring Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table-monitor.md Initiates the table monitoring process. This method will block until the monitoring is stopped via `stop()` or an interruption occurs. ```python monitor.start() ``` -------------------------------- ### Define Complex GPUdbRecordType with Column Properties Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-record.md Example demonstrating how to define a complex record type using various column properties like primary key, character limits, nullability, and precision. ```python import gpudb # Create a complex record type with multiple column properties columns = [ # Primary key ['user_id', 'long', gpudb.GPUdbColumnProperty.PRIMARY_KEY], # String with character limit ['username', 'string', gpudb.GPUdbColumnProperty.CHAR32], # Email as indexed string ['email', 'string', gpudb.GPUdbColumnProperty.CHAR64], # Nullable field ['phone', 'string', gpudb.GPUdbColumnProperty.CHAR16], # Decimal with precision ['balance', 'decimal', 'decimal(12,2)'], # Timestamps ['created_at', 'datetime'], ['updated_at', 'datetime'], # Boolean flag ['is_active', 'boolean'], # JSON metadata ['metadata', 'json'] ] record_type = gpudb.GPUdbRecordType(columns) # Query the type structure for col in record_type.get_columns(): print(f"{col.name:20} {col.column_type:10} nullable={col.is_nullable}") ``` -------------------------------- ### Configure GPUdb from Dictionary Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/configuration.md Initialize GPUdb.Options using a dictionary for configuration parameters. ```python from gpudb import GPUdb config_dict = { 'username': 'myuser', 'password': 'mypass', 'timeout': 30, 'skip_ssl_cert_verification': True, 'logging_level': 'debug', 'max_retries': 3 } options = GPUdb.Options(config_dict) db = GPUdb(host='http://localhost:9191', options=options) ``` -------------------------------- ### Example: Complex Type Definition Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-record.md Demonstrates how to define a complex record type using various column properties and data types, and how to query the defined type structure. ```APIDOC ## Example: Complex Type Definition ```python import gpudb # Create a complex record type with multiple column properties columns = [ # Primary key ['user_id', 'long', gpudb.GPUdbColumnProperty.PRIMARY_KEY], # String with character limit ['username', 'string', gpudb.GPUdbColumnProperty.CHAR32], # Email as indexed string ['email', 'string', gpudb.GPUdbColumnProperty.CHAR64], # Nullable field ['phone', 'string', gpudb.GPUdbColumnProperty.CHAR16], # Decimal with precision ['balance', 'decimal', 'decimal(12,2)'] , # Timestamps ['created_at', 'datetime'], ['updated_at', 'datetime'], # Boolean flag ['is_active', 'boolean'], # JSON metadata ['metadata', 'json'] ] record_type = gpudb.GPUdbRecordType(columns) # Query the type structure for col in record_type.get_columns(): print(f"{col.name:20} {col.column_type:10} nullable={col.is_nullable}") ``` ``` -------------------------------- ### Create GPUdb.Options Instances Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/configuration.md Demonstrates various ways to create GPUdb.Options objects, including default, from a dictionary, from another Options object, and using a class method. ```python from gpudb import GPUdb # Create default options options = GPUdb.Options() # Create from dictionary options = GPUdb.Options({ 'username': 'user', 'password': 'pass', 'timeout': 30 }) # Create from another Options object (copy) options2 = GPUdb.Options(options) # Use class method options = GPUdb.Options.default() ``` -------------------------------- ### Get Column Descriptions Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/dbapi.md Access `cursor.description` after executing a query to get metadata about the result columns, including name and type information. ```python cursor.execute("SELECT id, name, value FROM users") columns = cursor.description # List of column info for col_info in columns: print(f"Column: {col_info[0]}, Type: {col_info[1]}") ``` -------------------------------- ### Queue-Based Table Monitoring Example Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table-monitor.md Implement queue-based monitoring for multi-threaded applications. Event handlers place events onto a shared queue for decoupled processing. The monitor runs in a background thread. ```python import gpudb from queue import Queue import threading db = gpudb.GPUdb(host='http://localhost:9191') # Create table columns = [['id', 'int'], ['data', 'string', 'char64']] record_type = gpudb.GPUdbRecordType(columns) db.create_table(table_name='data_table', record_type=record_type) # Shared queue for inter-thread communication event_queue = Queue() # Event handlers that enqueue events def on_insert(record): event_queue.put(('insert', record)) def on_update(count): event_queue.put(('update', count)) def on_error(msg): event_queue.put(('error', msg)) # Create monitor with queue-based callbacks callbacks = [ gpudb.GPUdbTableMonitor.Callback( callback_type=gpudb.GPUdbTableMonitor.Callback.Type.INSERT_DECODED, event_callback=on_insert, error_callback=on_error ), gpudb.GPUdbTableMonitor.Callback( callback_type=gpudb.GPUdbTableMonitor.Callback.Type.UPDATED, event_callback=on_update, error_callback=on_error ) ] monitor = gpudb.GPUdbTableMonitor.Client( db=db, table_name='data_table', callback_list=callbacks ) # Run monitor in background monitor_thread = threading.Thread(target=monitor.start) monitor_thread.daemon = True monitor_thread.start() ``` -------------------------------- ### Create and Access GPUdbRecord Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-record.md Demonstrates how to create a new GPUdbRecord with specified fields and access its data. Use this to instantiate records before inserting them into a table. ```python record = gpudb.GPUdbRecord(record_type, { 'user_id': 1, 'username': 'alice', 'email': 'alice@example.com', 'phone': '+1-555-0100', 'balance': 1234.56, 'created_at': '2024-01-15 10:30:00', 'updated_at': '2024-01-15 10:30:00', 'is_active': True, 'metadata': '{"tier": "premium"}' }) # Access record fields print(f"Username: {record.get_field('username')}") print(f"Balance: {record.get_field('balance')}") # Modify and convert record.set_field('is_active', False) data_dict = record.to_dict() ``` -------------------------------- ### Create and Reference GPUdbTable Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table.md Demonstrates how to create a new GPUdbTable with a defined schema or reference an existing table using its name. ```python from gpudb import GPUdbTable, GPUdbRecordType # Create a new table columns = [['id', 'int'], ['name', 'string', 'char32']] record_type = GPUdbRecordType(columns) table = GPUdbTable(record_type=record_type, name='users', db=db) # Reference an existing table existing_table = GPUdbTable(None, name='users', db=db) ``` -------------------------------- ### Get GPUdbTable Name Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table.md Retrieves the name of the GPUdbTable instance. ```python name = table.get_name() ``` -------------------------------- ### get_primary_url Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb.md Gets the URL of the primary or head node in the HA cluster. ```APIDOC ## get_primary_url() ### Description Gets the primary/head cluster URL. ### Method Not applicable (Python SDK method) ### Parameters None ### Request Example ```python primary = db.get_primary_url() print(f"Primary cluster: {primary}") ``` ### Response #### Success Response A string representing the URL of the primary cluster. #### Response Example ```json "http://cluster1:9191" ``` ``` -------------------------------- ### Initialize GPUdb Client Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb.md Instantiate the GPUdb client with default options or custom authentication and SSL settings. ```python from gpudb import GPUdb # Create a client with default options db = GPUdb(host='http://localhost:9191') # Create a client with authentication and options options = GPUdb.Options() options.username = "user" options.password = "password" options.skip_ssl_cert_verification = False db = GPUdb(host='https://kinetica.example.com:8082/gpudb-0', options=options) ``` -------------------------------- ### Get Table Size Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table.md Retrieves the current number of records in the GPUdbTable. ```APIDOC ## get_size ### Description Returns the total number of records currently in the table. ### Method `get_size()` ### Parameters None ### Request Example ```python size = users.get_size() ``` ### Response #### Success Response (200) - **size** (int) - The number of records in the table. #### Response Example ```json { "size": 150 } ``` ``` -------------------------------- ### Get GPUdbTable Schema Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table.md Retrieves the Avro schema object for the GPUdbTable. ```python schema = table.get_schema() ``` -------------------------------- ### get_urls Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb.md Gets all available cluster URLs, potentially including more details than get_cluster_list. ```APIDOC ## get_urls() ### Description Gets all available cluster URLs. ### Method Not applicable (Python SDK method) ### Parameters None ### Request Example ```python urls = db.get_urls() ``` ### Response #### Success Response A list of URL objects or strings representing all available cluster URLs. #### Response Example ```json [ "http://cluster1:9191", "http://cluster2:9191" ] ``` ``` -------------------------------- ### Create Simple GPUdbRecordColumn Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-record.md Instantiate a GPUdbRecordColumn with just the name and data type. This is for basic column definitions. ```python from gpudb import GPUdbRecordColumn, GPUdbColumnProperty # Simple column col1 = GPUdbRecordColumn(name='id', column_type='int') ``` -------------------------------- ### Get Column Index Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table.md Retrieves the index of a specified column within the GPUdbTable. ```python idx = table.get_column_index('name') ``` -------------------------------- ### Get GPUdbTable Size Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table.md Retrieves the number of records currently stored in the GPUdbTable. ```python count = table.get_size() print(f"Table contains {count} records") ``` -------------------------------- ### Get GPUdbTable Record Type Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table.md Retrieves the record type definition for the GPUdbTable. ```python record_type = table.get_type() columns = record_type.get_columns() ``` -------------------------------- ### Get Records Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table.md Retrieves records from the GPUdbTable, with options to limit the number of records returned. ```APIDOC ## get_records ### Description Retrieves records from the table, with an optional limit. ### Method `get_records(limit=None)` ### Parameters #### Path Parameters None #### Query Parameters - **limit** (int) - Optional - The maximum number of records to retrieve. ### Request Example ```python response = users.get_records(limit=10) ``` ### Response #### Success Response (200) - **records** (list of dict) - A list of records retrieved from the table. #### Response Example ```json { "records": [ { "user_id": 1, "username": "alice", "email": "alice@example.com", "age": 30, "score": 95.5 }, { "user_id": 2, "username": "bob", "email": "bob@example.com", "age": 25, "score": 87.3 } ] } ``` ``` -------------------------------- ### GPUdbTableMonitor.Options Constructor and Properties Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table-monitor.md Demonstrates how to create and configure options for the GPUdbTableMonitor. This includes setting inactivity timeouts, monitor ports, and retry parameters. ```APIDOC ## GPUdbTableMonitor.Options ### Description Options for configuring the GPUdbTableMonitor client. ### Constructor ```python from gpudb import GPUdbTableMonitor options = GPUdbTableMonitor.Options() options.inactivity_timeout = 10.0 # 10 minutes options.monitor_port = 8089 # Custom port ``` ### Configuration Properties | Property | Type | Default | Description | |----------|------|---------|-------------| | inactivity_timeout | float | 20 | Timeout in minutes for monitor inactivity; monitors table existence and cluster status if no events for this duration | | monitor_port | int | 8090 | ZMQ port for receiving monitor notifications | | max_retries | int | 3 | Maximum connection retry attempts | | wait_interval | float | 1 | Wait interval in seconds between retries | ### Class Methods #### default() Creates a default Options instance. ```python options = GPUdbTableMonitor.Options.default() ``` **Returns:** GPUdbTableMonitor.Options with defaults ``` -------------------------------- ### Create, Insert, Query, and Delete Table Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb.md Demonstrates basic table operations: creating a table with a defined record type, inserting records, querying records, executing SQL, and finally deleting the table. ```python columns = [ ['id', 'int'], ['name', 'string', 'char32'], ['value', 'double'] ] record_type = gpudb.GPUdbRecordType(columns) db.create_table(table_name='my_data', record_type=record_type) # Insert records records = [ {'id': 1, 'name': 'Alice', 'value': 10.5}, {'id': 2, 'name': 'Bob', 'value': 20.3} ] db.insert_records(table_name='my_data', records=records) # Query records response = db.get_records(table_name='my_data', limit=10) for record in response['records']: print(f"{record['id']}: {record['name']} = {record['value']}") # Execute SQL result = db.execute_sql("SELECT * FROM my_data WHERE value > 15") sql_records = db.get_records(table_name=result['table_name'])['records'] # Cleanup db.delete_table(table_name='my_data') ``` -------------------------------- ### Get Column Count Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-record.md Obtain the total number of columns defined in the record type. ```python num_cols = record_type.get_column_count() ``` -------------------------------- ### Initialize GPUdbTableMonitor Options Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/configuration.md Create default options for GPUdbTableMonitor. This sets inactivity timeouts, monitor ports, and retry parameters. ```python from gpudb import GPUdbTableMonitor options = GPUdbTableMonitor.Options() options = GPUdbTableMonitor.Options.default() ``` -------------------------------- ### Get Inserted Record Count Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-multihead-io.md Retrieve the number of records that were successfully inserted before an `InsertionException` occurred. ```python count = exception.get_count() ``` -------------------------------- ### GPUdbSqlContext.offset Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-sql.md Applies an OFFSET clause to specify the starting point for returning records, useful for pagination. ```APIDOC ## GPUdbSqlContext.offset ### Description Applies an OFFSET clause to specify the starting point for returning records, useful for pagination. ### Parameters #### Path Parameters - **offset_value** (int) - Required - Record offset ### Returns self (for method chaining) ``` -------------------------------- ### show_system_properties Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb.md Retrieves system properties and configuration details. ```APIDOC ## show_system_properties() ### Description Retrieves system properties and configuration. ### Method Not applicable (Python SDK method) ### Parameters None ### Request Example ```python props = db.show_system_properties() version = props['property_map'].get('version.gpudb_core_version') print(f"Server version: {version}") ``` ### Response #### Success Response An AttrDict containing a `property_map` dictionary with various system properties. #### Response Example ```json { "property_map": { "version.gpudb_core_version": "7.0.0", "network.port": 9191 } } ``` ``` -------------------------------- ### Create Default Options Instance Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table-monitor.md Instantiate GPUdbTableMonitor.Options with default values. ```python options = GPUdbTableMonitor.Options.default() ``` -------------------------------- ### Initialize MultipartOperation Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-file-handler.md Instantiate MultipartOperation for handling large file transfers. Configure the database connection, file path, target table, and optionally the chunk size. ```python from gpudb import MultipartOperation operation = MultipartOperation( db=db, file_path='/path/to/large_file.bin', table_name='my_table', chunk_size=5242880 # 5MB chunks ) ``` -------------------------------- ### Get Table Size Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-table.md Retrieves the current number of records in the table. This is useful for monitoring table contents. ```python size = users.get_size() print(f"Table now has {size} records") ``` -------------------------------- ### Get Row Count Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/dbapi.md Check `cursor.rowcount` after executing a DML statement to determine the number of rows affected. ```python cursor.execute("DELETE FROM users WHERE age < 18") print(f"Deleted {cursor.rowcount} rows") ``` -------------------------------- ### Get Shard Key Column(s) Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-record.md Retrieve the name(s) of the column(s) designated as the shard key for the record type. ```python shard_key = record_type.get_shard_key() ``` -------------------------------- ### Get Primary Key Column(s) Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-record.md Retrieve the name(s) of the column(s) designated as the primary key for the record type. ```python pk = record_type.get_primary_key() print(f"Primary key: {pk}") ``` -------------------------------- ### Column Type Reference Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-record.md Lists the supported data types for columns within a GPUdbRecord, along with their descriptions and examples. ```APIDOC ## Column Type Reference Supported column types for GPUdbRecordColumn: | Type | Description | Example | |------|-------------|---------| | int | 32-bit integer | `['id', 'int']` | | long | 64-bit integer | `['big_num', 'long']` | | float | Single-precision float | `['score', 'float']` | | double | Double-precision float | `['value', 'double']` | | string | Variable-length string | `['name', 'string', 'char32']` | | boolean | Boolean value | `['active', 'boolean']` | | bytes | Binary data | `['data', 'bytes']` | | date | Date (YYYY-MM-DD) | `['created', 'date']` | | time | Time (HH:MM:SS) | `['start_time', 'time']` | | datetime | Datetime timestamp | `['updated', 'datetime']` | | decimal | Fixed-point decimal | `['amount', 'decimal', 'decimal(10,2)']` | | ipv4 | IPv4 address | `['ip', 'ipv4']` | | uuid | UUID identifier | `['uuid', 'uuid']` | | json | JSON object | `['metadata', 'json']` | | vector | Vector (float array) | `['embedding', 'vector(768)']` | ``` -------------------------------- ### Monitor Cluster Health Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/errors.md Example of checking the overall cluster status and handling a specific HA unavailability exception. ```python try: status = db.show_system_status() print(f"Cluster status: {status}") except gpudb.GPUdbHAUnavailableException: print("HA ring is broken, restart clusters") ``` -------------------------------- ### Get Field Value by Column Index Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-record.md Retrieve the value of a specific field from the record using its column index. ```python value = record.get_field_by_index(0) ``` -------------------------------- ### GPUdbRecordColumn Constructor Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-record.md Initializes a GPUdbRecordColumn with name, type, optional properties, and nullability. ```APIDOC ## GPUdbRecordColumn Constructor ### Description Initializes a GPUdbRecordColumn with name, type, optional properties, and nullability. ### Constructor ```python from gpudb import GPUdbRecordColumn, GPUdbColumnProperty # Simple column col1 = GPUdbRecordColumn(name='id', column_type='int') # Column with properties col2 = GPUdbRecordColumn( name='name', column_type='string', column_properties=[GPUdbColumnProperty.CHAR32], is_nullable=False ) # Decimal column with precision col3 = GPUdbRecordColumn( name='amount', column_type='decimal', column_properties=['decimal(10,2)'], is_nullable=False ) ``` ### Parameters #### Parameters - **name** (str) - Required - Column name - **column_type** (str) - Required - Data type (int, string, double, etc.) - **column_properties** (list) - Optional - List of column property flags - **is_nullable** (bool) - Optional - Whether column allows NULL values (defaults to False) ### Returns A GPUdbRecordColumn instance ``` -------------------------------- ### Get Field Value by Column Name Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-record.md Retrieve the value of a specific field from the record using its column name. ```python value = record.get_field('name') ``` -------------------------------- ### Get Failed Record Indices Source: https://github.com/kineticadb/kinetica-api-python/blob/master/_autodocs/api-reference/gpudb-multihead-io.md Retrieve the indices of records that failed to be inserted during a bulk operation, as reported by an `InsertionException`. ```python failed = exception.get_failed_indices() print(f"Failed record indices: {failed}") ```