Spaces:
Configuration error
Configuration error
Update database.py
Browse files- database.py +20 -10
database.py
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
|
|
| 1 |
from pymongo import MongoClient
|
| 2 |
-
import requests
|
| 3 |
-
|
| 4 |
import os
|
| 5 |
-
from pymongo import MongoClient
|
| 6 |
-
|
| 7 |
|
|
|
|
|
|
|
| 8 |
MONGO_URI = os.getenv('MONGO_URI', 'mongodb://localhost:27017')
|
| 9 |
REFERENCE_DB = os.getenv('REFERENCE_DB', 'EgitimDatabase')
|
| 10 |
INPUT_DB = os.getenv('INPUT_DB', 'InputDatabase')
|
|
|
|
| 11 |
|
| 12 |
def connect_to_mongodb():
|
| 13 |
"""MongoDB'ye bağlan."""
|
|
@@ -18,7 +18,7 @@ def get_reference_data():
|
|
| 18 |
"""Reference database'den verileri al."""
|
| 19 |
client = connect_to_mongodb()
|
| 20 |
db = client[REFERENCE_DB]
|
| 21 |
-
collection = db['test'] #
|
| 22 |
data = collection.find({}, {"title": 1, "keywords": 1, "text": 1, "_id": 0})
|
| 23 |
return list(data)
|
| 24 |
|
|
@@ -26,15 +26,20 @@ 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 get_user_input():
|
| 33 |
"""Kullanıcıdan veri al."""
|
| 34 |
-
#
|
| 35 |
user_input = [
|
| 36 |
{"title": "Sample Title", "keywords": ["keyword1", "keyword2","keyword3","keyword4","keyword5"], "text": "Sample text."}
|
| 37 |
-
|
| 38 |
]
|
| 39 |
return user_input
|
| 40 |
|
|
@@ -42,14 +47,19 @@ def main():
|
|
| 42 |
"""Ana fonksiyon."""
|
| 43 |
client = connect_to_mongodb()
|
| 44 |
|
|
|
|
| 45 |
reference_data = get_reference_data()
|
|
|
|
|
|
|
| 46 |
user_data = get_user_input()
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
insert_data_into_input_db(user_data)
|
| 49 |
|
| 50 |
client.close()
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
main()
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 1 |
+
import boto3
|
| 2 |
from pymongo import MongoClient
|
|
|
|
|
|
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# AWS DynamoDB ve MongoDB bağlantıları için gerekli ayarlar
|
| 6 |
+
dynamodb = boto3.resource('dynamodb')
|
| 7 |
MONGO_URI = os.getenv('MONGO_URI', 'mongodb://localhost:27017')
|
| 8 |
REFERENCE_DB = os.getenv('REFERENCE_DB', 'EgitimDatabase')
|
| 9 |
INPUT_DB = os.getenv('INPUT_DB', 'InputDatabase')
|
| 10 |
+
DYNAMO_TABLE = os.getenv('DYNAMO_TABLE', 'UserInputs')
|
| 11 |
|
| 12 |
def connect_to_mongodb():
|
| 13 |
"""MongoDB'ye bağlan."""
|
|
|
|
| 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 |
|
|
|
|
| 26 |
"""Input database'e veri ekle."""
|
| 27 |
client = connect_to_mongodb()
|
| 28 |
db = client[INPUT_DB]
|
| 29 |
+
collection = db['input'] #kullanıcıdan alınacak verilerin saklandığı koleksiyon adı
|
| 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 |
|
|
|
|
| 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()
|
|
|
|
|
|