This topic guides you through the entire process, from connection, creating a Collection, building a virtual table, to reading, writing, and cleanup, using the simplest example.
Prerequisites
- OceanBase AI Database V4.6.2.1 or later is deployed. For deployment instructions, see Choose an appropriate deployment plan.
- json-table-sdk (version 1.5.0 or later) is installed in Namespace mode. For installation instructions, see the "Install json-table-sdk" section in Connect to database by using json-table-sdk.
- A connectable database account and target database are prepared. For complete parameter descriptions on initializing the client using json-table-sdk, see Connect to database by using json-table-sdk.
Initialize the client and create a Collection
You must enable use_namespace=True, create or bind a Collection first, then perform business read and write operations.
# Import the VirtualTableClient and RequestContext classes from the json-table-sdk module.
from antob_json_table import VirtualTableClient, RequestContext
# Create Client
client = VirtualTableClient(
host="127.0.0.1", # Database address
port=2881, # Database port
user="root@mysql", # Username
password="", # Password
database="test", # Database name
use_namespace=True, # Whether to use namespaces. This parameter must be set to True for logical table mode.
)
# Create Collection (partition_count is optional, range [1, 8192], default 64)
coll = client.create_collection("my_flash_app", partition_count=64)
Specify an app
Each time you create a table or perform read/write operations, you need to pass a RequestContext to indicate under which app the operation is being performed. app_prefix corresponds to the Namespace name. For parameter descriptions, see Data operations. For isolation rules, see Collection and app management.
# app_prefix indicates the application prefix, which is automatically mapped to the namespace name by the system.
# Created_by indicates the operator. This parameter is optional and defaults to "admin".
ctx = RequestContext(coll, app_prefix="app001", created_by="admin")
Create a virtual table and write data
client.create_table(ctx, """
CREATE TABLE users (
id BIGINT NOT NULL,
name VARCHAR(64) NOT NULL,
age TINYINT DEFAULT 0
)
""")
client.insert(ctx, """
INSERT INTO users (id, name, age) VALUES
(1, 'Zhang San', 25),
(2, 'Li Si', 30)
""")
Note
Users directly use business table names in SQL statements (for example, users). The SDK locates data based on the namespace_id and table name.
Query and update
rows = client.select(
ctx,
"SELECT * FROM users WHERE age > 25 ORDER BY age DESC LIMIT 100",
)
client.update(ctx, "UPDATE users SET age = 26 WHERE id = 1 LIMIT 10")
Prewarm and cleanup
# Prewarm the current app (it must have performed at least one business operation to register a namespace).
coll.prewarm("app001")
# View Table Structure
schema = client.get_table_schema(ctx, "users")
# Delete Virtual Table
client.drop_table(ctx, "users")
# Delete App/Collection
coll.drop_app("app001")
client.delete_collection("my_flash_app")
client.close()
Multi-application isolation
You can use different app_prefix under the same Collection to isolate data. Tables with the same name in different Apps do not affect each other. For complete rules and examples, see Collection and app management.
References
- Flash application scenario overview
- Limitations and constraints
- Connect to database by using json-table-sdk
- Collection and app management
- The quick start guide only provides the simplest data operation examples. For complete features, refer to Data operations.