python编写es脚本_Python 批量写入 Elasticsearch 脚本

Elasticsearch

官方和社区提供了各种各样的客户端库,在之前的博客中,我陆陆续续提

到和演示过

Perl

的,

Javascript

的,

Ruby

的。上周写了一版

Python

的,考虑到好像很难

找到现成的示例,如何用

python

批量写数据进

Elasticsearch

,今天一并贴上来。

#!/usr/bin/env pypy

#coding:utf-8

import re

import sys

import time

import datetime

import logging

from elasticsearch import Elasticsearch

from elasticsearch import helpers

from elasticsearch import ConnectionTimeout

es

=

Elasticsearch(['192.168.0.2',

'192.168.0.3'],

sniff_on_start=True,

sniff_on_connection_fail=True, max_retries=3, retry_on_timeout=True)

logging.basicConfig()

logging.getLogger('elasticsearch').setLevel(logging.WARN)

logging.getLogger('urllib3').setLevel(logging.WARN)

def parse_www(logline):

try:

time_local,

request,

http_user_agent,

staTus,

remote_addr,

http_referer,

request_time,

body_bytes_sent,

http_x_forwarded_proto,

http_x_forwarded_for,

http_host,

http_cookie,

upstream_response_time = logline.split('`')

try:

upstream_response_time = float(upstream_response_time)

except:

upstream_response_time = None

method, uri, verb = request.split(' ')

arg = {}

try:

url_path, url_args = uri.split('?')

for args in url_args.split('&'):

k, v = args.split('=')

arg[k] = v

except:

url_path = uri

# Why %z do not implement?

date = datetime.datetime.strptime(time_local, '[%d/%b/%Y:%H:%M:%S +0800]')

ret = {

"@timestamp": date.strftime('%FT%T+0800'),

"host": "127.0.0.1",

"method": method.lstrip('"'),

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值