AWSGluepythonshelljob-如何在VPC中连接到Elasticsearch?
创始人
2024-09-25 16:01:44
0
  1. 在AWS Glue的VPC设置中添加与Elasticsearch相同的VPC。
  2. 给AWS Glue的执行角色添加允许访问Elasticsearch的权限。
  3. 安装Elasticsearch和boto3模块。
  4. 编写连接Elasticsearch的Python代码,示例如下:
import boto3
import requests
from requests_aws4auth import AWS4Auth
import json

# 填写Elasticsearch的Endpoint 
endpoint = 'your-es-endpoint'
# 填写Elasticsearch的Region 
region = 'your-region'
# 填写Elasticsearch的访问账户和密钥
access_key = 'your-access-key'
secret_key = 'your-secret-key'

# 认证
service = 'es'
es_auth = AWS4Auth(access_key, secret_key, region, service)

# 索引名称
index = 'your-index-name'
doc_type = 'your-doc-type'

# 简单的查询
query = { 
  "query": {
    "match_all": {}
  }
}

# 发送查询请求
url = 'https://' + endpoint + '/' + index + '/' + doc_type + '/_search'
headers = { "Content-Type": "application/json" }
response = requests.post(url, auth=es_auth, headers=headers, data=json.dumps(query))
print(response.json())
  1. 在Glue Job中调用Python代码。
from awsglue.utils import getResolvedOptions
import boto3
import sys
import json

args = getResolvedOptions(sys.argv, ['JOB_NAME'])

# Glue和Elasticsearch在同一VPC中,可以使用内网访问
es_endpoint = 'https://your-es-endpoint'
region = 'your-region'
index = 'your-index-name'

# 填写认证信息
access_key = 'your-access-key'
secret_key = 'your-secret-key'

# 认证
service = 'es'
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)

# 发送查询请求
query_url = es_endpoint + '/' + index + '/_search?'
payload = {
    'query': {
        'match_all': {}
    },
    'size': 10
}
headers = {'Content-Type': 'application/json'}
response = requests.get(query_url, headers=headers, auth=awsauth, data=json.dumps(payload))
result = response.content.decode('utf-8')
print(result)

注意:上面两段代码需要根据不同的Elasticsearch版本和ES集群角色进行修改。

通过以上步骤,即可在AWS Glue的Pythonshell Job中成功连接到Elasticsearch集群。

相关内容

热门资讯

一分钟揭秘!微扑克有辅助软件吗... 一分钟揭秘!微扑克有辅助软件吗,aapoker挂,我来教教你(固有有挂)1、aapoker挂ai辅助...
重大发现!wpk德州辅助,we... 重大发现!wpk德州辅助,wepoke有挂,微扑克教程(原本有挂)1.wepoke有挂 ai辅助创建...
总算明白!微扑克辅助器是真的么... 总算明白!微扑克辅助器是真的么,德州ai辅助有用,力荐教程(都是是真的有挂)1、构建自己的微扑克辅助...
玩家必备科技!gg扑克辅助,w... 玩家必备科技!gg扑克辅助,wepoke辅助有挂,AI教程(确实真的是有挂)1、在wepoke辅助有...
重大来袭!cloudpoker... 重大来袭!cloudpoker辅助器,wepoke真的有挂,力荐教程(固有真的有挂)1、wepoke...
最新技巧!wepoke透明真的... 最新技巧!wepoke透明真的吗,智星德州菠萝有挂吗,AI教程(最初有挂);人气非常高,ai更新快且...
一分钟快速了解!德州微扑克辅助... 一分钟快速了解!德州微扑克辅助,德州微扑克辅助,高科技教程(好像存在有挂)是一款可以让一直输的玩家,...
一分钟教会你!wpk有吗,德州... 一分钟教会你!wpk有吗,德州ai辅助软件,玩家教你(固有是真的有挂)德州ai辅助软件辅助器中分为三...
实操分享!微扑克辅助,德州ai... 实操分享!微扑克辅助,德州ai机器人,科技教程(真是有挂)1、超多福利:超高返利,海量正版游戏,德州...
玩家必看科普!微扑克有辅助软件... 自定义aapoker辅助系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不...