Importing Table Data from DLI to a GaussDB(DWS) Cluster
This exercise demonstrates how to use the GaussDB(DWS) foreign table function to import data from DLI to GaussDB(DWS).
For details about DLI, see What Is Data Lake Insight?
This exercise lasts for approximately 60 minutes and involves utilizing various cloud services such as Virtual Private Cloud (VPC) and Subnet, Data Lake Insight (DLI), Object Storage Service (OBS), and GaussDB(DWS). The following is an outline of the exercise.
- Preparations
- Step 1: Preparing DLI Source Data
- Step 2: Creating a GaussDB(DWS) Cluster
- Step 3: Obtaining Authentication Information Required by the GaussDB(DWS) External Server.
- Step 4: Importing DLI Table Data Using a Foreign Table
Preparations
- You have registered a Huawei ID and enabled Huawei Cloud services.. The account cannot be in arrears or frozen.
- You have created a VPC and subnet. For details, see Creating a VPC.
- You have obtained the AK and SK of your Huawei account. For details, see Access Keys.
Step 1: Preparing DLI Source Data
- Create a DLI elastic resource pool and queue.
- Log in to the Huawei Cloud console and choose Analytics > Data Lake Insight from the service list. The DLI console is displayed.
- In the navigation pane on the left, choose Resources > Resource Pool.
- Click Buy Resource Pool in the upper right corner, set the following parameters, and retain the default values for other parameters that are not described in the table.
Table 1 DLI elastic resource pool parameters Parameter
Value
Billing Mode
Pay-per-use
Region
CN-Hong Kong
Name
dli_dws
Specifications
Standard
CIDR Block
172.16.0.0/18.
- Click Buy and click Submit.
After the resource pool is created, go to the next step.
- On the elastic resource pool page, locate the row that contains the created resource pool, click Add Queue in the Operation column, and set the following parameters. Retain the default values for other parameters that are not described in the table.
Table 2 Adding a queue Parameter
Value
Name
dli_dws
Type
For SQL
- Click Next and click OK. The queue is created.
- Upload the source data to the OBS bucket.
- An OBS bucket has been created with a user-defined name, for example, dli-obs01 (if the bucket name is already in use, use dli-obs02 instead). The region is CN-Hong Kong.
- Download the data sample file.
- Create a folder dli_order in the OBS bucket and upload the downloaded data file to that folder.
- Go back to the DLI management console. In the navigation pane, click SQL Editor. Select dli_dws for Queue and Default for Database. Run the following command to create a database named dli_data:
1
CREATE DATABASE dli_data;
- Create a table.
LOCATION specifies the OBS directory where the data file is stored, formatted as obs://OBS bucket name/folder name. In this example, the directory is obs://dli-obs01/dli_order. If the bucket name or folder name changes, substitute it accordingly.
1 2 3 4 5 6 7 8 9
CREATE EXTERNAL TABLE dli_data.dli_order ( order_id VARCHAR(12), order_channel VARCHAR(32), order_time TIMESTAMP, cust_code VARCHAR(6), pay_amount DOUBLE, real_pay DOUBLE ) STORED AS parquet LOCATION 'obs://dli-obs01/dli_order';
- Run the following statement to query data.
1
SELECT * FROM dli_data.dli_order;
Step 2: Creating a GaussDB(DWS) Cluster
- Create a cluster . To ensure network connectivity, set the region of the GaussDB(DWS) cluster to CN-Hong Kong.
Step 3: Obtaining Authentication Information Required by the GaussDB(DWS) External Server
- Obtain the endpoint of the OBS bucket.
- Log in to the OBS management console.
- Click the bucket name, choose Overview on the left, and record the endpoint.
- Visit Endpoints to obtain the endpoint of DLI.
In this example, the endpoint is dli.ap-southeast-1.myhuaweicloud.com.
In this example (EU-Dublin), the endpoint is dli.eu-west-101.myhuaweicloud.com.
- Obtain the project ID for the specific region of the account used to create DLI.
- Move the cursor to the account name in the upper right corner and click My Credentials.
- Choose API Credentials on the left.
- In the list, find the region where the DLI instance is deployed, for example, CN-Hong Kong, and record the project ID corresponding to the region name.
- Obtain the AK and SK of your account. For details, see Prerequisites.
Step 4: Importing DLI Table Data Using a Foreign Table
- Log in to the GaussDB(DWS) database as the system administrator dbadmin. By default, you can log in to the GaussDB database.
- Run the following SQL statements to create a foreign server: The OBS endpoint is obtained from 1, the AK and SK are obtained from Preparations, and the DLI endpoint is obtained from 2.
If the GaussDB(DWS) and DLI instances are created by the same account, enter the AK and SK twice.
1 2 3 4 5 6 7 8 9
CREATE SERVER dli_server FOREIGN DATA WRAPPER DFS_FDW OPTIONS (ADDRESS'OBS endpoint', ACCESS_KEY'AK value' SECRET_ACCESS_KEY'SK value' TYPE 'DLI', DLI_ADDRESS'DLI endpoint', DLI_ACCESS_KEY'AK value', DLI_SECRET_ACCESS_KEY 'SK value' );
- Run the following SQL statement to create a target schema:
1
CREATE SCHEMA dws_data;
- Run the following SQL statements to create a foreign table: Replace Project ID with the actual value obtained in 3.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
CREATE FOREIGN TABLE dws_data.dli_pq_order ( order_id VARCHAR(14) PRIMARY KEY NOT ENFORCED, order_channel VARCHAR(32), order_time TIMESTAMP, cust_code VARCHAR(6), pay_amount DOUBLE PRECISION, real_pay DOUBLE PRECISION ) SERVER dli_server OPTIONS ( FORMAT 'parquet', ENCODING 'utf8', DLI_PROJECT_ID'Project ID' DLI_DATABASE_NAME 'dli_data', DLI_TABLE_NAME 'dli_order') DISTRIBUTE BY roundrobin;
- Run the following SQL statement to query the DLI table data through the foreign table.
The DLI table data is successfully accessed.
1
SELECT * FROM dws_data.dli_pq_order;
- Run the following SQL statements to create a local table for importing DLI table data:
1 2 3 4 5 6
CREATE TABLE dws_data.dws_monthly_order ( order_month CHAR(8), cust_code VARCHAR(6), order_count INT, total_pay_amount DOUBLE PRECISION, total_real_pay DOUBLE PRECISION );
- Run the following SQL statements to query the monthly order details of 2023 and import the result to the GaussDB(DWS) table:
1 2 3 4 5 6 7 8
INSERT INTO dws_data.dws_monthly_order ( order_month, cust_code, order_count , total_pay_amount, total_real_pay ) SELECT TO_CHAR(order_time, 'MON-YYYY'), cust_code, COUNT(*) , SUM(pay_amount), SUM(real_pay) FROM dws_data.dli_pq_order WHERE DATE_PART('Year', order_time) = 2023 GROUP BY TO_CHAR(order_time, 'MON-YYYY'), cust_code;
- Run the following SQL statement to query table data.
The DLI table data is successfully imported to the DWS database.
1
SELECT * FROM dws_data.dws_monthly_order;
Feedback
Was this page helpful?
Provide feedbackThank 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