下周开始,项目中要添加Elasticsearch大数据搜索服务了。并且用kong运行整个项目。开文章,后面用来记录。
安装java JDK 下载地址
需要对应自己所需要的版本
安装 elasticsearch 下载地址
下载后进入到目录中直接运行 ./elasticsearch
路径不能有中文,否则可能会报错。会报很多奇葩的错误。
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
开启后的界面
django 连接elasticsearch,并取出索引下所有数据
集成到django
代码语言:javascript复制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)
运行结果
Tags: 大数据, elasticsearch, 搜索服务
Archives QR Code