MongoDB使用BI Connector支持BI組件直接使用SQL或ODBC數據源方式直接訪問MongoDB,在早期MongoDB直接使用Postgresql FDW實現 SQL到MQL的轉換,後來實現更加輕量級的mongosqld支持BI工具的鏈接。mysql
參考 Install BI Connector
https://docs.mongodb.com/bi-connector/master/installation/linux
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
參考 Lauch BI Connector
https://docs.mongodb.com/bi-connector/current/launch/sql
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 信息導出。mongodb
$./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
mongosqld 可直接支持 MySQL 客戶端訪問,還能夠經過 Excel、Access、Tableau等BI工具鏈接
https://docs.mongodb.com/bi-connector/current/client-applications/數據庫
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 }
好比要將針對 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")}}, ]
雙12來襲!500元淘寶紅包、iPhone11等你拿https://www.aliyun.com/1212/2019/home?utm_content=g_1000092611app
本文做者:Roin123tcp
閱讀原文工具
本文爲雲棲社區原創內容,未經容許不得轉載。url