Để tạo một sơ đồ ERD (Entity-Relationship Diagram) cho việc crawl dữ liệu sản phẩm, chúng ta cần xác định các thực thể chính và mối quan hệ giữa chúng.
Dưới đây là một mô hình cơ bản cho sơ đồ ERD liên quan đến crawl dữ liệu sản phẩm.
Product (Sản phẩm)
Category (Danh mục)
Source (Nguồn dữ liệu)
CrawlHistory (Lịch sử crawl)
+----------------+ +----------------+ +-----------------+ +-------------------+ | Product | | Category | | Source | | CrawlHistory | +----------------+ +----------------+ +-----------------+ +-------------------+ | ProductID (PK) | | CategoryID (PK)| | SourceID (PK) | | CrawlID (PK) | | Name | | Name | | Name | | ProductID (FK) | | Description | | ParentCategoryID(FK)| | URL | | CrawlDate | | Price | +----------------+ +-----------------+ | Status | | CategoryID (FK)| | | Details | | SourceID (FK) | | +-------------------+ | CrawlDate | | +----------------+ | | +-----------------+ +-----------------+ +-----------------+ | Source | | Category | | Product | |<----------------+ +-----------------+ +-----------------+ | 1:N | | 1:N | | 1:N | +-----------------+ +-----------------+ +-----------------+
hoặc bạn có thể nhờ chatGPT thiết kế hộ ERD như thế này
Product:
Category:
Source:
CrawlHistory:
Nếu bạn muốn tôi tạo một sơ đồ ERD chi tiết hơn hoặc có thay đổi nào khác, hãy cho tôi biết liên hệ ở cuối bài
Dưới đây là một vài ví dụ về các mối quan hệ mẫu bạn có thể tham khảo
Dưới đây là 10 bài tập liên quan đến việc thiết kế và sử dụng sơ đồ ERD trong bối cảnh crawl dữ liệu sản phẩm.
Các bài tập này bao gồm từ việc thiết kế cơ sở dữ liệu đến việc thực hiện các truy vấn SQL để thao tác dữ liệu.
CREATE TABLE Category ( CategoryID INT PRIMARY KEY, Name VARCHAR(100) NOT NULL, ParentCategoryID INT, FOREIGN KEY (ParentCategoryID) REFERENCES Category(CategoryID) ); CREATE TABLE Source ( SourceID INT PRIMARY KEY, Name VARCHAR(100) NOT NULL, URL VARCHAR(255) NOT NULL ); CREATE TABLE Product ( ProductID INT PRIMARY KEY, Name VARCHAR(100) NOT NULL, Description TEXT, Price DECIMAL(10, 2), CategoryID INT, SourceID INT, CrawlDate DATETIME, FOREIGN KEY (CategoryID) REFERENCES Category(CategoryID), FOREIGN KEY (SourceID) REFERENCES Source(SourceID) ); CREATE TABLE CrawlHistory ( CrawlID INT PRIMARY KEY, ProductID INT, CrawlDate DATETIME, Status VARCHAR(50), Details TEXT, FOREIGN KEY (ProductID) REFERENCES Product(ProductID) );
SELECT * FROM Product WHERE CategoryID = 1;
SELECT * FROM Product WHERE SourceID = 2;
SELECT * FROM CrawlHistory WHERE ProductID = 1 ORDER BY CrawlDate DESC;
Các câu lệnh SQL trên chỉ là mẫu, bạn có thể cần điều chỉnh chúng dựa trên cấu trúc thực tế của cơ sở dữ liệu của bạn và yêu cầu cụ thể của từng bài tập.
Một vài câu truy vấn mẫu
Để lấy thông tin của sản phẩm bao gồm cả tên danh mục (category name) và tên nguồn (source name), chúng ta cần thực hiện một truy vấn SQL sử dụng các lệnh JOIN để kết hợp các bảng Product, Category và Source.
Sau đó, ta có thể định dạng dữ liệu kết quả thành JSON.
SELECT p.ProductID, p.Name AS ProductName, p.Description, p.Price, c.Name AS CategoryName, s.Name AS SourceName, p.CrawlDate FROM Product p JOIN Category c ON p.CategoryID = c.CategoryID JOIN Source s ON p.SourceID = s.SourceID;
Dưới đây là một ví dụ về JSON mẫu được tạo từ kết quả truy vấn trên:
[ { "ProductID": 1, "ProductName": "Laptop Dell Inspiron", "Description": "Laptop Dell Inspiron 15 3000 series", "Price": 750.00, "CategoryName": "Laptops", "SourceName": "Dell Official Store", "CrawlDate": "2024-06-20T10:00:00" }, { "ProductID": 2, "ProductName": "Smartphone Samsung Galaxy S21", "Description": "Samsung Galaxy S21 5G", "Price": 999.99, "CategoryName": "Smartphones", "SourceName": "Samsung Official Store", "CrawlDate": "2024-06-21T11:30:00" }, { "ProductID": 3, "ProductName": "Headphones Sony WH-1000XM4", "Description": "Sony WH-1000XM4 Wireless Noise-Cancelling Headphones", "Price": 349.99, "CategoryName": "Audio", "SourceName": "Sony Official Store", "CrawlDate": "2024-06-21T15:00:00" } ]
JSON trên thể hiện danh sách các sản phẩm với thông tin chi tiết về tên sản phẩm, mô tả, giá cả, tên danh mục và tên nguồn cùng với ngày crawl.
Mỗi mục trong danh sách là một đối tượng JSON chứa thông tin của một sản phẩm.
Để lấy số lượng sản phẩm của mỗi nguồn từ bảng Source, chúng ta cần thực hiện một truy vấn SQL sử dụng các lệnh JOIN và GROUP BY để đếm số sản phẩm của từng nguồn.
Kết quả của truy vấn sẽ là một danh sách các nguồn cùng với số lượng sản phẩm tương ứng.
SELECT s.SourceID, s.Name AS SourceName, COUNT(p.ProductID) AS ProductCount FROM Source s LEFT JOIN Product p ON s.SourceID = p.SourceID GROUP BY s.SourceID, s.Name;
Dưới đây là một ví dụ về JSON mẫu được tạo từ kết quả truy vấn trên:
[ { "SourceID": 1, "SourceName": "Dell Official Store", "ProductCount": 10 }, { "SourceID": 2, "SourceName": "Samsung Official Store", "ProductCount": 7 }, { "SourceID": 3, "SourceName": "Sony Official Store", "ProductCount": 5 } ]
Nếu bạn muốn lấy số lượng sản phẩm của một nguồn cụ thể, bạn có thể thêm điều kiện WHERE vào truy vấn:
SELECT s.SourceID, s.Name AS SourceName, COUNT(p.ProductID) AS ProductCount FROM Source s LEFT JOIN Product p ON s.SourceID = p.SourceID WHERE s.SourceID = 1 GROUP BY s.SourceID, s.Name;
Dưới đây là một ví dụ về JSON mẫu cho nguồn cụ thể với SourceID là 1:
{ "SourceID": 1, "SourceName": "Dell Official Store", "ProductCount": 10 }
Nếu bạn muốn điều chỉnh hoặc mở rộng truy vấn cho các yêu cầu khác, hãy cho tôi biết để tôi có thể cung cấp thêm hướng dẫn.
Để lấy thông tin về số lượng sản phẩm của một nguồn cụ thể cùng với chi tiết về các sản phẩm đó, chúng ta cần thực hiện hai truy vấn SQL kết hợp: một để đếm số lượng sản phẩm và một để lấy chi tiết sản phẩm.
Dưới đây là cách thực hiện điều này.
SELECT s.SourceID, s.Name AS SourceName, COUNT(p.ProductID) AS ProductCount FROM Source s LEFT JOIN Product p ON s.SourceID = p.SourceID WHERE s.SourceID = 1 GROUP BY s.SourceID, s.Name;
SELECT p.ProductID, p.Name AS ProductName, p.Description, p.Price, c.Name AS CategoryName, p.CrawlDate FROM Product p JOIN Category c ON p.CategoryID = c.CategoryID WHERE p.SourceID = 1;
Dưới đây là ví dụ về JSON mẫu kết hợp số lượng sản phẩm và chi tiết các sản phẩm của một nguồn cụ thể (SourceID = 1).
{ "SourceID": 1, "SourceName": "Dell Official Store", "ProductCount": 3, "Products": [ { "ProductID": 1, "ProductName": "Laptop Dell Inspiron", "Description": "Laptop Dell Inspiron 15 3000 series", "Price": 750.00, "CategoryName": "Laptops", "CrawlDate": "2024-06-20T10:00:00" }, { "ProductID": 2, "ProductName": "Dell Monitor 27 inch", "Description": "Dell UltraSharp 27 Monitor - U2720Q", "Price": 550.00, "CategoryName": "Monitors", "CrawlDate": "2024-06-21T09:00:00" }, { "ProductID": 3, "ProductName": "Dell Keyboard", "Description": "Dell Wired Keyboard - KB216", "Price": 30.00, "CategoryName": "Accessories", "CrawlDate": "2024-06-21T15:00:00" } ] }
Trong thực tế, bạn sẽ cần sử dụng một ngôn ngữ lập trình để thực hiện các truy vấn SQL này và kết hợp kết quả lại với nhau.
Dưới đây là một ví dụ bằng Python sử dụng thư viện sqlite3 để minh họa cách kết hợp kết quả từ hai truy vấn trên:
import sqlite3 import json # Kết nối đến cơ sở dữ liệu conn = sqlite3.connect('database.db') cursor = conn.cursor() # Truy vấn số lượng sản phẩm của nguồn cụ thể source_id = 1 cursor.execute(''' SELECT s.SourceID, s.Name AS SourceName, COUNT(p.ProductID) AS ProductCount FROM Source s LEFT JOIN Product p ON s.SourceID = p.SourceID WHERE s.SourceID = ? GROUP BY s.SourceID, s.Name ''', (source_id,)) source_info = cursor.fetchone() # Truy vấn chi tiết sản phẩm của nguồn cụ thể cursor.execute(''' SELECT p.ProductID, p.Name AS ProductName, p.Description, p.Price, c.Name AS CategoryName, p.CrawlDate FROM Product p JOIN Category c ON p.CategoryID = c.CategoryID WHERE p.SourceID = ? ''', (source_id,)) products = cursor.fetchall() # Định dạng kết quả thành JSON result = { "SourceID": source_info[0], "SourceName": source_info[1], "ProductCount": source_info[2], "Products": [ { "ProductID": row[0], "ProductName": row[1], "Description": row[2], "Price": row[3], "CategoryName": row[4], "CrawlDate": row[5] } for row in products ] } # Chuyển đổi kết quả thành JSON result_json = json.dumps(result, indent=4) print(result_json) # Đóng kết nối conn.close()
Mã Python trên thực hiện hai truy vấn SQL và kết hợp kết quả lại thành một cấu trúc JSON hoàn chỉnh.
Bạn có thể điều chỉnh và mở rộng mã này theo nhu cầu của mình.
Chắc chắn! Dưới đây là tóm tắt về quy trình lấy thông tin sản phẩm của một nguồn cụ thể cùng với số lượng sản phẩm và chi tiết sản phẩm dưới dạng JSON.
Thiết kế Cơ sở Dữ liệu:
Tạo các bảng SQL:
Thêm dữ liệu mẫu vào các bảng:
Truy vấn SQL để lấy thông tin:
Kết hợp kết quả và định dạng thành JSON:
SELECT s.SourceID, s.Name AS SourceName, COUNT(p.ProductID) AS ProductCount FROM Source s LEFT JOIN Product p ON s.SourceID = p.SourceID WHERE s.SourceID = 1 GROUP BY s.SourceID, s.Name;
SELECT p.ProductID, p.Name AS ProductName, p.Description, p.Price, c.Name AS CategoryName, p.CrawlDate FROM Product p JOIN Category c ON p.CategoryID = c.CategoryID WHERE p.SourceID = 1;
Dưới đây là ví dụ JSON mẫu từ kết quả của hai truy vấn trên:
{ "SourceID": 1, "SourceName": "Dell Official Store", "ProductCount": 3, "Products": [ { "ProductID": 1, "ProductName": "Laptop Dell Inspiron", "Description": "Laptop Dell Inspiron 15 3000 series", "Price": 750.00, "CategoryName": "Laptops", "CrawlDate": "2024-06-20T10:00:00" }, { "ProductID": 2, "ProductName": "Dell Monitor 27 inch", "Description": "Dell UltraSharp 27 Monitor - U2720Q", "Price": 550.00, "CategoryName": "Monitors", "CrawlDate": "2024-06-21T09:00:00" }, { "ProductID": 3, "ProductName": "Dell Keyboard", "Description": "Dell Wired Keyboard - KB216", "Price": 30.00, "CategoryName": "Accessories", "CrawlDate": "2024-06-21T15:00:00" } ] }
Sử dụng mã Python để thực hiện hai truy vấn SQL và kết hợp kết quả thành JSON:
import sqlite3 import json # Kết nối đến cơ sở dữ liệu conn = sqlite3.connect('database.db') cursor = conn.cursor() # Truy vấn số lượng sản phẩm của nguồn cụ thể source_id = 1 cursor.execute(''' SELECT s.SourceID, s.Name AS SourceName, COUNT(p.ProductID) AS ProductCount FROM Source s LEFT JOIN Product p ON s.SourceID = p.SourceID WHERE s.SourceID = ? GROUP BY s.SourceID, s.Name ''', (source_id,)) source_info = cursor.fetchone() # Truy vấn chi tiết sản phẩm của nguồn cụ thể cursor.execute(''' SELECT p.ProductID, p.Name AS ProductName, p.Description, p.Price, c.Name AS CategoryName, p.CrawlDate FROM Product p JOIN Category c ON p.CategoryID = c.CategoryID WHERE p.SourceID = ? ''', (source_id,)) products = cursor.fetchall() # Định dạng kết quả thành JSON result = { "SourceID": source_info[0], "SourceName": source_info[1], "ProductCount": source_info[2], "Products": [ { "ProductID": row[0], "ProductName": row[1], "Description": row[2], "Price": row[3], "CategoryName": row[4], "CrawlDate": row[5] } for row in products ] } # Chuyển đổi kết quả thành JSON result_json = json.dumps(result, indent=4) print(result_json) # Đóng kết nối conn.close()
Sau khi chế biến sẽ ra được kết quả tựa như thế này sẽ được hướng dẫn ở các bài sau
Đây nha
Quy trình trên giúp bạn lấy thông tin sản phẩm từ cơ sở dữ liệu, bao gồm số lượng sản phẩm của một nguồn cụ thể và chi tiết các sản phẩm đó, và định dạng kết quả thành JSON.
Bạn có thể tùy chỉnh các truy vấn và mã Python này theo nhu cầu của mình để mở rộng và tối ưu hóa hơn nữa.
#Mtips5s #Contact
Fanpage: https://www.facebook.com/mtipscoder
Group trao đổi, chia sẻ: https://www.facebook.com/groups/mtipscoder
Website: https://mtips5s.com
Youtube: https://mtips5s.com
Twitter(X): @takagiks99
Instagram: @khuongkara
Threads: @khuongkara
Google Maps: @khuongkara
#Base Code #Souce Code
Bộ công cụ My Self: @tools.mtips5s.com
Github: @github
Npm: @npm
Docker: @docker
Chúc các bạn thành công!
Leave A Comment