四步教你玩转 MongoDB BI Connector

本文来自于【阿里云官方镜像站:https://developer.aliyun.com/mirror/?utm_content=g_1000307095 】

原文链接:https://developer.aliyun.com/article/740875?spm=a2c6h.12873581.0.0.484f7e46JZ6t8J

MongoDB使用BI Connector支持BI组件直接使用SQL或ODBC数据源方式直接访问MongoDB,在早期MongoDB直接使用Postgresql FDW实现 SQL到MQL的转换,后来实现更加轻量级的mongosqld支持BI工具的连接。

安装 BI Connector

参考 Install BI Connector
https://docs.mongodb.com/bi-connector/master/installation/

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
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/

mongodb-bi-linux-x86_64-rhel70-v2.12.0/bin/mongosqld --addr 127.0.0.1:3307 --mongo-uri 127.0.0.1:9555

--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

使用 MySQL 客户端连接 mongosqld

mongosqld 可直接支持 MySQL 客户端访问,还可以通过 Excel、Access、Tableau等BI工具连接
https://docs.mongodb.com/bi-connector/current/client-applications/

mysql --protocol=tcp --port=3307
mysql> use test
Database changedmysql> 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 }

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")}},

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值