A custom SQL Server Connector for ETL processes with Pandas
Project description
SQL Server Connector
Thư viện kết nối SQL Server chuyên dụng cho các tác vụ ETL, được tối ưu hóa cho Pandas, hỗ trợ Tiếng Việt (Unicode) và Upsert (Merge) hiệu năng cao.
🚀 Tính năng nổi bật
- High Performance: Sử dụng
fast_executemanygiúp insert dữ liệu nhanh gấp 10-50 lần so với thông thường. - Smart Upsert: Tự động chèn mới (Insert) hoặc cập nhật (Update) dựa trên Khóa chính (Primary Key).
- Schema Evolution: Tự động tạo bảng nếu chưa có, tự động thêm cột mới (Add Column) nếu DataFrame có thay đổi.
- Unicode Support: Xử lý triệt để lỗi font chữ Tiếng Việt khi làm việc với SQL Server & Pandas.
- SQLAlchemy 2.0: Tuân thủ chuẩn kết nối hiện đại, an toàn.
📦 Cài đặt
Cách 1: Cài đặt trực tiếp từ Git (Khuyên dùng nội bộ)
Dành cho đồng nghiệp trong team, cài đặt không cần file whl.
# Cài phiên bản mới nhất từ nhánh main
pip install git+https://github.com/johnnyb1509/sqlServerConnector.git
Cách 2: Cài đặt từ file .whl
Dành cho người dùng cuối, cài đặt từ file whl đã build sẵn.
pip install sqlServerConnector
Cấu hình kết nối Database
File cấu hình db_config.yaml
# Thông tin kết nối Database
# Lưu ý: Đảm bảo máy tính đã cài đặt ODBC Driver 17 for SQL Server
db_info:
server: "localhost" # Ví dụ: localhost hoặc
database: "YOUR_DATABASE_NAME" # Ví dụ: TestDB
username: "YOUR_USERNAME" # Ví dụ: sa
password: "YOUR_PASSWORD" # Mật khẩu
📝 Hướng dẫn sử dụng nhanh
- Khởi tạo kết nối
import yaml
from connector import SQLServerConnector
# Load config
with open('config/db_config.yaml', 'r') as f:
conf = yaml.safe_load(f)['db_info']
# Khởi tạo
db = SQLServerConnector(
server=conf['server'],
database=conf['database'],
username=conf['username'],
password=conf['password']
)
- Lấy dữ liệu (Read)
# Cách 1: Lấy toàn bộ bảng
df = db.get_data("DM_KhachHang")
# Cách 2: Dùng câu lệnh SQL tùy ý
query = """
SELECT TOP 100 * FROM Sales_Transaction
WHERE created_date >= '2023-01-01'
"""
df_sales = db.get_data(query)
print(df_sales.head())
- Ghi dữ liệu (Upsert)
import pandas as pd
# Giả lập dữ liệu
data = {
'TransactionID': [101, 102],
'Product': ['Laptop Dell', 'Chuột Logitech'], # Hỗ trợ tiếng Việt
'Amount': [15000000, 250000]
}
df_new = pd.DataFrame(data)
# Đẩy vào DB
db.upsert_data(
df=df_new,
target_table="Fact_Sales",
primary_key="TransactionID", # Cột dùng để định danh (tránh trùng lặp)
auto_evolve_schema=True # Tự động thêm cột nếu thiếu
)
print("Dữ liệu đã được upsert thành công!")
- Đóng kết nối
# Luôn đóng kết nối khi hoàn tất để giải phóng tài nguyên
db.dispose()
⚠️ Lưu ý quan trọng
-
Primary Key: Khi dùng upsert_data, bắt buộc phải cung cấp primary_key. Nếu bảng chưa có Primary Key, thư viện sẽ tự set cột đó làm khóa chính khi tạo bảng mới.
-
Date Time: Các cột ngày tháng nên được convert sang datetime64[ns] trong Pandas trước khi đẩy vào để đảm bảo tính chính xác.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlserverconnector-0.1.2.tar.gz.
File metadata
- Download URL: sqlserverconnector-0.1.2.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0252a1be707c0f2fc8f8e083565e9c518a406eb69eaf8b941e2d28315e9566a7
|
|
| MD5 |
403d9f5aafd52fbf687da5065d346218
|
|
| BLAKE2b-256 |
e621e6bef5c53f31ae8cdabf5cebe3863185bc944d99b097e20bd447a5c1d784
|
File details
Details for the file sqlserverconnector-0.1.2-py3-none-any.whl.
File metadata
- Download URL: sqlserverconnector-0.1.2-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ede166e038486edb484971112e4e8ac8012c51b80140b95271486a704be8428
|
|
| MD5 |
78ba4f930cf71f2eed24790abb1c4405
|
|
| BLAKE2b-256 |
f32f11919247c1df13b859e946a2575027a497dd74a85bfe19c34d0d2dc57d24
|