Skip to content

Commit c4c268b

Browse files
implement data extraction and insertion to MongoDB with error handling
1 parent da35aaa commit c4c268b

File tree

5 files changed

+81
-10
lines changed

5 files changed

+81
-10
lines changed

logs/01_25_2025_21_33_44.log/01_25_2025_21_33_44.log

Whitespace-only changes.

push_data.py

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,64 @@
1+
import os
2+
import sys
3+
import json
14

2-
from pymongo.mongo_client import MongoClient
5+
from dotenv import load_dotenv
36

4-
uri = "mongodb+srv://basakanimesh16:[email protected]/?retryWrites=true&w=majority&appName=Cluster0"
7+
load_dotenv()
58

6-
# Create a new client and connect to the server
7-
client = MongoClient(uri)
9+
MONGO_DB_URI = os.getenv("MONGO_DB_URI")
810

9-
# Send a ping to confirm a successful connection
10-
try:
11-
client.admin.command('ping')
12-
print("Pinged your deployment. You successfully connected to MongoDB!")
13-
except Exception as e:
14-
print(e)
11+
import certifi
12+
13+
ca = certifi.where()
14+
15+
import pandas as pd
16+
import numpy as np
17+
import pymongo
18+
from src.exception.exception import NetworkSecurityException
19+
from src.logging.logger import logging
20+
21+
class NetworkDataExtract:
22+
def __init__(self):
23+
try:
24+
pass
25+
except Exception as e:
26+
raise NetworkSecurityException(e, sys)
27+
28+
def cv_to_json_converter(self, file_path):
29+
try:
30+
data = pd.read_csv(file_path)
31+
data.reset_index(drop=True,inplace=True)
32+
records = list(json.loads(data.T.to_json()).values())
33+
return records
34+
35+
except Exception as e:
36+
logging.error(e)
37+
raise NetworkSecurityException(e, sys)
38+
39+
def insert_data_mongodb(self, records, database, collection):
40+
try:
41+
self.database = database
42+
self.collection = collection
43+
self.records = records
44+
45+
self.mongo_client = pymongo.MongoClient(MONGO_DB_URI, tlsCAFile=ca)
46+
47+
self.database = self.mongo_client[self.database]
48+
self.collection = self.database[self.collection]
49+
self.collection.insert_many(self.records)
50+
return(len(self.records))
51+
52+
except Exception as e:
53+
logging.error(e)
54+
raise NetworkSecurityException(e, sys)
55+
56+
57+
if __name__ == "__main__":
58+
FILE_PATH = "dataset\phisingData.csv"
59+
DATABASE = "ANIMESHDB"
60+
COLLECTION = "NETWORKDATA"
61+
networkobj = NetworkDataExtract()
62+
RECORDS = networkobj.cv_to_json_converter(file_path=FILE_PATH)
63+
no_of_records = networkobj.insert_data_mongodb(RECORDS, DATABASE, COLLECTION)
64+
print(f"Total {no_of_records} records inserted in MongoDB")
Binary file not shown.

test_mongodb.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
from pymongo.mongo_client import MongoClient
3+
import os
4+
from dotenv import load_dotenv
5+
6+
load_dotenv()
7+
8+
MONGO_DB_URI = os.getenv("MONGO_DB_URI")
9+
10+
11+
uri = MONGO_DB_URI
12+
13+
# Create a new client and connect to the server
14+
client = MongoClient(uri)
15+
16+
# Send a ping to confirm a successful connection
17+
try:
18+
client.admin.command('ping')
19+
print("Pinged your deployment. You successfully connected to MongoDB!")
20+
except Exception as e:
21+
print(e)

0 commit comments

Comments
 (0)