Python编写Oracle和Elasticsearch数据同步脚本


一、版本

python版本 x64 2.7.12 

Oracle(x64 12.1.0.2.0)和Elasticsearch(2.2.0)

python编辑器 PyCharm

下载安装请选择适合自己机器的版本

二、下载模块
通过官网下载和安装cx_Oracle和pyes模块,分别用于操作Oracle数据库和ES。安装fcntl模块用于解决python脚本单例执行问题。
如果是远程连接数据库和ES,请一定注意安装的模块或包版本。务必选择相应的版本,不然会遇到问题。

三、安装过程中会遇到的问题

cx_Oracle在本地安装过程中出现的一些问题:
1、安装c++for python的环境
2、安装Oracle数据库(或者安装API接口中需要的文件而不必下载配置整个oracle环境)
3、打开数据库工具 oracle SQL developor 按要求创建连接,并新建用户(创建数据库用户名时以c##开头,不然会提示)
4、oracle连接不上远程的服务器,检查版本是否匹配

fcntl在windows上安装时出现的问题:
1、用pip install fcntl 报错:indentationerror: unexpected indent(模块版本有问题)

四、源码

# -*- coding: utf-8 -*-
"""
作者:陈龙
日期:2016-7-22
功能:oracle数据库到ES的数据同步
"""
import os
import sys
import datetime, time
# import fcntl
import threading
import pyes  # 引入pyes模块,ES接口
import cx_Oracle  # 引入cx_Oracle模块,Oracle接口

os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'  # 中文编码
reload(sys)  # 默认编码设置为utf-8
sys.setdefaultencoding('utf-8')

# 创建ES连接 并返回连接参数
def connect_ES(addr):
    try:
        global conn
        conn = pyes.ES(addr)  # 链接ES '127.0.0.1:9200'
        print 'ES连接成功'
        return conn
    except:
        print 'ES连接错误'
        pass

# 创建ES映射mapping 注意各各个字段的类型
def create_ESmapping():
    global spiderInfo_mapping, involveVideo_mapping, involveCeefax_mapping,keyWord_mapping,sensitiveWord_mapping
    spiderInfo_mapping = {'tableName': {'index': 'not_analyzed', 'type': 'string'},
                          'tableId': {'index': 'not_analyzed', 'type': 'integer'},
                          'title': {'index': 'analyzed', 'type': 'string'},
                          'author': {'index': 'not_analyzed', 'type': 'string'},
                          'content': {'index': 'analyzed', 'type': 'string'},
                          'publishTime': {'index': 'not_analyzed', 'type': 'string'},
                          'browseNum': {'index': 'not_analyzed', 'type': 'integer'},
                          'commentNum': {'index': 'not_analyzed', 'type': 'integer'},
                          'dataType': {'index': 'not_analyzed', 'type': 'integer'}}  # 除去涉我部分内容的ES映射结构
    involveVideo_mapping = {'tableName': {'index': 'not_analyzed', 'type': 'string'},
                            'tableId': {'index': 'not_analyzed', 'type': 'integer'},
                            'title': {'index': 'analyzed', 'type': 'string'},
                            'author': {'index': 'not_analyzed', 'type': 'string'},
                            'summary': {'index': 'analyzed', 'type': 'string'},
                            'publishTime': {'index': 'not_analyzed', 'type': 'string'},
                            'url': {'index': 'not_analyzed', 'type': 'string'},
                            'imgUrl': {'index': 'not_analyzed', 'type': 'string'},
                            'ranking': {'index': 'not_analyzed', 'type': 'integer'},
                            'playNum': {'index': 'not_analyzed', 'type': 'integer'},
                            'dataType': {'index': 'not_analyzed', 'type': 'integer'}}  # 涉我视音频内容的ES映射结构
    involveCeefax_mapping = {'tableName': {'index': 'not_analyzed', 'type': 'string'},
                            'tableId': {'index': 'not_analyzed', 'type': 'integer'},
                            'title': {'index': 'analyzed', 'type': 'string'},
                            'author': {'index': 'not_analyzed', 'type': 'string'},
                            'content': {'index': 'analyzed', 'type': 'string'},
                            'publishTime': {'index': 'not_analyzed', 'type': 'string'},
                            'keyWords': {'index': 'not_analyzed', 'type': 'string'},
                            'popularity': {'index': 'not_analyzed', 'type': 'integer'},
                            'url': {'index': 'not_analyzed', 'type': 'string'},
                            'dataType': {'index': 'not_analyzed', 'type': 'integer'}}  # 涉我图文资讯内容的ES映射结构
    keyWord_mapping = {'id':{'index': 'not_analyzed', 'type': 'integer'},
                      'keywords':{'index': 'not_analyzed', 'type': 'string'}}
    sensitiveWord_mapping = {'id':{'index': 'not_analyzed', 'type': 'integer'},
                            'sensitiveType':{'index': 'not_analyzed', 'type': 'string'},
                            'sensitiveTopic': {'index': 'not_analyzed', 'type': 'string'},
                            'sensitiveWords': {'index': 'not_analyzed', 'type': 'string'}}

# 创建ES相关索引和索引下的type
def create_ESindex(ES_index, index_type1,index_type2,index_type3,index_type4,index_type5):

    if conn.indices.exists_index(ES_index):
        pass
    else:
        conn.indices.create_index(ES_index)  # 如果所有Str不存在,则创建Str索引
        create_ESmapping()
        conn.indices.put_mapping(index_type1, {'properties': spiderInfo_mapping},[ES_index])  # 在索引pom下创建spiderInfo的_type  "spiderInfo"
        conn.indices.put_mapping(index_type2, {'properties': involveVideo_mapping},[ES_index])  # 在索引pom下创建involveVideo的_type  "involveVideo"
        conn.indices.put_mapping(index_type3, {'properties': involveCeefax_mapping},[ES_index])  # 在索引pom下创建involveCeefax的_type  "involveCeefax"
        conn.indices.put_mapping(index_type4, {'properties': keyWord_mapping}, [ES_index])
        conn.indices.put_mapping(index_type5, {'properties': sensitiveWord_mapping}, [ES_index])
    # conn.ensure_index

# 创建数据库连接 并返回连接参数
def connect_Oracle(name, password, address):
    try:
        global conn1
        # conn1 = cx_Oracle.connect('c##chenlong','1234567890','localhost:1521/ORCL') #链接本地数据库
        conn1 = cx_Oracle.connect(name, password, address)  # 链接远程数据库 "pom","Bohui@123","172.17.7.118:1521/ORCL"
        print 'Oracle
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值