pyhton 操作Elasticsearch
来源:互联网 发布:wifi mac地址查询 编辑:程序博客网 时间:2024/06/08 11:46
1、python连接elasticsearch
#coding=utf-8from elasticsearch import Elasticsearch as ESes=ES('192.168.1.38',port=9200)
2、创建索引
初始化索引 (mapping 相当于mysql的建表语句)
# coding=utf-8from elasticsearch import Elasticsearchimport sys# 解决编码问题reload(sys)sys.setdefaultencoding("utf8")es = Elasticsearch('192.168.1.38', port=9200)# 初始化索引mapping设置(ES6.0中每个_index只能有一个_type)my_index_map = { "mappings": { "as_org": { "properties": { "asnumber": {"type": "integer"}, "asname": {"type": "text", }, "orgname": {"type": "text"}, "type": {"type": "text"}, "country": {"type": "text"} } } }}# 如果索引不存在,则创建索引,索引名称不能有大写,应为全部小写if es.indices.exists(index="awtools") is not True: es.indices.create(index="awtools", body=my_index_map)
3、批量插入数据
批量写入数据用的是es中的bulk。目前bulk批量插入数据一次最多只能有500条。
#res_ip_as为数据库中的fetchall()的值 actions=[] i = 1 for each in res_ip_as: action = { "_index": "awtools", "_type": "as_org", "_id": i, "_source": { "minip": each[1], "maxip": each[2], "asnumber_as": each[3], } } i += 1 actions.append(action) if len(actions) == 500: helpers.bulk(es, actions) del actions[0:len(actions)] if len(actions) > 0: helpers.bulk(es, actions)
4、单条插入数据
es.index(index='awtools',doc_type='as_org',body=action)
阅读全文
0 0
- pyhton 操作Elasticsearch
- pyhton的切片操作
- Pyhton 操作数据库返回中文
- Elasticsearch操作
- elasticsearch 操作
- pyhton的操作笔记--未完待续
- pyhton列表合并、追加操作extend()、+
- Elasticsearch之基本操作
- elasticSearch批量操作bulk
- Elasticsearch-PHP 索引操作
- Elasticsearch-PHP 索引操作
- Elasticsearch 基本操作
- Elasticsearch-PHP 搜索操作
- java对elasticsearch操作
- elasticsearch 的 Percolator操作
- Elasticsearch API操作01
- ElasticSearch服务器操作命令
- Java操作elasticsearch
- Mybatis拦截器实现分页和存储过程实现分页
- 送给自己的书
- windows中安装mysql
- 使用Mardown遇到的问题
- 异步JS -- 异步控制台解惑
- pyhton 操作Elasticsearch
- Kotlin-1.1-基本类型
- 字符串出现次数统计
- 算法引论
- html 的打印和下载
- js 数组,字符串,json互相转换
- 一个程序员优秀的程序员应该所具备的素质
- Tensorflow运作之变量
- PAT乙级1021. 个位数统计 (15)