Spaces:
Configuration error
Configuration error
Update database.py
Browse files- database.py +105 -49
database.py
CHANGED
|
@@ -1,65 +1,121 @@
|
|
|
|
|
|
|
|
| 1 |
import boto3
|
| 2 |
from pymongo import MongoClient
|
| 3 |
-
import
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
dynamodb = boto3.resource('dynamodb')
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def connect_to_mongodb():
|
| 13 |
"""MongoDB'ye bağlan."""
|
| 14 |
client = MongoClient(MONGO_URI)
|
| 15 |
return client
|
| 16 |
|
| 17 |
-
def get_reference_data():
|
| 18 |
-
"""Reference database'den verileri al."""
|
| 19 |
-
client = connect_to_mongodb()
|
| 20 |
-
db = client[REFERENCE_DB]
|
| 21 |
-
collection = db['test'] #test koleksiyonu içerisinde
|
| 22 |
-
data = collection.find({}, {"title": 1, "keywords": 1, "text": 1, "_id": 0})
|
| 23 |
-
return list(data)
|
| 24 |
-
|
| 25 |
def insert_data_into_input_db(data):
|
| 26 |
"""Input database'e veri ekle."""
|
| 27 |
client = connect_to_mongodb()
|
| 28 |
db = client[INPUT_DB]
|
| 29 |
-
collection = db['input'] #
|
| 30 |
collection.insert_many(data)
|
| 31 |
-
|
| 32 |
-
def insert_data_into_dynamodb(data):
|
| 33 |
-
"""Kullanıcıdan alınan verileri DynamoDB'ye ekle."""
|
| 34 |
-
table = dynamodb.Table(input_to_users)
|
| 35 |
-
for item in data:
|
| 36 |
-
table.put_item(Item=item)
|
| 37 |
-
|
| 38 |
-
def get_user_input():
|
| 39 |
-
"""Kullanıcıdan veri al."""
|
| 40 |
-
# Lambda fonksiyonu ile kullanıcıdan gelen veriyi alın
|
| 41 |
-
user_input = [
|
| 42 |
-
{"title": "Sample Title", "keywords": ["keyword1", "keyword2","keyword3","keyword4","keyword5"], "text": "Sample text."}
|
| 43 |
-
]
|
| 44 |
-
return user_input
|
| 45 |
-
|
| 46 |
-
def main():
|
| 47 |
-
"""Ana fonksiyon."""
|
| 48 |
-
client = connect_to_mongodb()
|
| 49 |
-
|
| 50 |
-
# Referans veriyi MongoDB'den alın
|
| 51 |
-
reference_data = get_reference_data()
|
| 52 |
-
|
| 53 |
-
# Kullanıcıdan veri alın (Lambda aracılığıyla alınacak)
|
| 54 |
-
user_data = get_user_input()
|
| 55 |
-
|
| 56 |
-
# Kullanıcı verilerini DynamoDB'ye ekle
|
| 57 |
-
insert_data_into_dynamodb(user_data)
|
| 58 |
-
|
| 59 |
-
# Kullanıcı verilerini MongoDB'deki input database'e ekle
|
| 60 |
-
insert_data_into_input_db(user_data)
|
| 61 |
-
|
| 62 |
client.close()
|
| 63 |
-
|
| 64 |
-
if __name__ == "__main__":
|
| 65 |
-
main()
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
import boto3
|
| 4 |
from pymongo import MongoClient
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
|
| 9 |
+
# Çevresel değişkenlerden yapılandırma bilgilerini alıyoruz
|
| 10 |
+
DYNAMO_TABLE = os.getenv('DYNAMO_TABLE')
|
| 11 |
+
MONGO_URI = os.getenv('MONGO_URI')
|
| 12 |
+
INPUT_DB = os.getenv('INPUT_DB')
|
| 13 |
+
|
| 14 |
+
# AWS DynamoDB kaynağı
|
| 15 |
dynamodb = boto3.resource('dynamodb')
|
| 16 |
+
|
| 17 |
+
def lambda_handler(event, context):
|
| 18 |
+
"""AWS Lambda işleyici fonksiyonu."""
|
| 19 |
+
method = event['requestContext']['http']['method']
|
| 20 |
+
|
| 21 |
+
if method == 'POST':
|
| 22 |
+
return handle_post(event)
|
| 23 |
+
elif method == 'GET':
|
| 24 |
+
return handle_get(event)
|
| 25 |
+
else:
|
| 26 |
+
return {
|
| 27 |
+
'statusCode': 405,
|
| 28 |
+
'body': json.dumps('Method Not Allowed')
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
def handle_post(event):
|
| 32 |
+
"""POST isteklerini işleyin."""
|
| 33 |
+
table = dynamodb.Table(DYNAMO_TABLE)
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
body = json.loads(event['body'])
|
| 37 |
+
title = body.get('title')
|
| 38 |
+
keywords = body.get('keywords')
|
| 39 |
+
text = body.get('text')
|
| 40 |
+
|
| 41 |
+
if not title or not keywords or not text:
|
| 42 |
+
return {
|
| 43 |
+
'statusCode': 400,
|
| 44 |
+
'body': json.dumps('Invalid input data')
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
# DynamoDB'ye veri ekleyin
|
| 48 |
+
table.put_item(
|
| 49 |
+
Item={
|
| 50 |
+
'title': title,
|
| 51 |
+
'keywords': keywords,
|
| 52 |
+
'text': text
|
| 53 |
+
}
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# MongoDB'ye veri ekleyin
|
| 57 |
+
insert_data_into_input_db([{"title": title, "keywords": keywords, "text": text}])
|
| 58 |
+
|
| 59 |
+
return {
|
| 60 |
+
'statusCode': 200,
|
| 61 |
+
'body': json.dumps('Data inserted into DynamoDB and MongoDB!')
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
except Exception as e:
|
| 65 |
+
return {
|
| 66 |
+
'statusCode': 500,
|
| 67 |
+
'body': json.dumps(f"Error: {str(e)}")
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
def handle_get(event):
|
| 71 |
+
"""GET isteklerini işleyin."""
|
| 72 |
+
table = dynamodb.Table(DYNAMO_TABLE)
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
query_params = event.get('queryStringParameters', {})
|
| 76 |
+
title = query_params.get('title')
|
| 77 |
+
|
| 78 |
+
if not title:
|
| 79 |
+
return {
|
| 80 |
+
'statusCode': 400,
|
| 81 |
+
'body': json.dumps('Title parameter is required')
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
# DynamoDB'den veri alın
|
| 85 |
+
response = table.get_item(
|
| 86 |
+
Key={
|
| 87 |
+
'title': title
|
| 88 |
+
}
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
item = response.get('Item', {})
|
| 92 |
+
|
| 93 |
+
if not item:
|
| 94 |
+
return {
|
| 95 |
+
'statusCode': 404,
|
| 96 |
+
'body': json.dumps('Data not found')
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
return {
|
| 100 |
+
'statusCode': 200,
|
| 101 |
+
'body': json.dumps(item)
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
except Exception as e:
|
| 105 |
+
return {
|
| 106 |
+
'statusCode': 500,
|
| 107 |
+
'body': json.dumps(f"Error: {str(e)}")
|
| 108 |
+
}
|
| 109 |
|
| 110 |
def connect_to_mongodb():
|
| 111 |
"""MongoDB'ye bağlan."""
|
| 112 |
client = MongoClient(MONGO_URI)
|
| 113 |
return client
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
def insert_data_into_input_db(data):
|
| 116 |
"""Input database'e veri ekle."""
|
| 117 |
client = connect_to_mongodb()
|
| 118 |
db = client[INPUT_DB]
|
| 119 |
+
collection = db['input'] # Kullanıcıdan alınacak verilerin saklandığı koleksiyon
|
| 120 |
collection.insert_many(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
client.close()
|
|
|
|
|
|
|
|
|