python读取MySQL插入ES

注释:1.MySQL表中字段类型需和es字段相互对应
2.日期转为字符串且“-”需要换为“\”

# -*- coding: utf-8 -*-

"""
Create by Mr.Hao on 2019/5/28.

"""
import sys
reload(sys)
sys.setdefaultencoding('utf8')

import pymysql
from pymysql.cursors import DictCursor
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk


class FinanceStorage(object):

    def __init__(self, *args, **kwargs):
        super(FinanceStorage, self).__init__(*args, **kwargs)

    def connection(self):
        return pymysql.connect(host='127.0.0.1',
                                user='root',
                                passwd="",
                                db='bdp_spider')

    def get_data(self):
        conn = self.connection()
        cursor = conn.cursor(DictCursor)
        sql = "SELECT * FROM media_consume_table;"
        cursor.execute(sql)
        result = cursor.fetchall()
        cursor.close()
        conn.close()
        return result


class ElasticObj(object):

    def __init__(self, index_name,index_type, ip ="127.0.0.1"):
        '''

        :param index_name: 索引名称
        :param index_type: 索引类型
        '''
        self.index_name =index_name
        self.index_type = index_type
        self.es = Elasticsearch([ip],http_auth=('elastic', 'password'),port=9200)

    def create_index(self, index_name='data', index_type="media_consume_table"):
        _index_mappings = {
            "mappings": {
                index_type: {  # 相当于数据库中的表名
                    "properties": {
                        "id": {
                          "type": "long",
                          "store": True,
                        },
                        "dt": {
                            "type": "date",
                            "store": True,
                            "format": "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
                        },
                        "product": {
                            "type": "keyword",
                            "index": True,
                        },
                        "meida": {
                            "type": "keyword",
                            "index": True,
                        },
                        "meida_sub_categorey": {
                            "type": "keyword",
                            "index": True,
                        },
                        "account": {
                            "type": "keyword",
                            "index": True,
                        },
                        "ad_campaign": {
                          "type": "text",
                          # "analyzer": "ik_max_word",
                          # "search_analyzer": "ik_max_word"
                        },
                        "ad_type": {
                            "type": "keyword",
                            "index": True,
                        },
                        "consume": {
                            "type": "keyword",
                            "store": True,
                        },
                        "shows": {
                            "type": "keyword",
                            "store": True,
                        },
                        "clicks": {
                            "type": "keyword",
                            "store": True,
                        },
                        "pay_downloads": {
                            "type": "keyword",
                            "store": True,
                        },
                        "publish_time": {
                              "type": "date",
                              "store": True,
                              "format": "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
                        },
                        "update_time": {
                              "type": "date",
                              "store": True,
                              "format": "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
                        },
                        "ad_group": {
                            "type": "string",  # 字符串
                            "store": True
                        },
                        "ad_case": {
                            "type": "string",
                            "store": True
                        }
                    }
                }
            }
        }
        if self.es.indices.exists(index=index_name) is not True:
            res = self.es.indices.create(index=index_name, body=_index_mappings)
            print res

    def bulk_Index_Data(self):
        '''
        用bulk将批量数据存储到es
        :return:
        '''
        list = FinanceStorage().get_data()
        ACTIONS = []
        i = 1
        for line in list:
            action = {
                "_index": self.index_name,
                "_type": self.index_type,
                "_id": i, #_id 也可以默认生成,不赋值
                "_source": {
                    "id": line['id'],
                    "dt": str(line['dt']).replace("-","/"),
                    "product": line['product'].encode('utf-8') ,
                    "media": line['media'].encode('utf-8') ,
                    "meida_sub_categorey": line['media_sub_category'].encode('utf-8') ,
                    "account": line['account'],
                    "ad_type": line['ad_type'].encode('utf-8'),
                    "ad_campaign": line.get('ad_campaign').encode('utf-8'),
                    "consume": line.get('consume'),
                    "shows": line.get('shows'),
                    "clicks": line.get('clicks'),
                    "publish_time": str(line['publish_time']).replace("-","/"),
                    "update_time": str(line['update_time']).replace("-","/"),
                    "ad_case": line['ad_case'].encode('utf-8'),
                    "ad_group": line['ad_group'].encode('utf-8')}
            }
            i += 1
            print action

            # 批量处理
            ACTIONS.append(action)
            success, _ = bulk(self.es, ACTIONS, index=self.index_name, raise_on_error=True)
            print('Performed %d actions' % success)

if __name__ == '__main__':
    obj = ElasticObj("data", "media_consume_table")
    obj.bulk_Index_Data()
    # obj.create_index()
    # FinanceStorage().get_data()

结果:


image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 什么是数据库? 数据库是存储数据的集合的单独的应用程序。每个数据库具有一个或多个不同的API,用于创建,访问,管理...
    chen_000阅读 4,057评论 0 19
  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 5,820评论 0 10
  • 手动不易,转发请注明出处 --Trance 数据库系统命令: (1).查看存储过程状态:show pro...
    Trance_b54c阅读 1,709评论 0 8
  • MySQL数据库对象与应用 2.1-MySQL数据类型 库建立好之后基本不动,和我们接触最频繁的是表. 建表就是声...
    极客圈阅读 2,174评论 0 8
  • 转 # https://www.cnblogs.com/easypass/archive/2010/12/ 08/...
    吕品㗊阅读 9,823评论 0 44