Java技术栈 —— Spark入门(二)之实时WordCount

Java技术栈 ------ Spark入门(二)

  • 一、kafka
    • [1.1 创建topic](#1.1 创建topic)
    • [1.2 准备input与查看output](#1.2 准备input与查看output)
  • 二、spark
    • [2.1 spark下的程序文件](#2.1 spark下的程序文件)
    • [2.2 用spark-submit提交作业](#2.2 用spark-submit提交作业)

参考文章:

参考文章或视频链接
1 《Kafka + Spark Stream实时WordCount》

实验环境:

假设你的用户为root,以下软件安装路径为/opt

软件版本
spark: 3.5.2 (scala 2.12)
kafka: 3.8.0 (scala 2.13)

实验结构图

一、kafka

1.1 创建topic

sh 复制代码
# 创建input
bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic test.wordcount.input --partitions 1 --replication-factor 1
# 创建output
bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic test.wordcount.output --partitions 1 --replication-factor 1

1.2 准备input与查看output

sh 复制代码
# 打开两个terminal终端
# 准备键盘输入作为prodcuer
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test.wordcount.input
# 在屏幕上查看输出
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test.wordcount.output

二、spark

2.1 spark下的程序文件

python 复制代码
# coding=utf-8
# /opt/spark-3.5.2-bin-hadoop3/jobs/pyjobs/kafka-wordcount.py
from __future__ import print_function
from pyspark.sql import SparkSession
from pyspark.sql.functions import explode
from pyspark.sql.functions import split
from pyspark.sql import functions as F

bootstrapServers = "localhost:9092"

spark = SparkSession\
    .builder\
    .appName("StructuredKafkaWordCount")\
    .getOrCreate()

# 基于来自kafka的数据流,创建dataframe
lines = spark\
    .readStream\
    .format("kafka")\
    .option("kafka.bootstrap.servers", bootstrapServers)\
    .option("subscribe", "test.wordcount.input")\
    .option("failOnDataLoss", False)\
    .option("group.id", "wordcount-group3")\
    .load()\
    .selectExpr("CAST(value AS STRING)")

# 将单行数据拆分,转成多行数据
words = lines.select(
    explode(split(lines.value, ' ')).alias('word')
)

# 对单词进行分组,并计算总数
wordCounts = words.groupBy('word').count()

# 将两列数据合并成单列数据
wordCounts = wordCounts.select(F.concat(F.col("word"), F.lit("|"), F.col("count").cast("string")).alias("value"))

# 测试时,可以不将结果写入kafka,直接输出到控制台
# query = wordCounts \
#     .writeStream \
#     .outputMode("complete") \
#     .format("console") \
#     .start()

# 将结果输出到 test.wordcount.output
query = wordCounts \
    .writeStream \
    .format('kafka') \
    .outputMode('update') \
    .option("kafka.bootstrap.servers", bootstrapServers) \
    .option('checkpointLocation', '/spark/job-checkpoint') \
    .option("topic", "test.wordcount.output") \
    .start()

query.awaitTermination()

2.2 用spark-submit提交作业

shell 复制代码
# 提交Spark作业,这个过程需要保证网络畅通,会将一些依赖下载到/root/.ivy2/jars目录下
$SPARK_HOME/bin/spark-submit \
--packages org.apache.spark:spark-sql-kafka-0-10_2.12:3.5.2,\
org.apache.kafka:kafka-clients:3.5.2 \
/opt/spark-3.5.2-bin-hadoop3/jobs/pyjobs/kafka-wordcount.py
相关推荐
黄贵根5 小时前
JavaScript实现教培行业意向登记系统
开发语言·javascript·ecmascript
zzzll11118 小时前
Claude Code离线安装方案揭秘
开发语言·php
mifengxing9 小时前
LeetCode 41.缺失的第一个正数|Hard题O(n)+O(1)最优解法深度解析
java·算法·leetcode·排序算法
wling03019 小时前
vasp虚频计算-python脚本
开发语言·windows·python·vasp计算
郝学胜-神的一滴9 小时前
Qt 高级编程 040:按钮悬浮弹出滑块弹窗的完整攻略
开发语言·c++·qt·软件工程·用户界面
xrandzj10 小时前
Python面向对象编程入门:类、实例、初始化与封装实践
开发语言·python
DLYSB_11 小时前
API 网关流量洪峰与突发 CC 攻击:我用 Go 写了个“现场物理防御哨兵”,把故障响应压缩到秒级
开发语言·后端·golang·报警灯
markinmarkin11 小时前
Spring 中Bean 的作用域有哪些?
java·后端·spring
山荷枝12 小时前
Java学习第十天
java·学习
matlabgoodboy13 小时前
计算机毕设代做|Java Python Matlab APP 全套开发设计
java·python·课程设计