实战课堂 | 让大数据分析更简单,4步教你玩转MongoDB BI Connector mongodb
程序员文章站
2022-03-21 11:08:31
...
MongoDB使用BI Connector支持BI组件直接使用SQL或ODBC数据源方式直接访问MongoDB,在早期MongoDB直接使用Postgresql FDW实现 SQL到MQL的转换,后来实现更加轻量级的mongosqld支持BI工具的连接。
![e411f1b8264781f0e6fcccc1d60ecaf3c8ea61ae.png](https://ucc.alicdn.com/pic/developer-ecology/7ad28347e38b46b0b5e44b4a7ac576f7.png)
安装 BI Connector
---------------
参考 **Install BI Connector**
[https://docs.mongodb.com/bi-connector/master/installation/](https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fdocs.mongodb.com%2Fbi-connector%2Fmaster%2Finstallation%2F)
```
wget https://info-mongodb-com.s3.amazonaws.com/mongodb-bi/v2/mongodb-bi-linux-x86_64-rhel70-v2.12.0.tgz
$tar xvf mongodb-bi-linux-x86_64-rhel70-v2.12.0.tgz
mongodb-bi-linux-x86_64-rhel70-v2.12.0/LICENSE
mongodb-bi-linux-x86_64-rhel70-v2.12.0/README
mongodb-bi-linux-x86_64-rhel70-v2.12.0/THIRD-PARTY-NOTICES
mongodb-bi-linux-x86_64-rhel70-v2.12.0/example-mongosqld-config.yml
mongodb-bi-linux-x86_64-rhel70-v2.12.0/bin/mongosqld
mongodb-bi-linux-x86_64-rhel70-v2.12.0/bin/mongodrdl
mongodb-bi-linux-x86_64-rhel70-v2.12.0/bin/mongotranslate
```
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
* mongosqld 接受 SQL 查询,并将请求发到 MongoDB Server,是 BI Connector 的核心
* mongodrdl 工具生成数据库 schema 信息,用于服务 BI SQL 查询
* mongotranslate 工具将 SQL 查询转换为 MongoDB Aggregation Pipeline
启动 mongosqld
------------
参考 Lauch BI Connector
[https://docs.mongodb.com/bi-connector/current/launch/](https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fdocs.mongodb.com%2Fbi-connector%2Fcurrent%2Flaunch%2F)
```
mongodb-bi-linux-x86_64-rhel70-v2.12.0/bin/mongosqld --addr 127.0.0.1:3307 --mongo-uri 127.0.0.1:9555
```
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
\--addr 指定 mongosqld 监听的地址
\--mongo-uri 指定连接的 MongoDB Server 地址
默认情况下,mongosqld 自动会分析目标 MongoDB Server 里数据的 Schema,并缓存在内存,我们也可以直接在启动时指定 schema 影射关系。schema 也可以直接 mongodrdl 工具来生成,指定集合,可以将集合里的字段 shema 信息导出。
```
$./bin/mongodrdl --uri=mongodb://127.0.0.1:9555/test -c coll01
schema:
- db: test
tables:
- table: coll01
collection: coll01
pipeline: []
columns:
- Name: _id
MongoType: float64
SqlName: _id
SqlType: float
- Name: qty
MongoType: float64
SqlName: qty
SqlType: float
- Name: type
MongoType: string
SqlName: type
SqlType: varchar
```
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
使用 MySQL 客户端连接 mongosqld
------------------------
mongosqld 可直接支持 MySQL 客户端访问,还可以通过 Excel、Access、Tableau等BI工具连接
[https://docs.mongodb.com/bi-connector/current/client-applications/](https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fdocs.mongodb.com%2Fbi-connector%2Fcurrent%2Fclient-applications%2F)
```
mysql --protocol=tcp --port=3307
mysql> use test
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| coll |
| coll01 |
| coll02 |
| inventory |
| myCollection |
| yourCollection |
+----------------+
6 rows in set (0.00 sec)
mysql> select * from coll01;
+------+------+--------+
| _id | qty | type |
+------+------+--------+
| 1 | 5 | apple |
| 2 | 10 | orange |
| 3 | 15 | banana |
+------+------+--------+
3 rows in set (0.00 sec)
// 对照 MongoDB 数据库里的原始数据
mongo --port
mymongo:PRIMARY> use test
switched to db test
mymongo:PRIMARY> show tables;
coll
coll01
coll02
inventory
myCollection
yourCollection
mymongo:PRIMARY> db.coll01.find()
{ "_id" : 1, "type" : "apple", "qty" : 5 }
{ "_id" : 2, "type" : "orange", "qty" : 10 }
{ "_id" : 3, "type" : "banana", "qty" : 15 }
```
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
SQL 转 Aggregation
-----------------
比如要将针对 test.coll01 的 SQL 查询转换为 MongoDB Aggregation Pipeline,需要先通过 mongodrdl 分析 schema,然后使用 mongotranslate 工具来转换
```
// 导出分析的 shema 文件
$./bin/mongodrdl --uri=mongodb://127.0.0.1:9555/test -c coll01 > coll01.schema
// SQL 转换为 Aggregation
$./bin/mongotranslate --query "select * from test.coll01" --schema coll01.schema
[
{"$project": {"test_DOT_coll01_DOT__id": "$_id","test_DOT_coll01_DOT_qty": "$qty","test_DOT_coll01_DOT_type": "$type","_id": NumberInt("0")}},
]
```
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
![e411f1b8264781f0e6fcccc1d60ecaf3c8ea61ae.png](https://ucc.alicdn.com/pic/developer-ecology/7ad28347e38b46b0b5e44b4a7ac576f7.png)
安装 BI Connector
---------------
参考 **Install BI Connector**
[https://docs.mongodb.com/bi-connector/master/installation/](https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fdocs.mongodb.com%2Fbi-connector%2Fmaster%2Finstallation%2F)
```
wget https://info-mongodb-com.s3.amazonaws.com/mongodb-bi/v2/mongodb-bi-linux-x86_64-rhel70-v2.12.0.tgz
$tar xvf mongodb-bi-linux-x86_64-rhel70-v2.12.0.tgz
mongodb-bi-linux-x86_64-rhel70-v2.12.0/LICENSE
mongodb-bi-linux-x86_64-rhel70-v2.12.0/README
mongodb-bi-linux-x86_64-rhel70-v2.12.0/THIRD-PARTY-NOTICES
mongodb-bi-linux-x86_64-rhel70-v2.12.0/example-mongosqld-config.yml
mongodb-bi-linux-x86_64-rhel70-v2.12.0/bin/mongosqld
mongodb-bi-linux-x86_64-rhel70-v2.12.0/bin/mongodrdl
mongodb-bi-linux-x86_64-rhel70-v2.12.0/bin/mongotranslate
```
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
* mongosqld 接受 SQL 查询,并将请求发到 MongoDB Server,是 BI Connector 的核心
* mongodrdl 工具生成数据库 schema 信息,用于服务 BI SQL 查询
* mongotranslate 工具将 SQL 查询转换为 MongoDB Aggregation Pipeline
启动 mongosqld
------------
参考 Lauch BI Connector
[https://docs.mongodb.com/bi-connector/current/launch/](https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fdocs.mongodb.com%2Fbi-connector%2Fcurrent%2Flaunch%2F)
```
mongodb-bi-linux-x86_64-rhel70-v2.12.0/bin/mongosqld --addr 127.0.0.1:3307 --mongo-uri 127.0.0.1:9555
```
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
\--addr 指定 mongosqld 监听的地址
\--mongo-uri 指定连接的 MongoDB Server 地址
默认情况下,mongosqld 自动会分析目标 MongoDB Server 里数据的 Schema,并缓存在内存,我们也可以直接在启动时指定 schema 影射关系。schema 也可以直接 mongodrdl 工具来生成,指定集合,可以将集合里的字段 shema 信息导出。
```
$./bin/mongodrdl --uri=mongodb://127.0.0.1:9555/test -c coll01
schema:
- db: test
tables:
- table: coll01
collection: coll01
pipeline: []
columns:
- Name: _id
MongoType: float64
SqlName: _id
SqlType: float
- Name: qty
MongoType: float64
SqlName: qty
SqlType: float
- Name: type
MongoType: string
SqlName: type
SqlType: varchar
```
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
使用 MySQL 客户端连接 mongosqld
------------------------
mongosqld 可直接支持 MySQL 客户端访问,还可以通过 Excel、Access、Tableau等BI工具连接
[https://docs.mongodb.com/bi-connector/current/client-applications/](https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fdocs.mongodb.com%2Fbi-connector%2Fcurrent%2Fclient-applications%2F)
```
mysql --protocol=tcp --port=3307
mysql> use test
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| coll |
| coll01 |
| coll02 |
| inventory |
| myCollection |
| yourCollection |
+----------------+
6 rows in set (0.00 sec)
mysql> select * from coll01;
+------+------+--------+
| _id | qty | type |
+------+------+--------+
| 1 | 5 | apple |
| 2 | 10 | orange |
| 3 | 15 | banana |
+------+------+--------+
3 rows in set (0.00 sec)
// 对照 MongoDB 数据库里的原始数据
mongo --port
mymongo:PRIMARY> use test
switched to db test
mymongo:PRIMARY> show tables;
coll
coll01
coll02
inventory
myCollection
yourCollection
mymongo:PRIMARY> db.coll01.find()
{ "_id" : 1, "type" : "apple", "qty" : 5 }
{ "_id" : 2, "type" : "orange", "qty" : 10 }
{ "_id" : 3, "type" : "banana", "qty" : 15 }
```
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
SQL 转 Aggregation
-----------------
比如要将针对 test.coll01 的 SQL 查询转换为 MongoDB Aggregation Pipeline,需要先通过 mongodrdl 分析 schema,然后使用 mongotranslate 工具来转换
```
// 导出分析的 shema 文件
$./bin/mongodrdl --uri=mongodb://127.0.0.1:9555/test -c coll01 > coll01.schema
// SQL 转换为 Aggregation
$./bin/mongotranslate --query "select * from test.coll01" --schema coll01.schema
[
{"$project": {"test_DOT_coll01_DOT__id": "$_id","test_DOT_coll01_DOT_qty": "$qty","test_DOT_coll01_DOT_type": "$type","_id": NumberInt("0")}},
]
```
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")