Python读取Redis数据导出到Elasticsearch

#! usr/bin/python
# -*- coding:utf-8 -*-
import redis
import datetime
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
import sys, getopt

def usage():
	print 'usage: python cmd.py -e <elasticsearch host> -r <redis host> -p <redis list key prefix>'

if __name__ == '__main__':
	
	opts, args = getopt.getopt(sys.argv[1:], "e:r:p:")

	elasticsearch_host = ''
	redis_host = ''
	redis_list_prefix= ''
	for op, value in opts:
		if op == "-e":
			elasticsearch_host = value
		elif op == "-r":
			redis_host = value
		elif op == "-p":
			redis_list_prefix = value
		
	if elasticsearch_host == '' or redis_host == '' or redis_list_prefix == '':
		usage()
		sys.exit(1)
		
	client = Elasticsearch([{'host' : elasticsearch_host}])

	r = redis.Redis(host = redis_host, port=6379)

	doctype = "watchvideocount"

	#get all keys ends with day description except today
	videocount_keys = r.keys(redis_list_prefix + '_*')

	#start query data
	for k_index in videocount_keys:
		#get all <k, v> data
		kvalues = r.hgetall(k_index)
		
		#get day
		p = k_index.index('_')
		day = k_index[p+1:]	
			
		#make index name	
		index_name = k_index.lower()		
		#create index
		try:
			client.indices.create(index=index_name)
		except Exception:
			print 'index ', index_name, ' exist'
		namapping = {
				doctype: {
					"properties": {
						"day":{"type": "date"},
						"EID":{"type": "string"},
						"CID":{"type": "string"},
						"VID":{"type": "string"},
						"VALUE":{"type": "integer"}
					}
				}
			}
		client.indices.put_mapping(index=index_name, doc_type=doctype, body=namapping)
		
		#data cache
		data_cache = []	
		for key in kvalues:
			fields = key.split('_')
			eid = fields[0]
			cid = fields[1]
			vid = fields[2]
			value = kvalues[key]
			#make primary key
			ID = day + '_' + eid + '_' + cid + '_' + vid		
			#make JSON data
			json_data = {'_index':index_name, '_type':doctype, "_id":ID, "DAY":day, "EID":eid, "CID":cid, "VID":vid, "VALUE":value}	
			#append data cache
			data_cache.append(json_data)
		
		#index into elasticsearch	
		bulk(client, actions=data_cache, stats_only=True)
		print k_index, 'done'


转载于:https://my.oschina.net/u/2242064/blog/553022

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值