Elasticsearch入门的3个基本问题


elasticsearch(以下简称ES),的3个基本问题。


1. ES 如何写入输入?

2. 如何从ES读取数据?

3. ES的性能优化

1. 如何写入数据到ES?

ES使用了2个端口9200和9300(默认),9200负责HTTP的请求,即REST apis。我们可以REST api将数据写入ES. 例如, 使用curl命令即可实现一个写入操作。curl -XPUT http://esnode:9200/index/type -d ‘{json_doc}’

9300是TCP监听,供ES nodes之间通信使用。


写数据到ES,所使用的clients分成2种,

第一是JAVA的,第二是其它。

先说其它的(python/go/...等),都是用的是http REST api的方式,即访问的是9200.


再说JAVA client,根据角色的不同, 用分成2种, 其一,节点client(node client)即,java client成为ES cluster中的一个节点,但不存储数据。其二,传输client(transport client),即java client不加入ES cluster, 只是传输数据给ES cluster中的节点。JAVA client使用的都是9300端口, 使用的是Elasticsearch的传输 协议(native Elasticsearch transport protocol)。


使用Elasticsearch的官方python cleint(https://github.com/elastic/elasticsearch-py) 进行写数据的操作的case


from elasticsearch import Elasticsearch

from elasticsearch import helpers

from logger import diylogger

from datetime import datetime

import pickle



class ESWriter(object):

    '''

    elasticsearch writer

    '''

    BULK_SIZE = 10000

    TIME_OUT = 120


    def __init__(self, es_hosts, index):

        if isinstance(es_hosts, str):

            self.hosts = [es_hosts]

        elif isinstance(es_hosts, list):

            self.hosts = es_hosts

        else:

            raise TypeError("wrong type of es_hosts")


        self.client = Elasticsearch(self.hosts)

        self.index = index


    def bulk(self, file_path):

        '''

        bulk operation

        '''

        try:

            messages = list()

            with open(file_path, 'rb') as f:

                messages = pickle.load(f)


            actions = list()

            for item in messages:

                # message = json.loads(item)

                item = item[0:-1] + ',"timestamp":"' + \

                    datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%f") + '"}'

                action = dict()

                action["_index"] = self.index

                action["_type"] = "logs"

                action["_source"] = item

                actions.append(action)


                if len(actions) == self.BULK_SIZE:

                    helpers.bulk(self.client,

                                 actions,

                                 chunk_size=self.BULK_SIZE,

                                 request_timeout=self.TIME_OUT)

                    diylogger.info(

                        "bulk once to es {0}".format(self.BULK_SIZE))

                    actions = list()


            if len(actions) > 0:

                helpers.bulk(

                    self.client, actions, request_timeout=self.TIME_OUT)


            diylogger.info("bulk done for {0} messages".format(len(messages)))

        except Exception, e:

            diylogger.error("error in bulk of ESWriter:{0}".format(str(e)))

        finally:

            del messages

            del actions

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值