This topic describes how to read specified data from OSS and insert the data into ApsaraDB for RDS (RDS) by using DLA. The following code uses ApsaraDB RDS for MySQL (MySQL) as an example.
Procedure
Step 1: Create an OSS schema
CREATE DATABASE tpch_50x_text
WITH DBPROPERTIES (
catalog = 'oss',
location = 'oss://${your bucket}/datasets/tpch/50x/text_date/'
)
Step 2: Create a table named customer in OSS
CREATE EXTERNAL TABLE tpch_50x_text.customer (
c_custkey int,
c_name string
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '|'
STORED AS TEXTFILE
LOCATION 'oss://${your bucket}/datasets/tpch/50x/text_date/customer_text'
Query data in the customer table.
mysql> select * from customer;
+------+--------------------+
| c_custkey | c_name
+------+--------------------+
| 1 | Customer#000000001 |
| 3 | Customer#000000003 |
| 5 | Customer#000000005 |
| 2 | Customer#000000002 |
| 4 | Customer#000000004 |
| 7 | Customer#000000007 |
| 6 | Customer#000000006 |
| 9 | Customer#000000009 |
| 8 | Customer#000000008 |
| 10 | Customer#000000010 |
+------+--------------------+
10 rows in set (0.26 sec)
Step 3: Create an RDS schema
CREATE SCHEMA hello_mysql_vpc_rds WITH DBPROPERTIES (
CATALOG = 'mysql',
LOCATION = 'jdbc:mysql://rm-****.mysql.rds.aliyuncs.com:3306/rds_mysql_dbname',
USER = 'rds_mysqldb_username',
PASSWORD = 'rds_mysqldb_password',
INSTANCE_ID = 'rds_mysql_instance_id',
VPC_ID = 'rds_mysqldb_vpcid'
);
Step 4: Create a table named person in RDS
create external table person (
id int,
name varchar(1023),
age int
) tblproperties (
table_mapping = "person"
);
Insert data into the person table.
insert into person
values
(1, 'james', 10),
(2, 'bond', 20),
(3, 'jack', 30),
(4, 'lucy', 40);
Step 5: Import data from OSS to RDS
Execute the following SQL statements to convert the first 10 records from the customer table in oss_db, and then insert them into the hello_mysql_vpc_rds.person table:
mysql> insert into hello_mysql_vpc_rds.person
select c_custkey, c_name, c_custkey + 20 from tpch_50x_text.customer limit 10;
+------+
| rows |
+------+
| 10 |
+------+
1 row in set (4.57 sec)
mysql> select * from person;
+------+--------------------+------+
| id | name | age |
+------+--------------------+------+
| 1 | james | 10 |
| 2 | bond | 20 |
| 3 | jack | 30 |
| 4 | lucy | 40 |
| 1 | Customer#000000001 | 21 |
| 3 | Customer#000000003 | 23 |
| 5 | Customer#000000005 | 25 |
| 2 | Customer#000000002 | 22 |
| 4 | Customer#000000004 | 24 |
| 7 | Customer#000000007 | 27 |
| 6 | Customer#000000006 | 26 |
| 9 | Customer#000000009 | 29 |
| 8 | Customer#000000008 | 28 |
| 10 | Customer#000000010 | 30 |
+------+--------------------+------+
14 rows in set (0.26 sec)