- Subquery (truy vấn con) là một câu truy vấn SELECT nằm bên trong một câu truy vấn khác
- Còn được gọi là Inner Query hoặc Nested Query
- Câu truy vấn chính chứa subquery được gọi là Outer Query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import psycopg2 | |
# Database connection parameters | |
dbname = "dvd_rental" | |
user = "postgres" | |
password = "1234" | |
host = "localhost" # Defaults to localhost if not specified | |
port = "5432" # Defaults to 5432 if not specified | |
try: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import psycopg2 | |
import pandas as pd | |
import time | |
from sqlalchemy import create_engine | |
start_time = time.perf_counter() | |
# Database connection parameters | |
dbname = "dvd_rental" | |
user = "postgres" |
Database mẫu về hệ thống quản lý bán hàng để bạn có thể thực hành.
-- Tạo database
CREATE DATABASE IF NOT EXISTS sales_management;
USE sales_management;
-- Bảng Danh mục sản phẩm
Giải thích chi tiết về dòng code from pyspark.sql import SparkSession
:
-
Đây là một câu lệnh import trong Python, được sử dụng để nhập (import) một module/class cụ thể.
-
Phân tích từng phần:
-
from pyspark.sql
: chỉ định đường dẫn đến package/module. Trong đó:pyspark
là thư viện chính để làm việc với Apache Spark trong Pythonsql
là một module con trong pyspark, chứa các công cụ để xử lý dữ liệu dạng bảng
-
import SparkSession
: SparkSession là một class rất quan trọng, là điểm khởi đầu để làm việc với Spark SQL
-
Giải thích chi tiết từng phần của câu lệnh khởi tạo SparkSession này:
-
spark =
: Tạo một biến tên "spark" để lưu trữ đối tượng SparkSession -
SparkSession.builder
:builder
là một phương thức để bắt đầu xây dựng cấu hình cho SparkSession- Sử dụng mẫu thiết kế Builder để cấu hình linh hoạt
-
.appName("TelcoChurnAnalysis")
:- Đặt tên cho ứng dụng Spark là "TelcoChurnAnalysis"
BƯỚC 1: CHUẨN BỊ DỮ LIỆU
- Đọc dữ liệu từ file
- Loại bỏ 5 cột theo yêu cầu:
- MonthlyCharges
- OnlineSecurity
- StreamingTV
- InternetService
- Partner
- Chuyển đổi cột Churn từ Yes/No sang 1/0
OlderNewer