本文以在DLA中,读取OSS数据(customer、nation表),并通过INSERT…SELECT将符合条件的数据写入Table Store为例,介绍如何通过DLA向Table Store中写入数据。
操作步骤
步骤一:创建OSS Schema
CREATE DATABASE tpch_50x_text
WITH DBPROPERTIES (
catalog = 'oss',
location = 'oss://${您的bucket}/datasets/tpch/50x/text_date/'
)
步骤二:创建OSS表
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://${您的bucket}/datasets/tpch/50x/text_date/customer_text'
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://${您的bucket}/datasets/tpch/50x/text_date/nation_text'
步骤三:创建Table Store Schema
CREATE DATABASE etl_ots_test
WITH DBPROPERTIES (
catalog = 'ots',
location = 'https://${您的instance}.cn-shanghai.ots-internal.aliyuncs.com',
instance = '${您的instance}'
)
步骤四:创建Table Store表test_insert
CREATE EXTERNAL TABLE test_insert (
id1_int int NOT NULL COMMENT '客户id主键',
c_address varchar(20) NULL COMMENT '客户的地址',
c_acctbal double NULL COMMENT '客户的account balance',
PRIMARY KEY (`id1_int`)
)
步骤五:查询数据
mysql> select * from etl_ots_test.test_insert;
Empty set (0.31 sec)
查询nation表数据,其中CANADA的nationkey是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)
查询customer表数据,查询条件为nationkey=3以及c_mktsegment=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)
步骤六:导入数据
找到n_name为CANADA、c_mktsegment为BUILDING的客户,然后对c_custkey降序排序,选择前10条数据,将其对应的c_custkey、c_address、c_acctbal三列导入到Table Store的test_insert表中。
先查询数据,确认有几条数据
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)
开始导入数据
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)
步骤七:验证结果
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)