Spaces:
Configuration error
Configuration error
import boto3 | |
from pymongo import MongoClient | |
import os | |
# AWS DynamoDB ve MongoDB bağlantıları için gerekli ayarlar | |
dynamodb = boto3.resource('dynamodb') | |
MONGO_URI = os.getenv('MONGO_URI', 'mongodb://localhost:27017') | |
REFERENCE_DB = os.getenv('REFERENCE_DB', 'EgitimDatabase') | |
INPUT_DB = os.getenv('INPUT_DB', 'InputDatabase') | |
DYNAMO_TABLE = os.getenv('DYNAMO_TABLE', 'UserInputs') | |
def connect_to_mongodb(): | |
"""MongoDB'ye bağlan.""" | |
client = MongoClient(MONGO_URI) | |
return client | |
def get_reference_data(): | |
"""Reference database'den verileri al.""" | |
client = connect_to_mongodb() | |
db = client[REFERENCE_DB] | |
collection = db['test'] #test koleksiyonu içerisinde | |
data = collection.find({}, {"title": 1, "keywords": 1, "text": 1, "_id": 0}) | |
return list(data) | |
def insert_data_into_input_db(data): | |
"""Input database'e veri ekle.""" | |
client = connect_to_mongodb() | |
db = client[INPUT_DB] | |
collection = db['input'] #kullanıcıdan alınacak verilerin saklandığı koleksiyon adı | |
collection.insert_many(data) | |
def insert_data_into_dynamodb(data): | |
"""Kullanıcıdan alınan verileri DynamoDB'ye ekle.""" | |
table = dynamodb.Table(input_to_users) | |
for item in data: | |
table.put_item(Item=item) | |
def get_user_input(): | |
"""Kullanıcıdan veri al.""" | |
# Lambda fonksiyonu ile kullanıcıdan gelen veriyi alın | |
user_input = [ | |
{"title": "Sample Title", "keywords": ["keyword1", "keyword2","keyword3","keyword4","keyword5"], "text": "Sample text."} | |
] | |
return user_input | |
def main(): | |
"""Ana fonksiyon.""" | |
client = connect_to_mongodb() | |
# Referans veriyi MongoDB'den alın | |
reference_data = get_reference_data() | |
# Kullanıcıdan veri alın (Lambda aracılığıyla alınacak) | |
user_data = get_user_input() | |
# Kullanıcı verilerini DynamoDB'ye ekle | |
insert_data_into_dynamodb(user_data) | |
# Kullanıcı verilerini MongoDB'deki input database'e ekle | |
insert_data_into_input_db(user_data) | |
client.close() | |
if __name__ == "__main__": | |
main() | |