ES介绍:
Elasticsearch(ES)和传统数据库之间没有一一对应的关系,因为它们在数据存储和查询上有很大的区别。然而,在某些方面,它们具有一些相似之处。下面是一些它们之间可能存在的类比:
1. 索引(Index) - 数据库(Database)
Elasticsearch的索引类似于传统数据库中的数据库。在一个索引中,你可以存储多种类型的文档,并在每个文档中定义不同的字段。
2. 类型(Type) - 数据表(Table)
在Elasticsearch中,类型类似于传统数据库中的表格。在一个类型中,你可以定义不同的字段,并将其映射到每个文档中。
3. 文档(Document) - 行(Row)
在Elasticsearch中,文档类似于传统数据库中的行。每个文档都有一个唯一的ID,对应于关系数据库表中的主键。每个文档也包含一个或多个字段,对应于表格中的列。
4. 字段(Field) - 字段(Column)
在Elasticsearch中,每个文档包含多个字段。每个字段都有一个名称和一个值,对应于传统数据库表格的列。
5. 分片(Shard) - 分区(Partition)
在Elasticsearch中,索引被分成多个分片。每个分片存储了索引的一部分数据。这类似于传统数据库中的分区,其中表格的数据被分成多个区域以提高查询性能。
虽然Elasticsearch和传统数据库之间有一些类比,但它们在数据存储和查询方式上有很大的区别。Elasticsearch是一种分布式搜索引擎,可以快速处理大量数据。它不是传统数据库,不能完全替代它们。
一、下载地址
https://www.elastic.co/cn/elasticsearch
选择对应版本即可
二、配置操作
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
#cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
#
#network.host: 192.168.0.1
#
# By default Elasticsearch listens for HTTP traffic on the first free port it
# finds starting at 9200. Set a specific HTTP port here:
#
http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
#cluster.initial_master_nodes: ["node-1", "node-2"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# --------------------------------- Readiness ----------------------------------
#
# Enable an unauthenticated TCP readiness endpoint on localhost
#
#readiness.port: 9399
#
# ---------------------------------- Various -----------------------------------
#
# Allow wildcard deletion of indices:
#
#action.destructive_requires_name: false
#----------------------- BEGIN SECURITY AUTO CONFIGURATION -----------------------
#
# The following settings, TLS certificates, and keys have been automatically
# generated to configure Elasticsearch security features on 21-03-2023 07:44:21
#
# --------------------------------------------------------------------------------
# Enable security features
xpack.security.enabled: false
xpack.security.enrollment.enabled: true
# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
enabled: false
keystore.path: certs/http.p12
# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
enabled: true
verification_mode: certificate
keystore.path: certs/transport.p12
truststore.path: certs/transport.p12
# Create a new cluster with the current node only
# Additional nodes can still join the cluster later
cluster.initial_master_nodes: ["ZB-PF2G5QX1"]
# Allow HTTP API connections from anywhere
# Connections are encrypted and require user authentication
http.host: 0.0.0.0
# Allow other nodes to join the cluster from anywhere
# Connections are encrypted and mutually authenticated
#transport.host: 0.0.0.0
#----------------------- END SECURITY AUTO CONFIGURATION -------------------------
其中注意点:
关闭密码验证:xpack.security.enabled: false
关闭SSL验证:
xpack.security.http.ssl:
enabled: false
keystore.path: certs/http.p12
三、验证
出现如下界面即可:
![](https://img-blog.csdnimg.cn/img_convert/c48f1343837563501ff7ab7c3b0a32f3.png)
四、Python ES Demo示例
遍历Dataframe:
import pandas as pd
persons={'number':['小花','狗蛋','二狗','小草'],'crmFinalTag':[17,20,30,20],'crmFinalTagDesc':['女','男','男','女']}
df=pd.DataFrame(persons)
存入ES:
es.indices.create(index="tmp_crm_grade", ignore=400)
for index,row in df.iterrows():
data = {
"number": row['number'],
"crmFinalTag": row['crmFinalTag'],
"crmFinalTagDesc": row['crmFinalTagDesc']
} # 构建插入数据的结构