-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopensearch.py
More file actions
52 lines (49 loc) · 1.8 KB
/
Copy pathopensearch.py
File metadata and controls
52 lines (49 loc) · 1.8 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import boto3
from opensearchpy import OpenSearch, RequestsHttpConnection
from requests_aws4auth import AWS4Auth
def put_into_elasticsearch():
host = 'search-restaurants-i6vpb7aoab5uqzmzg57xczr6fy.us-west-2.es.amazonaws.com'
region = 'us-west-2'
service = 'es'
credentials = boto3.Session().get_credentials()
print(credentials.token)
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service,session_token=credentials.token)
es = OpenSearch(
hosts=[{'host': host, 'port': 443}],
http_auth=awsauth,
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection
)
dynamodb = boto3.resource('dynamodb', region_name='us-west-2')
table = dynamodb.Table("yelp-restaurants")
response = None
while True:
if response is None:
response = table.scan()
else:
# Scan from where you stopped previously.
response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
counter = 0
for business in response['Items']:
if not response:
# Scan from the start.
response = table.scan()
restaurantID = business["ID"]
doc = {
"ID": restaurantID,
"cuisine": business["cuisine"]
}
es.index(
index="restaurants",
doc_type="Restaurant",
id=restaurantID,
body=doc,
)
check = es.get(index="restaurants", doc_type="Restaurant", id=restaurantID)
if check["found"]:
print("Index %s succeeded" % restaurantID)
counter = counter + 1
# if __name__ == '__main__':
def lambda_handler(event, context):
put_into_elasticsearch()