Updated on 2026-11-06 GMT+08:00

Creating a Table and Writing Data to the Table

This section describes how to create a data table and write data into the table. This provides a data foundation for subsequent queries and analysis.

Prerequisites

Example

This example creates three data tables (text_samples, sales_line_items, and category_sales) are writes text, sales details, and category sales data respectively. This provides a data foundation for subsequent queries, analysis, and UDF usage in 3.1.6.4.4, 3.1.6.4.5, 3.1.6.4.6, 3.1.6.4.7, 3.1.6.4.8, 3.1.6.4.9, 3.1.6.4.10.

  1. Import dependencies and connect to the AuraJobV2 endpoint.
  2. Create the text_samples table (containing the id and content columns) and insert five text records.
  3. Create the sales_line_items table (containing the quantity, unit_price, discount_rate, tax_rate, return_flag, line_status, and ship_date columns) and insert five sales records.
  4. Create the category_sales table (containing the category and amount columns) and insert 12 category sales records.
  5. Disable the AuraJobV2 endpoint connection.
import os
import logging
import ibis
from aura_frame.multimodal import ai_lake

target_database = "test"  #Create a database in LakeFormation, for example, test. For details, see Creating a DataArts Lake Formation Instance and Planning Metadata.
#Connect to the AuraJobV2 endpoint. For details about the parameters, see Establishing a Connection with the AuraJobV2 Endpoint.
con = ai_lake.connect(
    aura_endpoint=os.getenv("aura_endpoint"),         # Endpoint where the AuraJobV2 endpoint is located
    aura_endpoint_id=os.getenv("aura_endpoint_id"),    # AuraJobV2 endpoint ID
    aura_endpoint_name=os.getenv("aura_endpoint_name"),# AuraJobV2 endpoint name
    aura_workspace_id=os.getenv("aura_workspace_id"),  # Workspace ID
    lf_catalog_name=os.getenv("lf_catalog_name"),      # LakeFormation Catalog name
    access_key=os.getenv("access_key"),                # Access Key ID (AK)
    secret_key=os.getenv("secret_key"),                # Secret Key (SK)
    default_database=target_database,                       # LakeFormation database name
    use_single_cn_mode=True,   # Whether to enable the single-CN mode
    logging_level=logging.INFO # Set the log level.
)
try:
    table_name = "text_samples"
    table_schema = ibis.schema({"id": "int32", "content": "string"})
    con.sql(f'DROP TABLE IF EXISTS "{target_database}"."{table_name}";')
    con.create_table(
        table_name,
        schema=table_schema,
        database=target_database,
        table_format="orc",
        if_not_exists=True,
    )
    insert_sql = f'''
    INSERT INTO "{target_database}"."{table_name}" (id, content) VALUES
        (1, 'Hello, how are you today?'),
        (2, 'The quick brown fox jumps over the lazy cat.'),
        (3, 'Machine learning is transforming the way we process data.'),
        (4, 'Natural language processing enables computers to understand human language.'),
        (5, 'Tokenization is the first step in text processing pipelines.')
    '''
    con.sql(insert_sql)
    print(f"Table {table_name} has been created and data has been written.")
    table_name = "sales_line_items"
    table_schema = ibis.schema({
        "quantity": "int32",
        "unit_price": "float64",
        "discount_rate": "float64",
        "tax_rate": "float64",
        "return_flag": "string",
        "line_status": "string",
        "ship_date": "date",
    })
    con.sql(f'DROP TABLE IF EXISTS "{target_database}"."{table_name}";')
    con.create_table(
        table_name,
        schema=table_schema,
        database=target_database,
        table_format="orc",
        if_not_exists=True,
    )
    insert_sql = f'''
    INSERT INTO "{target_database}"."{table_name}" (quantity, unit_price, discount_rate, tax_rate, return_flag, line_status, ship_date) VALUES
        (10, 1000.00, 0.05, 0.08, 'N', 'O', '1998-08-01'),
        (20, 2000.00, 0.10, 0.05, 'R', 'F', '1998-09-15'),
        (15, 1500.00, 0.08, 0.07, 'N', 'O', '1998-07-20'),
        (25, 2500.00, 0.05, 0.08, 'R', 'F', '1998-10-01'),
        (30, 3000.00, 0.02, 0.10, 'N', 'O', '1998-08-15')
    '''
    con.sql(insert_sql)
    print(f"Table {table_name} has been created and data has been written.")
    table_name = "category_sales"
    table_schema = ibis.schema({"category": "string", "amount": "float64"})
    con.sql(f'DROP TABLE IF EXISTS "{target_database}"."{table_name}";')
    con.create_table(
        table_name,
        schema=table_schema,
        database=target_database,
        table_format="orc",
        if_not_exists=True,
    )
    insert_sql = f'''
    INSERT INTO "{target_database}"."{table_name}" (category, amount) VALUES
        ('Electronics', 1299.00),
        ('Electronics', 799.00),
        ('Electronics', 599.00),
        ('Electronics', 1099.00),
        ('Books', 29.99),
        ('Books', 49.99),
        ('Books', 19.99),
        ('Books', 39.99),
        ('Clothing', 89.00),
        ('Clothing', 159.00),
        ('Clothing', 49.00),
        ('Clothing', 199.00)
    '''
    con.sql(insert_sql)
    print(f"Table {table_name} has been created and data has been written.")
finally:
    con.close()