python操作Elasticsearch (一、例子)
E lasticsearch是一款分布式搜索引擎,支持在大数据环境中进行实时数据分析。它基于Apache Lucene文本搜索引擎,内部功能通过ReST API暴露给外部。除了通过HTTP直接访问Elasticsearch,还可以通过支持Java、JavaScript、Python及更多语言的客户 端库来访问。它也支持集成Apache Hadoop环境。Elasticsearch在有些处理海量数据的公司中已经有所应用,如GitHub、Foursquare和SoundCloud等。
elasticsearch 他对外提供了rest的http的接口,貌似很强大的样子。 但是咱们的一些数据平台市场会对于elasticsearch的数据进行分析,尤其是实时分析。 当然不能用 http的方式。
下面是http的方式的一个demo:
下面是查询,/ceshi是索引,rui是type,搜索的内容是title为jones的数据
curl http://vim.xiaorui.cc:9200/ceshi/rui/_search?q=title:jones&size=5&pretty=true
添加数据
curl -X POST -d '{"title":"jones","amount":5.7}'
但是听说,1.x之后不能直接curl,这不是重点忽略
下面介绍一个python使用elasticsearch的例子
from datetime import datetime
from elasticsearch import Elasticsearch #连接elasticsearch,默认是9200
es = Elasticsearch() #创建索引,索引的名字是my-index,如果已经存在了,就返回个400,
#这个索引可以现在创建,也可以在后面插入数据的时候再临时创建
es.indices.create(index='my-index',ignore)
#{u'acknowledged':True} #插入数据,(这里省略插入其他两条数据,后面用)
es.index(index="my-index",doc_type="test-type",id=01,body={"any":"data01","timestamp":datetime.now()})
#{u'_type':u'test-type',u'created':True,u'_shards':{u'successful':1,u'failed':0,u'total':2},u'_version':1,u'_index':u'my-index',u'_id':u'1}
#也可以,在插入数据的时候再创建索引test-index
es.index(index="test-index",doc_type="test-type",id=42,body={"any":"data","timestamp":datetime.now()}) #查询数据,两种get and search
#get获取
res = es.get(index="my-index", doc_type="test-type", id=01)
print(res)
#{u'_type': u'test-type', u'_source': {u'timestamp': u'2016-01-20T10:53:36.997000', u'any': u'data01'}, u'_index': u'my-index', u'_version': 1, u'found': True, u'_id': u'1'}
print(res['_source'])
#{u'timestamp': u'2016-01-20T10:53:36.997000', u'any': u'data01'} #search获取
res = es.search(index="test-index", body={"query":{"match_all":{}}})
print(res)
#{u'hits':
# {
# u'hits': [
# {u'_score': 1.0, u'_type': u'test-type', u'_id': u'2', u'_source': {u'timestamp': u'2016-01-20T10:53:58.562000', u'any': u'data02'}, u'_index': u'my-index'},
# {u'_score': 1.0, u'_type': u'test-type', u'_id': u'1', u'_source': {u'timestamp': u'2016-01-20T10:53:36.997000', u'any': u'data01'}, u'_index': u'my-index'},
# {u'_score': 1.0, u'_type': u'test-type', u'_id': u'3', u'_source': {u'timestamp': u'2016-01-20T11:09:19.403000', u'any': u'data033'}, u'_index': u'my-index'}
# ],
# u'total': 5,
# u'max_score': 1.0
# },
#u'_shards': {u'successful': 5, u'failed': 0, u'total':5},
#u'took': 1,
#u'timed_out': False
#}
for hit in res['hits']['hits']:
print(hit["_source"])
res = es.search(index="test-index", body={'query':{'match':{'any':'data'}}}) #获取any=data的所有值
print(res)
至于body里面参数的设置,具体请看:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html
python操作Elasticsearch (一、例子)的更多相关文章
- Python 操作 ElasticSearch
Python 操作 ElasticSearch 学习了:https://www.cnblogs.com/shaosks/p/7592229.html 官网:https://elasticsearch- ...
- python操作elasticsearch增、删、改、查
最近接触了个新东西--es数据库 这东西虽然被用的很多,但我是前些天刚刚接触的,发现其资料不多,学起来极其痛苦,写个文章记录下 导入库from elasticsearch import Elastic ...
- Python操作ElasticSearch
Python批量向ElasticSearch插入数据 Python 2的多进程不能序列化类方法, 所以改为函数的形式. 直接上代码: #!/usr/bin/python # -*- coding:ut ...
- python实现elasticsearch操作-CRUD API
python操作elasticsearch常用API 目录 目录 python操作elasticsearch常用API1.基础2.常见增删改操作创建更新删除3.查询操作查询拓展类实现es的CRUD操作 ...
- Python学习笔记 - day11 - Python操作数据库
MySQL的事务 MySQL的事务支持不是绑定在MySQL服务器本身,而是与存储引擎相关,MySQL的两种引擎如下: 1.MyISAM:不支持事务,用于只读程序提高性能 2.InnoDB:支持ACID ...
- python使用elasticsearch模块操作elasticsearch
1.创建索引 命令如下 from elasticsearch import Elasticsearch es = Elasticsearch([{"host":"10.8 ...
- 利用NEST2.0 在C#中操作Elasticsearch
前言:本文主要演示了如何通过c#来操作elasticsearch,分两个方面来演示: 索引数据 搜索数据 Note: 注意我索引数据和搜索数据是两个不同的例子,没有前后依赖关系 准备工作:需要在vis ...
- Python操作Mysql之基本操作
pymysql python操作mysql依赖pymysql这个模块 下载安装 pip3 install pymysql 操作mysql python操作mysql的时候,是通过”游标”来进行操作的. ...
- Python 操作 mongodb 数据库
原文地址:https://serholiu.com/python-mongodb 这几天在学习Python Web开发,于 是做准备做一个博客来练练手,当然,只是练手的,博客界有WordPress这样 ...
随机推荐
- 编写Dockerfile自定义镜像
要求 编写一个Dockerfile自定义centos镜像,要求在容器内部可以使用vim和ifconfig命令,并且登入落脚点为/usr/local 编写Dockerfile FROM centos M ...
- javascript字符串机油
1.创建字符串和数组的方法 1.1创建字符串的方法 a.直接数量:var str=“: b.字符串对象创建:新字符串(“): 1.2创建阵列的方法 a.var.arr=要素…. b.var arr=n ...
- Oracle创建索引;查询索引
1.创建索引 create index 索引名 on 表名(列名); 2.删除索引 drop index 索引名; 3.创建组合索引 create index 索引名 on 表名(列名1,,列名2); ...
- UART串口简介
通用异步收发传输器(Universal Asynchronous Receiver Transmitter) 原理 发送数据时,CPU将并行数据写入UART,UART按照一定的格式在一根电线上串行发出 ...
- OpenCV_contrib里的Text(自然场景图像中的文本检测与识别)
平台:win10 x64 +VS 2015专业版 +opencv-3.x.+CMake 待解决!!!Issue说明:最近做一些字符识别的事情,想试一下opencv_contrib里的Text(自然场景 ...
- List、Set、Map 之间的区别是什么?(未完成)
List.Set.Map 之间的区别是什么?(未完成)
- Canvas对画布及文字控制基础API学习
这次纯API练习,比较简单,但是是为了之后的结合项目打基础的,所以也不能忽视它,下面开始: Canvas的平移.旋转.缩放 这里还是以上次画那个青春痘的DEMO为例[http://www.cnblog ...
- CSS样式设置小技巧
1.水平居中设置 行内元素居中设置:如果被设置元素为文本.图片等行内元素时,水平居中是通过给父元素设置 text-align:center 来实现的.代码示例如下: HTML代码 <body&g ...
- python大数据初探--pandas,numpy代码示例
import pandas as pd import numpy as np dates = pd.date_range(',periods=6) dates import pandas as pd ...
- [Web Component] Allow External Styling of a Web Component's Shadow DOM
The Shadow DOM protects your components from style conflicts. The same protection also makes it hard ...