前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Easticsearch 数据迁移至in

Easticsearch 数据迁移至in

作者头像
py3study
发布2020-01-08 17:09:29
1.1K0
发布2020-01-08 17:09:29
举报
文章被收录于专栏:python3python3

Easticsearch 数据迁移至influxdb python

需求:将Easticsearch部分数据迁移至influxdb中。

见过从mysql,influxdb迁移至Easticsearch中的,没见过从Easticsearch迁移至influxdb中,迁移的数据是一些实时性的流量数据,influxdb时序性数据库对这类数据的支撑比较客观。

解决方案:大批量从Easticsearch取数据,两种方案。1.from...size    2.scroll (类似于数据库的游标)  脚本采用第二种scroll方案对Easticsearch 查询取数据。循环通过scrool_id进行查询并写入influxdb中。

代码语言:javascript
复制
#!/usr/bin/env python
#coding=utf-8

import sys
import json
import datetime
import elasticsearch
from influxdb import InfluxDBClient

#连接Easticsearch
class ES(object):
    @classmethod
    def connect_host(cls):
        url = "http://192.168.121.33:9202/"
        es = elasticsearch.Elasticsearch(url,timeout=120)
        return es
es = ES.connect_host()

#连接influxdb
client = InfluxDBClient(host="192.168.121.33", port="8086", username='admin', password='admin', database='esl')
client.create_database('esl')

#DSL查询语法
data = {
    "query": { "match_all" : {}},
    "size": 100
}

# 设置要过滤返回的字段值,要什么字段。
    'hits.hits._source.resource_id',
    'hits.hits._source.timestamp',
    'hits.hits._source.counter_volume',
    'hits.hits._source.@timestamp',
]

# 指定search_type="scan"模式,并返回_scroll_id给es.scroll获取数据使用
res = es.search(
    index='pipefilter_meters*',
    doc_type ='canaledge.flow.bytes',
    body=data,
    search_type="scan",
    scroll="10m"
)
scroll_id = res['_scroll_id']

response= es.scroll(scroll_id=scroll_id, scroll= "10m",filter_path=return_fields,)
scroll_id = response['_scroll_id']   #获取第二次scroll_id
hits = response['hits']['hits'] 
in_data = []

while len(hits) > 0:
    for i in hits:
        res_id = i['_source']['resource_id']
        r_id, r_type = res_id.split(':')
        datas = {
            "measurement": "es_net",
            "tags": {
                 "resource_id": r_id,
                 "type": r_type
             },
            "time": i['_source']['timestamp'],
            "fields": {
                "counter_volume": i['_source']['counter_volume']
            }
        }
        in_data.append(datas)
    #循环写入influxdb
    client.write_points(in_data)
    in_data = []   #每次循环完重新定义列表为空

    data = {
        "query": { "match_all" : {}},
        "size": 100
    }
    ## 设置要过滤返回的字段值,要什么字段。
        '_scroll_id',
        'hits.hits._source.resource_id',
        'hits.hits._source.timestamp',
        'hits.hits._source.counter_volume',
        'hits.hits._source.@timestamp',
    ]

    ## 指定search_type="scan"模式,并返回_scroll_id给es.scroll获取数据使用
    response= es.scroll(scroll_id=scroll_id, scroll= "10m",filter_path=return_fields,)
    #调试
    #if not response.get('hits'):
    #    print response
    #    sys.exit(1)
    #else:
    
    hits = response['hits']['hits']
    scroll_id = response["_scroll_id"] #获取第三次scroll_id
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档