更新时间:2023-06-20 GMT+08:00

通过Python接入集群

本文介绍通过Python语言访问CSS集群的配置说明。

准备工作

  • CSS集群处于可用状态。
  • 确保运行Python代码的服务器与CSS集群的网络是互通的。

操作步骤

  1. 安装Elasticsearch Python客户端,建议和Elasticsearch的版本保持一致,例如需要访问的集群版本是7.6.2,则安装7.6的Elasticsearch Python客户端。
    1
    pip install Elasticsearch==7.6.2
    
  2. 创建Elasticsearch客户端并查看是否存在索引“test”。根据集群安全模式参考对应的示例代码。
    • 非安全模式的集群
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      from elasticsearch import Elasticsearch
      
      class ElasticFactory(object):
      
          def __init__(self, host: list, port: str, username: str, password: str):
              self.port = port
              self.host = host
              self.username = username
              self.password = password
      
          def create(self) -> Elasticsearch:
              addrs = []
              for host in self.host:
                  addr = {'host': host, 'port': self.port}
                  addrs.append(addr)
      
              if self.username and self.password:
                  elasticsearch = Elasticsearch(addrs, http_auth=(self.username, self.password))
              else:
                  elasticsearch = Elasticsearch(addrs)
              return elasticsearch
      
      es = ElasticFactory(["xxx.xxx.xxx.xxx"], "9200", None, None).create()
      print(es.indices.exists(index='test'))
      
    • 安全模式+HTTP协议的集群
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      from elasticsearch import Elasticsearch
      
      class ElasticFactory(object):
      
          def __init__(self, host: list, port: str, username: str, password: str):
              self.port = port
              self.host = host
              self.username = username
              self.password = password
      
          def create(self) -> Elasticsearch:
              addrs = []
              for host in self.host:
                  addr = {'host': host, 'port': self.port}
                  addrs.append(addr)
      
              if self.username and self.password:
                  elasticsearch = Elasticsearch(addrs, http_auth=(self.username, self.password))
              else:
                  elasticsearch = Elasticsearch(addrs)
              return elasticsearch
      
      es = ElasticFactory(["xxx.xxx.xxx.xxx"], "9200", "username", "password").create()
      print(es.indices.exists(index='test'))
      
    • 安全模式+HTTPS协议的集群
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      from elasticsearch import Elasticsearch
      import ssl
      
      class ElasticFactory(object):
      
          def __init__(self, host: list, port: str, username: str, password: str):
              self.port = port
              self.host = host
              self.username = username
              self.password = password
      
          def create(self) -> Elasticsearch:
              context = ssl._create_unverified_context()
      
              addrs = []
              for host in self.host:
                  addr = {'host': host, 'port': self.port}
                  addrs.append(addr)
      
              if self.username and self.password:
                  elasticsearch = Elasticsearch(addrs, http_auth=(self.username, self.password), scheme="https", ssl_context=context)
              else:
                  elasticsearch = Elasticsearch(addrs)
              return elasticsearch
      
      es = ElasticFactory(["xxx.xxx.xxx.xxx"], "9200", "username", "password").create()
      print(es.indices.exists(index='test'))
      
    表1 函数中的变量说明

    参数

    描述

    host

    ES集群各个节点(或者独立Client节点)的IP地址列表,当存在多个IP地址时,中间用“,”隔开。

    port

    ES集群的连接端口,填写“9200”

    username

    访问集群的用户名。

    password

    用户名对应的密码。

  3. 通过Elasticsearch客户端创建集群索引。
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    mappings = {
    	"settings": {
    		"index": {
    			"number_of_shards": number_of_shards,
    			"number_of_replicas": 1,
    		},
    	},
    	"mappings": {
    		properties
    	}
    }
    result = es.indices.create(index=index, body=mappings)
    
  4. 通过Elasticsearch客户端查询上一步创建的索引。
    1
    2
    3
    4
    5
    6
    7
    8
    body = {
    	"query": {
    		"match": {
    			"查询字段":  "查询内容"
    		}
    	}
    }
    result = es.search(index=index, body=body)