运行Spark程序-在shell中运行

Spark Shell运行程序步骤

  1. 启动Spark Shell
    根据语言选择启动命令:
  • Scala版本(默认):执行spark-shell
  • Python版本:执行pyspark
  1. 数据加载示例
    读取本地文本文件:
scala 复制代码
// Scala版本
val textData = sc.textFile("file:///path/to/file.txt")

// Python版本
text_data = sc.textFile("file:///path/to/file.txt")
  1. 执行数据处理
    实现词频统计(两种语言示例):
scala 复制代码
// Scala版本
val wordCounts = textData
  .flatMap(_.split(" "))
  .map(word => (word, 1))
  .reduceByKey(_ + _)
  
wordCounts.collect().foreach(println)
python 复制代码
# Python版本
word_counts = text_data \
    .flatMap(lambda line: line.split(" ")) \
    .map(lambda word: (word, 1)) \
    .reduceByKey(lambda a,b: a+b)

word_counts.collect()
  1. 结果输出
    保存到HDFS(两种语言通用):
scala 复制代码
wordCounts.saveAsTextFile("hdfs:///output/path")
  1. 退出环境
    输入命令:quitCtrl+D

注意事项

  1. 路径说明
  • 本地文件需加file://前缀
  • 集群文件使用hdfs://协议头
  1. 执行触发

    转换操作(如map/filter)需要执行动作(如collect/count)才会触发计算

  2. 配置调优

    启动时可添加参数:

bash 复制代码
spark-shell --master yarn --executor-memory 4g
  1. 日志控制
    在Shell中调整日志级别:
scala 复制代码
sc.setLogLevel("WARN")

验证示例

在Shell中运行快速验证:

scala 复制代码
// 创建测试RDD
val nums = sc.parallelize(1 to 100)
println(s"数据总量:${nums.count()}") 
相关推荐
starzy19903 小时前
Spark 核心之 Spark-SortShuffle 原理深度剖析
大数据·spark
用户3610588626128 小时前
Spark 核心之 Spark 内存管理深度剖析
大数据·spark
乌恩大侠1 天前
【AI-RAN】硬件产品:DELL 前传交换机
人工智能·spark·aerial·o-ru·ai-ran
starzy19901 天前
Spark 核心之自定义累加器以及版本对比变化深度剖析
大数据·ajax·spark
用户3610588626121 天前
Spark 核心之 Spark-SortShufflebypass 原理深度剖析
大数据·spark
starzy19901 天前
Spark 核心之二次排序、分组取 TopN 优化分析
大数据·分布式·spark
乌恩大侠2 天前
图解 AI-RAN开发需要哪些软硬件:O-RU、DGX Spark、Sionna与Aerial
人工智能·spark·o-ru·ai-ran
阿里云大数据AI技术3 天前
阿里云 EMR Daft AI Function:用 DataFrame 表达式搞定大模型调用与多模态向量化
人工智能·spark
BD_Marathon3 天前
部署Spark
大数据·javascript·spark
Blossom i3 天前
Python+spark2.0+Hadoop机器学习与大数据实战第十一章:Python Spark的集成开发环境
大数据·hadoop·spark