环境速览(
节点 新装组件 端口 备注
node1 Hive3.1.3 + MySQL 8.0 + Spark3.5.0 3306/10000/8080 HiveServer2 WebUI
node2 Spark HistoryServer 18080 日志持久化到 HDFS
node3 Kafka 3.7.0 + ZooKeeper(复用) 9092 单 broker,topic=user_event
Metastore 三模式:内嵌、本地、远程;生产必用远程(MySQL/Postgres)。
ORC + Snappy 压缩比 ≈ 1:5,查询快 8× 于 Text。
动态分区:需开 set hive.exec.dynamic.partition=true,防止生成过多小文件用 distributed by + sort by。
Spark on YARN
部署模式:client(driver 在本地) vs cluster(driver 在 YARN AM)。
资源参数:
--num-executors 4 --executor-cores 2 --executor-memory 3g
Spark History 需提前 spark.eventLog.enabled=true 并指向 hdfs:///spark-logs。
Kafka → HBase 实时
Kafka Connect API 可插件式,但学习阶段用 Python kafka-python 裸写最快。
HBase 缓冲写:
htable.setAutoFlush(false, true) + put.setWriteToWAL(false)(允许少量数据丢失换吞吐)。
小文件规避:Kafka 分区数 = RegionServer 数 × 2,保证写分散。
实操流水
MySQL & Hive Metastore
bash
node1
yum install -y mysql-server
systemctl enable --now mysqld
mysql -e "CREATE DATABASE hivemetastore;"
mysql -e "CREATE USER 'hive'@'%' IDENTIFIED BY 'Hive#123';"
mysql -e "GRANT ALL ON hivemetastore.* TO 'hive'@'%';"
初始化 schema
schematool -dbType mysql -initSchema
Hive 基本命令
sql
CREATE TABLE ds_customer
STORED AS ORC
AS SELECT * FROM customer;
-- 1 GB 数据 182 s,ORC 后 208 MB
TPC-DS 查询 01
sql
-- 单表扫描 28 M 行
SELECT count(*) FROM ds_customer WHERE c_birth_year = 1988;
-- Hive 42 s,SparkSQL 9 s(见下)
Spark 部署
bash
解压到 /opt/spark,同步到 3 节点
spark-defaults.conf
spark.eventLog.enabled true
spark.eventLog.dir hdfs://node1:9000/spark-logs
spark.history.fs.logDirectory hdfs://node1:9000/spark-logs
start-history-server.sh
SparkPi(cluster 模式)
bash
spark-submit
--master yarn --deploy-mode cluster
--class org.apache.spark.examples.SparkPi
$SPARK_HOME/examples/jars/spark-examples_2.12-3.5.0.jar 1000
估算 Pi ≈ 3.141592,耗时 27 s(MapReduce Pi 18 s/1000 map)
因启动开销大,大作业才能体现优势
WordCount 1 GB 文本
表格
框架 耗时 吞吐
MapReduce 4 min 12 s 4.0 MB/s
Spark 1 min 05 s 15.7 MB/s
提速 ↓ 74 % ↑ 3.9×
Kafka 安装
node3
tar -zxvf kafka_2.13-3.7.0.tgz -C /opt
bin/zookeeper-server-start.sh -daemon config/zookeeper.properties # 已存在,仅起 broker
bin/kafka-server-start.sh -daemon config/server.properties
创建 topic
bin/kafka-topics.sh --create --topic user_event --partitions 6 --replication-factor 2
Python 实时写 HBase
Python
from kafka import KafkaConsumer
import happybase
consumer = KafkaConsumer('user_event', bootstrap_servers='node3:9092')
conn = happybase.Connection('node1')
table = conn.table('user_event')
for msg in consumer:
key = msg.key.decode()
val = msg.value.decode()
table.put(key, {'cf:raw': val})
测试:
kcat -P -b node3:9092 -t user_event -K: <<< "u100:login"
HBase shell 能立刻 get 'user_event','u100'