背景

mongoDB是近几年迅速崛起的一种文档型数据库,广泛应用于对事务无要求,但是要求较好的开发灵活性,扩展弹性的领域,。

随着企业对数据挖掘需求的增加,用户可能会对存储在mongo中的数据有挖掘需求,但是mongoDB的语法较为单一,不能满足挖掘的需求。

PostgreSQL是起源于伯克利大小的一个开源数据库,已经有20多年的历史,以稳定性,功能强大著称,号称"开源界的Oracle"。

在国内外各个行业都有非常多的用户,如平安银行,邮储银行,中移动,去哪儿,高德,菜鸟,美国宇航局,俄罗斯杜马等等。

PostgreSQL 9.6 新增了基于CPU的并行计算。 20TB以内的OLTP+OLAP的混合场景,PostgreSQL 会是很好的选择。

PostgreSQL的FDW特性,可以允许它连接任何数据源,将外部数据源当成本地源使用。

MongoDB Connector for BI就是PostgreSQL 的FDW衍生的产品。 为mongoDB用户提供丰富的SQL接口。

除了可以连接mongoDB,PostgreSQL FDW还能连接几乎所有数据源,图中没有完全列出。

FDW请参考
http://wiki.postgresql.org/wiki/Fdw

本文将从mongodb用户视角,讲解一下mongodb bi connector的用法。

MongoDB Connector for BI的部署

因为国内下载mongodb-bi的包非常慢,我这里没有验证整个过程,以互联网上一篇文档或蓝本,细化一下整个过程。

OS环境

[root@mongobihost raj]# lsb_release -a
Distributor ID: RedHatEnterpriseServer
Description: Red Hat Enterprise Linux Server release 6.5 (Santiago)
Release: 6.5
[root@mongobihost raj]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.5 (Santiago)

python版本

[root@mongobihost raj]# which python
/usr/bin/python
[root@mongobihost raj]# python -V
Python 2.6.6

下载 mongodb-bi-1.1.3-1-centos6-rpms.tar.bz2, 解压
包含了PostgreSQL, FDW接口以及mongodb schema转换成SQL的工具等。

root@mongobihost bin]# cd /tmp/
[root@mongobihost tmp]# ls -ltr
mongodb-bi-schematools-1.1.3-1.el6.x86_64.rpm
mongodb-bi-libs-1.1.3-1.el6.x86_64.rpm
mongodb-bi-1.1.3-1.el6.x86_64.rpm
mongodb-bi-server-1.1.3-1.el6.x86_64.rpm -- PostgreSQL server
mongodb-bi-contrib-1.1.3-1.el6.x86_64.rpm -- PostgreSQL contrib
mongodb-bi-devel-1.1.3-1.el6.x86_64.rpm -- PostgreSQL include
mongodb-bi-multicorn-1.1.3-1.el6.x86_64.rpm -- PostgreSQL python FDW 开发接口
mongodb-bi-pymongo-1.1.3-1.x86_64.rpm
mongodb-bi-fdw-1.1.3-1.noarch.rpm -- PostgreSQL mongofdw based on mulitcorn
mongodb-bi-1.1.3-1-centos6-rpms.tar.bz2

安装这些 rpm

[root@mongobihost tmp]# rpm -ivh *.rpm --nodeps
Preparing... ########################################### [100%]
package mongodb-bi-libs-1.1.3-1.el6.x86_64 is already installed
package mongodb-bi-1.1.3-1.el6.x86_64 is already installed
package mongodb-bi-devel-1.1.3-1.el6.x86_64 is already installed
package mongodb-bi-server-1.1.3-1.el6.x86_64 is already installed
package mongodb-bi-contrib-1.1.3-1.el6.x86_64 is already installed
package mongodb-bi-schematools-1.1.3-1.el6.x86_64 is already installed
package mongodb-bi-pymongo-1.1.3-1.x86_64 is already installed
package mongodb-bi-fdw-1.1.3-1.noarch is already installed

安装 mongodb-bi-multicorn

[root@mongobihost tmp]# rpm -ivh mongodb-bi-multicorn-1.1.3-1.el6.x86_64 --nodeps
error: open of mongodb-bi-multicorn-1.1.3-1.el6.x86_64 failed: No such file or directory [root@mongobihost tmp]# rpm -ivh mongodb-bi-multicorn-1.1.3-1.el6.x86_64.rpm --nodeps
Preparing... ########################################### [100%]
1:mongodb-bi-multicorn ########################################### [100%]

安装完后,检查python 的collections模块是否正常

NOTE: python Version should be greater than 2.6 - Hence, upgrade it and then install RPMs.

One way to check is :  to start a Python2.6 shell, and confirm that the "collections" module includes the "OrderedDict()" methods. For example:

python
Python 2.6.6 (r266:84292, Sep 4 2013, 07:46:00)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import collections
>>> od = collections.OrderedDict()
>>> od OrderedDict() Ctrl+D to exit..

检查 本地 Mongo

 mongo ${HOST}:${PORT}/admin -u mongoadmin -p $password
MongoDB shell version: 3.2.4
connecting to: mongobihost:27017/admin
Server has startup warnings: 2016-04-01T16:49:54.454-0700 I CONTROL [initandlisten]
MongoDB Enterprise set01:PRIMARY> show dbs
admin 0.000GB
rajdb 1.210GB
abcdeconfig 0.015GB
abcdb 0.166GB
jiradb 0.026GB
local 1.199GB
exit;

创建 mongodb bi 用户
对应的操作是在PostgreSQL 中使用 create server和CREATE USER MAPPING FOR定义foreign server与user mapping的操作。( 指向提供的 mongodb url )
参考 https://docs.mongodb.com/bi-connector/reference/mongobiuser/#bin.mongobiuser

[root@mongobihost bin]# mongobiuser create biuser mongodb://biuser:test@mongobihost.myhost.com:27017/admin

or  

[root@mongobihost bin]# mongobiuser create biuser mongodb://mongobihost.myblog.com:27017/admin
Enter password: 2016-06-17T12:12:15.403-0700 creating user biuser
2016-06-17T12:12:15.408-0700 creating database buses

检查PostgreSQL是否启动
mongo bi connector修改了PostgreSQL中的一些默认选项,例如端口已修改为27032,当然你也可以自己去修改这个端口。
以下是PostgreSQL在unix socket上的监听,监听端口27032,如果你需要监听在IP端口上,需要修改postgresql.conf重启数据库.

[root@mongobihost bin]# netstat -an|grep PG
Active Internet connections (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 1262987 /tmp/.s.PGSQL.27032

查看PostgreSQL 配置文件的位置
其实用 rpm -ql mongodb-bi-server 更好

[root@mongobihost tmp]# find / -name postgresql.conf
/var/lib/pgsql/9.4/data/postgresql.conf

修改监听,在所有的接口上。这样你的BI软件才能通过网络连到PostgreSQL

vi /var/lib/pgsql/9.4/data/postgresql.conf
listen_addresses = '0.0.0.0'

配置PostgreSQL pg_hba.conf,允许所有来源IP访问这个PostgreSQL

[root@mongobihost bin]# vi /var/lib/pgsql/9.4/data/pg_hba.conf
#** Add below content :
# IPv4 local connections:
host all all 0.0.0.0/0 md5

重启postgresql

pg_ctl restart -m fast -D /var/lib/pgsql/9.4/data

使用mongodrdl将需要参与BI分析的collection导出成为创建PostgreSQL 外部表的DDL

mongodrdl -d rajdb -o rajdb.drdl -h mongobihost:27017 -u mongoadmin -p $password --authenticationDatabase admin

Note: 27017 is mongo port
2016-06-17T14:20:15.546-0700 Table "employee", column "sfg.sfgsf" has no types: ignoring column.
2016-06-17T14:20:15.546-0700 Table "employee", column "fgfs.gsdfgf" has no types: ignoring column.
2016-06-17T14:20:15.546-0700 Table "employee", column "fgsf.sgfgs" has no types: ignoring column.
2016-06-17T14:20:15.546-0700 Table "employee", column "sgss.srgs" has no types: ignoring column.
2016-06-17T14:20:16.123-0700 Table "emp_Pack_flat", column "rtgs.comments" has no types: ignoring column.
2016-06-17T14:20:16.972-0700 Table "customer_transaction", column "FValues" is an array that has no types: ignoring column.
2016-06-17T14:20:16.973-0700 Table "customer_transaction_Notes", column "Notes.enumValues" is an array that has no types: ignoring column.
2016-06-17T14:20:16.973-0700 Table "customer_transaction_SiteValues", column "F1z_v.fields.SiteAbbr.enumValues" is an array that has no types: ignoring column.
2016-06-17T14:20:16.973-0700 Table "customer_transaction_URL", column "URL.enumValues" is an array that has no types: ignoring column.
2016-06-17T14:20:16.974-0700 Table "customer_transaction_active", column "F1z_v.fields.active.enumValues" is an array that has no types: ignoring column.
2016-06-17T14:20:16.974-0700 Table "customer_transaction_active", column "colCur.enumValues" is an array that has no types: ignoring column.
2016-06-17T14:20:16.974-0700 Table "customer_transaction_active", column "colDiff.enumValues" is an array that has no types: ignoring column.

使用mongobischema 将DDL导入PostgreSQL

# To import data into BI schema

[root@mongobihost bin]# mongobischema import biuser ./rajdb.drdl
Enter password: 2016-06-17T14:55:02.541-0700 creating table employee
2016-06-17T14:55:02.572-0700 creating table emp_Pac_fla
2016-06-17T14:55:02.579-0700 creating table customer_transaction
2016-06-17T14:55:02.588-0700 creating table customer_transaction_Notes
2016-06-17T14:55:02.597-0700 creating table customer_transaction_SiteVa
2016-06-17T14:55:02.606-0700 creating table customer_transaction_URL
2016-06-17T14:55:02.614-0700 creating table customer_transaction_active # to look at the tables in the BI schema, run below stmt.

检查已导入的外部表

[root@mongobihost]# mongobischema list biuser
Enter password: employee customer_transaction customer_transaction_Notes customer_transaction_SiteVa customer_transaction_URL
customer_transaction_active

如何重启PostgreSQL,也可直接使用pg_ctl。

If you need to restart the BI Connector, then
sudo service postgresql-9.4 stop
sudo service postgresql-9.4 start or pg_ctl restart -m fast -D /var/lib/pgsql/9.4/data

列出bi用户,也可以直接用PostgreSQL中的SQL或视图查看

# mongobiuser list

检查连接PostgreSQL是否正常

to check if things are okay on postgre Sql..

 psql -h localhost -p 27032 -U biuser
Password for user biuser:
psql (9.4.5 MongoDB BI Connector 1.1.3)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help. biuser=> \d
List of relations
Schema | Name | Type | Owner
--------+-------------------------------------------------------------------------------+---------------+--------
public | customer_transaction | view | biuser
public | customer_transaction_Notes | foreign table | biuser
public | customer_transaction_SiteVa | view | biuser biuser=> select * from "customer_transaction" limit 1;

现在你可以用BI软件连接PostgreSQL来分析存储在mongoDB的数据了 。

小结

  • 其实创建fdw没有这么麻烦,几条SQL语句就搞定了。 mongodb只是出于mongodb用户可能不熟悉PostgreSQL的考虑,把操作都封装成了命令,便于mongodb的用户使用。
  • 如果用户未来又新增了需要分析的collection,使用导出和导入的步骤即可。
  • PostgreSQL作为mongodb bi connector的作用是扩充mongoDB的SQL功能,并不存储数据,数据都是在mongoDB中的。 如果涉及到比较复杂的运算无法下推到mongodb,则会将数据提取到PostgreSQL本地进行运算(过程自动,对用户透明)。但是如果数据量非常庞大(例如每次分析都超过百GB需要提取)会被数据在网络传输的时间拖累。
  • 如果数据量很庞大,建议还是讲mongoDB的数据导出到PostgreSQL或者Greenplum,直接在PostgreSQL或Greenplum中分析。效率会更高。
  • Greenplum 是基于PostgreSQL的一个MPP OLAP产品,在OLAP领域有着非常好的口碑,国内外的用户群也非常多。
  • 用户群覆盖了互联网,金融、物流、政府部门等各大行业,最大的集群规模有超过1000 segment的。
    20TB ~ 1PB 纯OLAP场景 Greenplum 会是更好的选择。
  • 如何将mongoDB的数据导入PostgreSQL或Greenplum , 参考 https://yq.aliyun.com/articles/31632 , 或者直接使用SQL导入. create table local_table(表结构); insert into local_table select * from 外部表;

参考

扩展阅读

mongoDB BI 分析利器 - PostgreSQL FDW (MongoDB Connector for BI)的更多相关文章

  1. MongoDB查询分析

    MongoDB 查询分析可以确保我们建立的索引是否有效,是查询语句性能分析的重要工具.MongoDB 查询分析常用函数有:explain() 和 hint(). 1. explain(): 提供查询信 ...

  2. MongoDB 查询分析

    MongoDB 查询分析可以确保我们建议的索引是否有效,是查询语句性能分析的重要工具. MongoDB 查询分析常用函数有:explain() 和 hint(). 使用 explain() expla ...

  3. MySQL,Oracle,PostgreSQL,mongoDB,Hive, SAP HANA 数据库web维护客户端管理工具

    TreeDMS数据库管理系统使用JAVA开发,采用稳定通用的springMVC +JDBC架构,实现基于WEB方式对 MySQL,Oracle,PostgreSQL,mongoDB ,Hive, SA ...

  4. Covering Indexes in MySQL, PostgreSQL, and MongoDB

    Covering Indexes in MySQL, PostgreSQL, and MongoDB - Orange Matter https://orangematter.solarwinds.c ...

  5. Mongodb的入门(8)mongodb事物分析

    老生常谈:<在前面博客中也介绍过> mongodb官网:https://docs.mongodb.com/manual/introduction/ mongodb:官网上是这样定义的Mon ...

  6. 基于C#的MongoDB数据库开发应用(1)--MongoDB数据库的基础知识和使用

    在花了不少时间研究学习了MongoDB数据库的相关知识,以及利用C#对MongoDB数据库的封装.测试应用后,决定花一些时间来总结一下最近的研究心得,把这个数据库的应用单独作为一个系列来介绍,希望从各 ...

  7. Power BI连接至Mogo Altas Connector For BI

    我需要使用Power BI连接至Connector For BI ,现在Connect For BI存放在Mongo Atlas中,详细的来自于官方文档,https://docs.atlas.mong ...

  8. 【译】为什么永远都不要使用MongoDB Why You Should Never Use MongoDB

    背景 最近在学习DDIA(Designing Data-Intensive Applications)这本分布式领域非常急经典的入门书籍,里面第二章<数据模型与查询语言>,强调了对一对多. ...

  9. Mongodb Manual阅读笔记:CH2 Mongodb CRUD 操作

    2 Mongodb CRUD 操作 Mongodb Manual阅读笔记:CH2 Mongodb CRUD 操作Mongodb Manual阅读笔记:CH3 数据模型(Data Models)Mong ...

随机推荐

  1. 关于云主机Thinkphp框架Session跨页失效的问题

    在网站部署到云主机之后,前台一直能够正常显示,后台确登录不上去,验证码也无法显示,研究半天,才确定是Session跨页传递失效的问题.找网上各种解决方法,都是关于Php.ini文件的设置,可又解决不了 ...

  2. 日志一直打印 DEBUG o.s.amqp.rabbit.listener.BlockingQueueConsumer

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <logger na ...

  3. 数组合并去重Array.from

    function uniqArr(){ if(arguments.length == 0) return []; var arr = arguments[0]; for(var i=1;i<ar ...

  4. 剑指offer——面试题16:数值的整数次方

    // 面试题16:数值的整数次方 // 题目:实现函数double Power(double base, int exponent),求base的exponent // 次方.不得使用库函数,同时不需 ...

  5. Sql2008R2 日志无法收缩解决方案

    在网上查了二天资料,终于找到个解决了这个问题的方案,记录下来.方便下次处理.    解决方案转贴自:  https://blog.csdn.net/kk185800961/article/detail ...

  6. centos 7 下安装nginx-1.15.7

    安装所需环境 Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Windows 版本,本篇则使用 CentOS 7 作为安装环境. 一. gcc 安装安装 nginx 需要先 ...

  7. ActivityManager的代理模式

    从官方文档的介绍可以看到ActivityManager的作用: 是与系统所有正在运行着的Acitivity进行交互,对系统所有运行中的Activity相关信息(Task,Memory,Service, ...

  8. CSAPP阅读笔记-汇编语言初探(控制类指令)-来自第三章3.6的笔记-P135-P163

    1.正溢出与负溢出: 首先,一个正数与一个负数相加,不可能溢出,因为结果的绝对值一定小于两个加数的绝对值,既然两个加数能合理表示出来,结果一定也能合理表示出来. 其次,正溢出是由于两个很大的正数相加, ...

  9. 判断title(title_is)

    判断 title 获取页面 title 的方法可以直接用 driver.title 获取到,然后也可以把获取到的结果用做断言.本篇介绍另外一种方法去判断页面 title 是否与期望结果一种,用到上一篇 ...

  10. Git学习系列之Git 的优势有哪些?

    Git 的优势主要有: 1.更方便的 Merge 分布式管理必然导致大量的 Branch 和 Merge 操作.因此分布式版本控制系统都特别注意这方面.在传统的 CVS 里面制作 Branch 和 M ...