This topic describes how to read data from OSS and then write the data to Table Store. In the following example, two OSS tables, customer and nation, are created. The INSERT…SELECT statement is used to read data from these two tables and then to write the results to Table Store.
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 OSS tables
Table customer
CREATE EXTERNAL TABLE tpch_50x_text.customer (
c_custkey int,
c_name string,
c_address string,
c_nationkey int,
c_phone string,
c_acctbal double,
c_mktsegment string,
c_comment string
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '|'
STORED AS TEXTFILE
LOCATION 'oss://${your bucket}/datasets/tpch/50x/text_date/customer_text'
Table nation
CREATE EXTERNAL TABLE tpch_50x_text.nation (
n_nationkey int,
n_name string,
n_regionkey int,
n_comment string
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '|'
STORED AS TEXTFILE
LOCATION 'oss://${your bucket}/datasets/tpch/50x/text_date/nation_text'
Step 3: Create a Table Store schema
CREATE DATABASE etl_ots_test
WITH DBPROPERTIES (
catalog = 'ots',
location = 'https://${your Table Store instance}.cn-shanghai.ots-internal.aliyuncs.com',
instance = '${your Table Store instance}'
Step 4: Create a Table Store table named test_insert
CREATE EXTERNAL TABLE test_insert (
id1_int int NOT NULL COMMENT 'Customer ID primary key',
c_address varchar(20) NULL COMMENT 'Customer address',
c_acctbal double NULL COMMENT 'Customer account balance',
PRIMARY KEY (`id1_int`)
)
Step 5: Query data in the tables
mysql> select * from etl_ots_test.test_insert;
Empty set (0.31 sec)
Query data in the nation table, where the nationkey of CANADA is 3:
mysql> select n_nationkey, n_name from nation;
+-------------+----------------+
| n_nationkey | n_name |
+-------------+----------------+
| 0 | ALGERIA |
| 1 | ARGENTINA |
| 2 | BRAZIL |
| 3 | CANADA |
| 4 | EGYPT |
| 5 | ETHIOPIA |
| 6 | FRANCE |
| 7 | GERMANY |
| 8 | INDIA |
| 9 | INDONESIA |
| 10 | IRAN |
| 11 | IRAQ |
| 14 | KENYA |
| 15 | MOROCCO |
| 16 | MOZAMBIQUE |
| 17 | PERU |
| 18 | CHINA |
| 19 | ROMANIA |
| 20 | SAUDI ARABIA |
| 21 | VIETNAM |
| 22 | RUSSIA |
| 23 | UNITED KINGDOM |
| 24 | UNITED STATES |
+-------------+----------------+
25 rows in set (0.37 sec)
Query data in the customer table based on the nationkey of 3 and c_mktsegment of BUILDING:
mysql> select count(*) from customer where c_nationkey = 3 and c_mktsegment = 'BUILDING';
+----------+
| count(*) |
+----------+
| 60350 |
+----------+
1 row in set (0.66 sec)
mysql> select * from customer where c_nationkey = 3 and c_mktsegment = 'BUILDING' order by c_custkey limit 3;
+-----------+--------------------+-------------------------+-------------+-----------------+-----------+--------------+----------------------------------------------------------------------------------------------------+
| c_custkey | c_name | c_address | c_nationkey | c_phone | c_acctbal | c_mktsegment | c_comment |
+-----------+--------------------+-------------------------+-------------+-----------------+-----------+--------------+----------------------------------------------------------------------------------------------------+
| 13 | Customer#000000013 | nsXQu0oVjD7PM659uC3SRSp | 3 | 13-761-547-5974 | 3857.34 | BUILDING | ounts sleep carefully after the close frays. carefully bold notornis use ironic requests. blithely |
| 27 | Customer#000000027 | IS8GIyxpBrLpMT0u7 | 3 | 13-137-193-2709 | 5679.84 | BUILDING | about the carefully ironic pinto beans. accoun |
| 40 | Customer#000000040 | gOnGWAyhSV1ofv | 3 | 13-652-915-8939 | 1335.3 | BUILDING | rges impress after the slyly ironic courts. foxes are. blithely |
+-----------+--------------------+-------------------------+-------------+-----------------+-----------+--------------+----------------------------------------------------------------------------------------------------+
3 rows in set (0.78 sec)
Step 6: Insert data
Locate the customers whose n_name is CANADA and c_mktsegment is BUILDING, sort the records in descending order by c_custkey, and select the first 10 records. Insert the data in the c_custkey, c_address, and c_acctbal columns to the test_insert table in Table Store.
First confirm the number of data records
mysql> select c.c_custkey, c.c_address, c.c_acctbal
from tpch_50x_text.customer c
join tpch_50x_text.nation n
on c.c_nationkey = n.n_nationkey
where n.n_name = 'CANADA'
and c.c_mktsegment = 'BUILDING'
order by c.c_custkey
limit 10;
+-----------+--------------------------------+-----------+
| c_custkey | c_address | c_acctbal |
+-----------+--------------------------------+-----------+
| 13 | nsXQu0oVjD7PM659uC3SRSp | 3857.34 |
| 27 | IS8GIyxpBrLpMT0u7 | 5679.84 |
| 40 | gOnGWAyhSV1ofv | 1335.3 |
| 64 | MbCeGY20kaKK3oalJD,OT | -646.64 |
| 255 | I8Wz9sJBZTnEFG08lhcbfTZq3S | 3196.07 |
| 430 | s2yfPEGGOqHfgkVSs5Rs6 qh,SuVmR | 7905.17 |
| 726 | 4w7DOLtN9Hy,xzZMR | 6253.81 |
| 905 | f iyVEgCU2lZZPCebx5bGp5 | -600.73 |
| 1312 | f5zgMB4MHLMSHaX0tDduHAmVd4 | 9459.5 |
| 1358 | t23gsl4TdVXqTZha DioEHIq5w7y | 5149.23 |
+-----------+--------------------------------+-----------+
10 rows in set (1.09 sec)
Insert data
mysql> insert into etl_ots_test.test_insert (id1_int ,c_address, c_acctbal)
select c.c_custkey, c.c_address, c.c_acctbal
from tpch_50x_text.customer c
join tpch_50x_text.nation n
on c.c_nationkey = n.n_nationkey
where n.n_name = 'CANADA'
and c.c_mktsegment = 'BUILDING'
order by c.c_custkey
limit 10;
+------+
| rows |
+------+
| 10 |
+------+
1 row in set (2.14 sec)
Step 7: Verify the results
mysql> select * from etl_ots_test.test_insert;
+---------+--------------------------------+-----------+
| id1_int | c_address | c_acctbal |
+---------+--------------------------------+-----------+
| 13 | nsXQu0oVjD7PM659uC3SRSp | 3857.34 |
| 27 | IS8GIyxpBrLpMT0u7 | 5679.84 |
| 40 | gOnGWAyhSV1ofv | 1335.3 |
| 64 | MbCeGY20kaKK3oalJD,OT | -646.64 |
| 255 | I8Wz9sJBZTnEFG08lhcbfTZq3S | 3196.07 |
| 430 | s2yfPEGGOqHfgkVSs5Rs6 qh,SuVmR | 7905.17 |
| 726 | 4w7DOLtN9Hy,xzZMR | 6253.81 |
| 905 | f iyVEgCU2lZZPCebx5bGp5 | -600.73 |
| 1312 | f5zgMB4MHLMSHaX0tDduHAmVd4 | 9459.5 |
| 1358 | t23gsl4TdVXqTZha DioEHIq5w7y | 5149.23 |
+---------+--------------------------------+-----------+
10 rows in set (0.27 sec)