当前位置: 首页 > web >正文

Spark,Idea中编写Spark程序 2

Idea中编写Spark程序

一、修改pom.xml文件

<build><sourceDirectory>src/main/scala</sourceDirectory><testSourceDirectory>src/test/scala</testSourceDirectory>
<!--        添加必要的插件以打包scala程序--><plugins><plugin><groupId>net.alchim31.maven</groupId><artifactId>scala-maven-plugin</artifactId><version>3.4.6</version><executions><execution><goals><goal>compile</goal><goal>testCompile</goal></goals></execution></executions><configuration><args><arg>-dependencyfile</arg><arg>${project.build.directory}/.scala_dependencies</arg></args></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>2.4.3</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals></execution></executions><configuration><filters><filter><artifact>*:*</artifact><excludes><exclude>META-INF/*.SF</exclude><exclude>META-INF/*.DSA</exclude><exclude>META-INF/*.RSA</exclude></excludes></filter></filters><transformers><transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainClass></mainClass></transformer></transformers></configuration></plugin></plugins></build>

二、修改代码

修改两个地方:输入目录改成args(0), 输出的目录改成args(1)。

import org.apache.spark.{SparkConf, SparkContext}object WordCount_online {// 写一个spark程序,统计input目录下所有文本文件中单词的词频// 把结果保存在output下def main(args: Array[String]): Unit = {
//    println("hello spark!")// 配置 Spark 应用程序val conf = new SparkConf().setAppName("WordCount_online")// 创建 SparkContext 对象val sc = new SparkContext(conf)// 读取目录下的所有文本文件val textFiles = sc.wholeTextFiles(args(0))// 提取文本内容并执行 WordCount 操作val counts = textFiles.flatMap { case (_, content) => content.split("\\s+") }.map(word => (word, 1)).reduceByKey(_ + _)// 将所有分区的数据合并成一个分区val singlePartitionCounts = counts.coalesce(1)// 保存结果到文件singlePartitionCounts.saveAsTextFile(args(1))// 停止 SparkContextsc.stop()}
}

三、打包

四、上传到集群

命令为:[root@hadoop100 sbin]# spark-submit --class WordCount_online --master yarn /opt/module/original-untitled-1.0-SNAPSHOT.jar /wcinput /output1

出现结果如下即为运行成功

http://www.xdnf.cn/news/3901.html

相关文章:

  • 题解:AT_abc245_e [ABC245E] Wrapping Chocolate
  • Go语言中的无锁数据结构与并发效率优化
  • Circular Plot系列(三):【视频教程】复现NCS图表之高大上的单细胞UMAP环形图
  • process terminated with status -1073741515
  • 永久免费的Google Colab 入门指南
  • C语言——寻找子串
  • 动态规划--回文串问题
  • 【深度学习-Day 5】Python 快速入门:深度学习的“瑞士军刀”实战指南
  • Vue常用优化
  • d3_v7绘制折线图
  • 启发式算法-遗传算法
  • C++ - 类和对象 #类的默认成员函数 #构造函数 #析构函数 #拷贝构造函数 #运算符重载函数 #赋值运算符重载函数
  • AI 入门:关键概念
  • 高等数学同步测试卷 同济7版 试卷部分 上 做题记录 第四章 不定积分同步测试卷 B卷
  • n8n 快速入门1:构建一个简单的工作流
  • 强化学习机器人模拟器——GridWorld:一个用于强化学习的 Python 环境
  • unorder_map/set的底层实现---C++
  • ESP32S3 多固件烧录方法、合并多个固件为单一固件方法
  • LangChain4J-XiaozhiAI 项目分析报告
  • 线程间通信--线程间顺序控制
  • C++类_局部类
  • 安装与配置Go语言开发环境 -《Go语言实战指南》
  • C#与西门子PLC通信:S7NetPlus和HslCommunication使用指南
  • JavaWeb:SpringBootWeb快速入门
  • 五、shell脚本--函数与脚本结构:搭积木,让脚本更有条理
  • JavaScript 中的 Proxy 与 Reflect 教程
  • 比特、字节与布尔逻辑:计算机数据存储与逻辑运算的底层基石
  • PMP-第四章 项目整合管理(一)
  • 享元模式(Flyweight Pattern)
  • MOS管极间电容参数学习