教程:使用Data Lake Analytics读/写MongoDB数据

Data Lake Analytics 作为云上数据处理的枢纽,最近加入了对于MongoDB 的支持, 这篇教程带你玩转 DLA 的 MongoDB 支持。

创建数据库

在 DLA 里面创建一个底层映射到 MongoDB 的外表的语法如下:

CREATE DATABASE `mongo_test`
WITH DBPROPERTIES (
    catalog = 'mongodb',
    location = 'mongodb://<your-user-name>:<your-password>@dds-bp1694axxxxxxxx.mongodb.rds.aliyuncs.com:3717,dds-bp1694ayyyyyyyy.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=zzzzz',
    database = 'mongo_test',
    vpc_id = 'vpc-aaaaaaa'
);

这里要特别说明一下这个 location 属性,如果对 MongoDB 不熟悉的同学有可能看不明白,这个实际上是MongoDB的 ConnectionString, 里面包含了你的用户名、密码、要连接的MongoDB的地址、使用的认证数据库等等,不过不用担心,这么长的ConnectionString你不需要手动去拼出来,直接从阿里云MongoDB的控制台上可以直接复制过来这个信息:

Connection String

因为信息安全的原因,密码被显示成星号,这里大家要替换成真实的密码。另外底层实际连接的数据库是通过 database 这个属性来指定的。

跟普通的建表不同的是这里多了两个属性: VPC_IDINSTANCE_IDVPC_ID 是你的 MongoDB 所在VPC的ID, 如下图所示:

VPC ID

INSTANCE_ID 则是你的 MongoDB 实例ID, 在 MongoDB 的详情页面可以找到:

INSTANCE ID

建表需要这两个额外信息是因为现在用户的 MongoDB 数据库都是处于用户自己的VPC内部,默认情况下 DLA 是访问不了用户 VPC 里面的资源的,为了让DLA能够访问到用户RDS里面的数据,我们需要利用阿里云的VPC反向访问技术。

权限声明: 当您通过上述方式建库,就视为您同意我们利用VPC反向访问的技术去读写您的 MongoDB 。

另外您还需要把 100.104.0.0/16 加入你的 MongoDB 的白名单列表,这是我们VPC反向访问的IP地段,如下图:

ip_whitelist

创建表

数据库建完之后,我们可以建表了,我们先在你的 MongoDB 里面建立如下的 person 表用来做测试, 因为MongoDB是没有schema信息的,我们必须往里面插入数据才能生效,所以我们插入一些测试数据:

db.person.insert({id:1, name: "james", age: 10, create_time: new Date()})
db.person.insert({id:2, name: "bond", age: 20, create_time: new Date()});
db.person.insert({id:3, name: "lily", age: 30, create_time: new Date()});
db.person.insert({id:4, name: "lucy", age: 20, create_time: new Date()});

然后就可以在 DLA 的数据库里面建立相应的映射表了:

create external table dla_person (
    id int,
    title varchar(127),
    age int,
    create_time timestamp
)TBLPROPERTIES (
    TABLE_MAPPING = 'person',
    COLUMN_MAPPING = 'title,name'
);

这里我们展示了DLA的两个特性:

  • 通过 TABLE_MAPPING 我们可以让DLA层面的表名跟底层的表名不一样。

    • 你可以省略这个设置,那么表名就跟底层一样。
  • 通过 COLUMN_MAPPING 我们可以让DLA层面的column名字也跟底层不一样。

    • 同样,你也可以省略这个配置。

如果 TABLE_MAPPINGCOLUMN_MAPPING 你都不需要配置,那么整个 TBLPROPERTIES 都可以去掉。

这样我们就可以通过MySQL客户端连接到 DLA 数据库上面,就可以对 MongoDB 数据库里面的数据进行查询了:

mysql> select * from dla_person;
+------+-------+------+-------------------------+
| id   | title | age  | create_time             |
+------+-------+------+-------------------------+
|    1 | james |   10 | 2018-12-14 14:22:54.369 |
|    2 | bond  |   20 | 2018-12-14 14:23:48.527 |
|    3 | lily  |   30 | 2018-12-14 14:23:48.962 |
|    4 | lucy  |   20 | 2018-12-14 14:23:49.396 |
+------+-------+------+-------------------------+
4 rows in set (0.92 sec)

熟悉SQL的同学一定觉得很爽吧,可以去熟悉的SQL语法去操作MongoDB数据库。

ETL: 把数据从OSS里面清洗出来写入MongoDB

很多场景下,在我们对存储在OSS/OTS上的大数据进行分析,分析完成之后把结果数据回写到 MongoDB 里面供前台业务使用。这种场景在DLA里面非常容易实现,还是举前面 dla_person 表例子,下面的语句把 oss_db 里面 customer 的前十条记录进行了一些转换然后插入了我们的 mongo_test.dla_person 表:

mysql> insert into mongo_test.dla_person
    -> select c_custkey, c_name, c_custkey + 20, now() from oss_db.customer limit 10;

+------+
| rows |
+------+
|   10 |
+------+
1 row in set (3.72 sec)

mysql> select * from mongo_test.dla_person;
+------+--------------------+------+-------------------------+
| id   | title              | age  | create_time             |
+------+--------------------+------+-------------------------+
|    1 | james              |   10 | 2018-12-14 14:22:54.369 |
|    2 | bond               |   20 | 2018-12-14 14:23:48.527 |
|    3 | lily               |   30 | 2018-12-14 14:23:48.962 |
|    4 | lucy               |   20 | 2018-12-14 14:23:49.396 |
|    1 | Customer#000000001 |   21 | 2018-12-20 10:15:56.629 |
|    3 | Customer#000000003 |   23 | 2018-12-20 10:15:56.629 |
|    5 | Customer#000000005 |   25 | 2018-12-20 10:15:56.629 |
|    7 | Customer#000000007 |   27 | 2018-12-20 10:15:56.629 |
|    9 | Customer#000000009 |   29 | 2018-12-20 10:15:56.629 |
|    2 | Customer#000000002 |   22 | 2018-12-20 10:15:56.629 |
|    4 | Customer#000000004 |   24 | 2018-12-20 10:15:56.629 |
|    6 | Customer#000000006 |   26 | 2018-12-20 10:15:56.629 |
|    8 | Customer#000000008 |   28 | 2018-12-20 10:15:56.629 |
|   10 | Customer#000000010 |   30 | 2018-12-20 10:15:56.629 |
+------+--------------------+------+-------------------------+
14 rows in set (0.16 sec)

总结

我们今天介绍了DLA对于MongoDB的支持,目前DLA支持的数据源已经包括: OSS, OTS, RDS(MySQL, SQLServer, Postgres), MongoDB, 数据可以在这些数据源之间进行联合JOIN、流转,更多详细的介绍可以直接去我们的官网试用。

Happy DLAing!

猜你喜欢

转载自yq.aliyun.com/articles/680758