The Seven Year Itch.

MENU

Elasticsearch 集成到项目中记录(现阶段已完成)

October 22, 2017 • Read: 711 • 工具,环境

下周开始,项目中要添加Elasticsearch大数据搜索服务了。并且用kong运行整个项目。开文章,后面用来记录。

安装java JDK
下载地址

需要对应自己所需要的版本

安装 elasticsearch
下载地址

下载后进入到目录中直接运行 ./elasticsearch

路径不能有中文,否则可能会报错。会报很多奇葩的错误。elasticsearch-run

elasticsearch的插件

elasticsearch-head

[head-下载地址](git clone git://github.com/mobz/elasticsearch-head.git
)

cd elasticsearch-head

npm install

npm run start

运行图

elasticsearch-kibana

kibana-下载地址

cd kibana/bin

./kibana

kibana-运行图

开启后的界面

kibana
elasticsearch-head

django 连接elasticsearch,并取出索引下所有数据

es数据查询
es查询结果

集成到django

class ElasticSearch(mixins.UpdateModelMixin,
                    mixins.ListModelMixin,
                    generics.GenericAPIView):

    def post(self,request):

        from elasticsearch import Elasticsearch
        from elasticsearch_dsl import Search
        from acl.jsonresponse import response_redis_code

        datetime = request.POST["datetime"] # _index 后面的时间
        start_time = request.POST["start_time"] # 开始时间
        end_time = request.POST["end_time"] # 结束时间
        fields = request.POST["fields"] # 接收address地址
        total = request.POST["total"] # 页数
        size = request.POST["size"] # 一页显示内容数

        address = fields.split(',') # 将前端传来的address进行分割

        client = Elasticsearch(hosts="192.168.31.81") # 连接elasticsearch

        s = Search(using=client,index="fset_iec104_"+datetime) \
        .filter("range",TS={"gt": start_time,
        "lt": end_time})
        s = s[int(total):int(size)] # 分页
        response = s.execute()

        dicts = []
        for hit in response:
            dicts.append(hit)
        response = []
        for addr in address:
            for dict in dicts:
                try:
                    res = {
                        'device':'1',
                        'address':addr,
                        'val':dict['1_'+addr],
                        'timeStamp':dict['TS'],
                        'description':response_redis_code(int(addr)).decode('utf8')
                    }
                    # print res
                    response.append(res)
                except Exception,e:
                    pass
                
        return jsonresponse.JsonResponse(list(response),code=200)

运行结果

es查询结果-django

Archives QR Code
QR Code for this page
Tipping QR Code