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
- The Aura DataFrame SDK has been installed. For details, see Obtaining and Installing the Aura DataFrame SDK.
- An AuraJobV2 endpoint has been created, and the endpoint name has been obtained. For details, see Creating an Aura Engine Endpoint.
- A LakeFormation instance has been created, where a catalog and a database are ready. For details, see Creating a LakeFormation Instance and Planning Metadata.
- A parallel file system has been created. For details, see Creating a Parallel File System.
- The parameters in os.getenv() involved in the code have been configured as environment variables.
- The Python 3.11 has been installed.
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.
- Import dependencies and connect to the AuraJobV2 endpoint.
- Create the text_samples table (containing the id and content columns) and insert five text records.
- 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.
- Create the category_sales table (containing the category and amount columns) and insert 12 category sales records.
- 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()
Helpful Links
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot