Requirement & Fulfillment:
-
Get ransomware data from given link. --> Downloaded from given link
-
Given this JSON as a sample, how will you write a short script/program to process this json and store into databases --> Script has been uploaded in repo
-
Explain selection of coding language and DB of your choice. --> Coding Language: Python Python is a versatile language with extensive support for handling JSON data and interfacing with various databases. It has powerful librarie for data manipulation, parsing JSON, and for database operations.
--> Database: PostgreSQL PostgreSQL is an advanced, open-source relational database with a strong reputation for reliability, feature robustness, and performance. It supports JSON data types, which makes it ideal for storing JSON data without losing the structure.
-
Given that its large set of data, how will you ensure that no records are missed when writing into the DB --> Missing rows if any are identified during insert and tackled & recorded for further investigation.
-
In the case of duplicate entries, how will you handle them? --> Before inserting new data, compare it with existing records to avoid duplicates.
Database ER Diagram:
Detailed Steps
Overall:
- Use Case understanding
- Tech Stack Selection
- Setup Enviroment - Server (EC2 instance) and Database (Postgres)
- Design Tables - ER Diagram
- Code flow chart
- Develop code
- Test code
- Document
Code Steps:
- Start: Begin the script execution.
- Load JSON Data: Load data from ransomware_overview.json.
- Initialize Tracking Variables: Initialize variables to track total records, inserted records, duplicate records, missed records, and missed rows.
- Setup Logging: Configure logging with a timestamped log file.
- Database Connection: Attempt to connect to the database.
- On success, proceed to create tables.
- On failure, log the error and exit.
- Create Tables: Execute SQL commands to create ransomware and ransomware_details tables.
- On success, proceed to insert data.
- On failure, log the error, rollback, and exit.
- Insert Data Function: Define the function to insert data into the database.
- For Each Entry in Data: Loop through each entry in the JSON data.
- Insert into Ransomware Table: Attempt to insert data into the ransomware table.
- On success, retrieve the ransomware_id.
- On IntegrityError, rollback the transaction, and retrieve the existing ransomware_id.
- Insert into Ransomware Details Table: Attempt to insert data into the ransomware_details table.
- On success, check the row count to determine if it was a duplicate.
- On IntegrityError, log the error, rollback, and add the entry to missed rows.
- Check Row Count: Check if the row count is zero.
- If zero, increment duplicate records and add to missed rows.
- If greater than zero, increment inserted records.
- Commit Transaction: Commit the transaction.
- All Entries Processed: After processing all entries, close the database connection.
- Write Missed Rows to JSON File: Write missed rows to missed_rows.json.
- Output Summary: Print the summary of total records, inserted records, duplicate records, and missed records.
- End: End the script execution.
Screenshot:
- PostgreSQL DB Creation:
- Script Execution to create table and insert data
Introduced deliberatly miss of row by changing data type to limited legnth varchar(2)