Elasticsearch:如何在 Python 中使用批量 API 为 Elasticsearch 索引文档

news/2024/9/24 0:25:36/

当我们需要创建 Elasticsearch 索引时,数据源通常没有规范化,无法直接导入。 原始数据可以存储在数据库、原始 CSV/XML 文件中,甚至可以从第三方 API 获取。 在这种情况下,我们需要对数据进行预处理以使其与 Bulk API 一起使用。 在本教程中,我们将演示如何使用简单的 Python 代码从 CSV 文件中索引 Elasticsearch 文档。 将使用原生 Elasticsearch bulk API 和 helpers 模块中的 API。 你将学习如何在不同的场合使用合适的工具来索引 Elasticsearch 文档。

在之前的文章 “Elasticsearch:关于在 Python 中使用 Elasticsearch 你需要知道的一切 - 8.x”,我展示了如何使用 bulk API 来索引文档到 Elasticsearch 中。细心的开发者可能观察到,如果我们的文档很多,数据量很大,那个方法可能并不适用,这是因为所以的操作都是在内存里进行操作的。如果我们的原始文档很大,这极有可能造成内存不够的情况。在今天的文章中,我将探讨使用 Python 里的 generator 来实现。

为了方便测试,我们的数据可以从 https://github.com/liu-xiao-guo/py-elasticsearch8 中获取。data.csv 将是我们使用的原始数据。

安装

为了方便进行测试,我们将采用我之前的文章 “Elasticsearch:如何在 Docker 上运行 Elasticsearch 8.x 进行本地开发” 来进行部署。在这里我们采用 docker compose 来进行安装 Elasticsearch 及 Kibana。我们将不采用安全设置。更多关于如何在具有安全性的条件下使用 Python 来连接 Elasticsearch,请参考之前的文章 “Elasticsearch:关于在 Python 中使用 Elasticsearch 你需要知道的一切 - 8.x”。我们可以参考那篇文章来进行安装所需要的 Python 包。

在 Python 中创建索引

我们将创建与之前文章中演示的相同的 latops-demo 索引。  首先,我们将使用 Elasticsearch 客户端直接创建索引。 此外,settings 和 mappings 将作为顶级参数传递,而不是通过 body 参数传递。创建索引的命令是:

main.py

# Import Elasticsearch package
from elasticsearch import Elasticsearch
import csv
import json# Connect to Elasticsearch cluster 
es = Elasticsearch( "http://localhost:9200")
resp = es.info()
print(resp)settings = {"index": {"number_of_replicas": 2},"analysis": {"filter": {"ngram_filter": {"type": "edge_ngram","min_gram": 2,"max_gram": 15,}},"analyzer": {"ngram_analyzer": {"type": "custom","tokenizer": "standard","filter": ["lowercase", "ngram_filter"],}}}
}mappings = {"properties": {"id": {"type": "long"},"name": {"type": "text","analyzer": "standard","fields": {"keyword": {"type": "keyword"},"ngrams": {"type": "text", "analyzer": "ngram_analyzer"},}},"brand": {"type": "text","fields": {"keyword": {"type": "keyword"},}},"price": {"type": "float"},"attributes": {"type": "nested","properties": {"attribute_name": {"type": "text"},"attribute_value": {"type": "text"},}}}
}configurations = {"settings": {"index": {"number_of_replicas": 2},"analysis": {"filter": {"ngram_filter": {"type": "edge_ngram","min_gram": 2,"max_gram": 15,}},"analyzer": {"ngram_analyzer": {"type": "custom","tokenizer": "standard","filter": ["lowercase", "ngram_filter"],}}}},"mappings": {"properties": {"id": {"type": "long"},"name": {"type": "text","analyzer": "standard","fields": {"keyword": {"type": "keyword"},"ngrams": {"type": "text", "analyzer": "ngram_analyzer"},}},"brand": {"type": "text","fields": {"keyword": {"type": "keyword"},}},"price": {"type": "float"},"attributes": {"type": "nested","properties": {"attribute_name": {"type": "text"},"attribute_value": {"type": "text"},}}}}
}INDEX_NAME = "laptops-demo"# check the existence of the index. If yes, remove it
if(es.indices.exists(index=INDEX_NAME)):print("The index has already existed, going to remove it")es.options(ignore_status=404).indices.delete(index=INDEX_NAME)# Create the index with the correct configurations
res = es.indices.create(index=INDEX_NAME, settings=settings,mappings=mappings)
print(res)# The following is another way to create the index, but it is deprecated
# es.indices.create(index = INDEX_NAME, body =configurations )

现在索引已创建。我们可以在 Kibana 中使用如下的命令来进行查看:

GET _cat/indices

我们可以开始向其中添加文档。

使用原生 Elasticsearch 批量 API

当你有一个小数据集要加载时,使用原生 Elasticsearch 批量 API 会很方便,因为语法与原生 Elasticsearch 查询相同,可以直接在 Dev 控制台中运行。 你不需要学习任何新东西。

将要加载的数据文件可以从这个链接下载。 将其保存为 data.csv,将在下面的 Python 代码中使用:

main.py

# Import Elasticsearch package
from elasticsearch import Elasticsearch
import csv
import json# Connect to Elasticsearch cluster 
es = Elasticsearch( "http://localhost:9200")
resp = es.info()
# print(resp)settings = {"index": {"number_of_replicas": 2},"analysis": {"filter": {"ngram_filter": {"type": "edge_ngram","min_gram": 2,"max_gram": 15,}},"analyzer": {"ngram_analyzer": {"type": "custom","tokenizer": "standard","filter": ["lowercase", "ngram_filter"],}}}
}mappings = {"properties": {"id": {"type": "long"},"name": {"type": "text","analyzer": "standard","fields": {"keyword": {"type": "keyword"},"ngrams": {"type": "text", "analyzer": "ngram_analyzer"},}},"brand": {"type": "text","fields": {"keyword": {"type": "keyword"},}},"price": {"type": "float"},"attributes": {"type": "nested","properties": {"attribute_name": {"type": "text"},"attribute_value": {"type": "text"},}}}
}configurations = {"settings": {"index": {"number_of_replicas": 2},"analysis": {"filter": {"ngram_filter": {"type": "edge_ngram","min_gram": 2,"max_gram": 15,}},"analyzer": {"ngram_analyzer": {"type": "custom","tokenizer": "standard","filter": ["lowercase", "ngram_filter"],}}}},"mappings": {"properties": {"id": {"type": "long"},"name": {"type": "text","analyzer": "standard","fields": {"keyword": {"type": "keyword"},"ngrams": {"type": "text", "analyzer": "ngram_analyzer"},}},"brand": {"type": "text","fields": {"keyword": {"type": "keyword"},}},"price": {"type": "float"},"attributes": {"type": "nested","properties": {"attribute_name": {"type": "text"},"attribute_value": {"type": "text"},}}}}
}INDEX_NAME = "laptops-demo"# check the existence of the index. If yes, remove it
if(es.indices.exists(index=INDEX_NAME)):print("The index has already existed, going to remove it")es.options(ignore_status=404).indices.delete(index=INDEX_NAME)# Create the index with the correct configurations
res = es.indices.create(index=INDEX_NAME, settings=settings,mappings=mappings)
print(res)# The following is another way to create the index, but it is deprecated
# es.indices.create(index = INDEX_NAME, body =configurations )with open("data.csv", "r") as fi:reader = csv.DictReader(fi, delimiter=",")actions = []for row in reader:action = {"index": {"_index": INDEX_NAME, "_id": int(row["id"])}}doc = {"id": int(row["id"]),"name": row["name"],"price": float(row["price"]),"brand": row["brand"],"attributes": [{"attribute_name": "cpu", "attribute_value": row["cpu"]},{"attribute_name": "memory", "attribute_value": row["memory"]},{"attribute_name": "storage","attribute_value": row["storage"],},],}actions.append(action)actions.append(doc)es.bulk(index=INDEX_NAME, operations=actions, refresh=True)# Check the results:
result = es.count(index=INDEX_NAME)
print(result)
print(result.body['count'])

我们运行上面的代码:

$ python main.py 
The index has already existed, going to remove it
{'acknowledged': True, 'shards_acknowledged': True, 'index': 'laptops-demo'}
{'count': 200, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}}
200

注意:在上面的 bulk 指令中,我们需要使用 refresh=True,否则当我们读出 count 的时候,它的值可能是 0。

在上面的代码中,有一个致命的问题就是我们在内存里创建 actions。如果我们的数据比较大的话,那么 actions 所需要的内存也会比较大。它显然不适合很大的数据的情况。

请注意,我们使用 csv 库方便地从 CSV 文件中读取数据。 可以看出,原生 bulk API 的语法非常简单,可以跨不同语言(包括 Dev Tools Console)使用。

使用批量助手 - bulk helper

如上所述,原生 bulk API 的一个问题是所有数据都需要先加载到内存,然后才能被索引。 当我们有一个大数据集时,这可能会出现问题并且效率很低。 为了解决这个问题,我们可以使用 bulk helper,它可以从迭代器(iterators)或生成器(generators)中索引 Elasticsearch 文档。 因此,它不需要先将所有数据加载到内存中,这在内存方面非常高效。 然而,语法有点不同,我们很快就会看到。

在我们使用 bulk helper 索引文档之前,我们应该删除索引中的文档以确认 bulk helper 确实成功工作。这个已经在我们上面的代码中已经完成了。然后我们可以运行以下代码使用批量助手将数据加载到 Elasticsearch:

main.py

# Import Elasticsearch package
from elasticsearch import Elasticsearch
from elasticsearch import helpers
import csv
import json# Connect to Elasticsearch cluster 
es = Elasticsearch( "http://localhost:9200")
resp = es.info()
# print(resp)settings = {"index": {"number_of_replicas": 2},"analysis": {"filter": {"ngram_filter": {"type": "edge_ngram","min_gram": 2,"max_gram": 15,}},"analyzer": {"ngram_analyzer": {"type": "custom","tokenizer": "standard","filter": ["lowercase", "ngram_filter"],}}}
}mappings = {"properties": {"id": {"type": "long"},"name": {"type": "text","analyzer": "standard","fields": {"keyword": {"type": "keyword"},"ngrams": {"type": "text", "analyzer": "ngram_analyzer"},}},"brand": {"type": "text","fields": {"keyword": {"type": "keyword"},}},"price": {"type": "float"},"attributes": {"type": "nested","properties": {"attribute_name": {"type": "text"},"attribute_value": {"type": "text"},}}}
}configurations = {"settings": {"index": {"number_of_replicas": 2},"analysis": {"filter": {"ngram_filter": {"type": "edge_ngram","min_gram": 2,"max_gram": 15,}},"analyzer": {"ngram_analyzer": {"type": "custom","tokenizer": "standard","filter": ["lowercase", "ngram_filter"],}}}},"mappings": {"properties": {"id": {"type": "long"},"name": {"type": "text","analyzer": "standard","fields": {"keyword": {"type": "keyword"},"ngrams": {"type": "text", "analyzer": "ngram_analyzer"},}},"brand": {"type": "text","fields": {"keyword": {"type": "keyword"},}},"price": {"type": "float"},"attributes": {"type": "nested","properties": {"attribute_name": {"type": "text"},"attribute_value": {"type": "text"},}}}}
}INDEX_NAME = "laptops-demo"# check the existence of the index. If yes, remove it
if(es.indices.exists(index=INDEX_NAME)):print("The index has already existed, going to remove it")es.options(ignore_status=404).indices.delete(index=INDEX_NAME)# Create the index with the correct configurations
res = es.indices.create(index=INDEX_NAME, settings=settings,mappings=mappings)
print(res)# The following is another way to create the index, but it is deprecated
# es.indices.create(index = INDEX_NAME, body =configurations )def generate_docs():with open("data.csv", "r") as fi:reader = csv.DictReader(fi, delimiter=",")for row in reader:doc = {"_index": INDEX_NAME,"_id": int(row["id"]),"_source": {"id": int(row["id"]),"name": row["name"],"price": float(row["price"]),"brand": row["brand"],"attributes": [{"attribute_name": "cpu","attribute_value": row["cpu"],},{"attribute_name": "memory","attribute_value": row["memory"],},{"attribute_name": "storage","attribute_value": row["storage"],},],},}yield dochelpers.bulk(es, generate_docs())
# (200, [])   -- 200 indexed, no errors.es.indices.refresh()# Check the results:
result = es.count(index=INDEX_NAME)
print(result.body['count'])

运行上面的代码。显示的结果如下:

$ python main.py 
The index has already existed, going to remove it
{'acknowledged': True, 'shards_acknowledged': True, 'index': 'laptops-demo'}
200

从上面的结果中我们可以看出来,我们已经成功地摄入了 200 个文档。


http://www.ppmy.cn/news/42952.html

相关文章

企业即时通讯软件开发基本功能有哪些?

即时通讯是基于互联网技术的新型交流沟通方式,是目前最流行的通讯方式,广泛的应用市场使得各种各样的即时通讯软件系统也层出不穷,企业即时通讯就是其中的一种延伸。是一种面向企业终端使用者的网络营销、网络沟通和内容管理的工具服务&#…

Windows wsl连接网络代理

使用 WSL 访问网络应用程序 | Microsoft Learn 为 WSL2 一键设置代理 - 知乎 (zhihu.com) 介绍 本文介绍开通了Windows WSL子系统之后,怎么在两者之间进行网络通讯;对在windows系统中开启了代理以后,如何在WSL中设置网络代理问题进行了详细…

从数据展示中汉字缺失了解字符编码知识

有人在使用皕杰报表时遇到如下问题: 有些汉字变成了“?”,这是为什么呢?实际上就是你用的字符集里没有这个汉字导致的,要想搞懂这个问题,还得从字符、字符集、字符编码说起。 所谓字符,就是各…

不服不行!盘点那些编程界的天才少年,11岁参加国际比赛,靠奖金赚了40万美金

程序员是一项专业性极强的工作,需要很强的思维能力和动手能力,所以大多数程序员要比普通人更聪明,与其他职业相比,程序员应该是最爱学习的一行了。科技网络的发展太快,新的技术层出不穷,时刻都要更新自己的…

【BAT】查询局域内所有的IP

【BAT】查询局域内所有的IP 在企业或家庭网络中,经常需要查询局域网内所有的 IP 地址。以下是一些方法可以帮助你实现这个目标: 使用 ping 命令 在 Windows 命令行中,可以使用 ping 命令向局域网中的所有主机发送 ICMP 请求,并…

Java基础——Map集合遍历方式

&#xff08;1&#xff09;方式一&#xff1a;键找值 先获取Map集合的全部键的Set集合。遍历键的Set集合&#xff0c;然后通过键提取对应值。 涉及API&#xff1a; 方法名称说明Set<K> keySet()获取所有键的集合V get(Object key)根据键获取值 &#xff08;2&#xff0…

【Python】Json读写操作_JsonPath用法详解

【Python】Json读写操作_JsonPath用法详解 文章目录【Python】Json读写操作_JsonPath用法详解1. 介绍2. 代码示例3. 参考1. 介绍 JSONPath是一种信息抽取类库&#xff0c;是从JSON文档中抽取指定信息的工具&#xff0c;提供多种语言实现版本&#xff0c;包括Javascript、Pytho…

[Gitops--1]GitOps环境准备

GitOps环境准备 1. 主机规划 序号主机名主机ip主机功能软件1dev192.168.31.1开发者 项目代码 apidemogit,golang,goland2gitlab192.168.31.14代码仓库,CI操作git-lab,git,golang,docker,gitlab-runner3harbor192.168.31.104管理和存储镜像docker,docker-compose,harbor4k8s-m…