当前位置:   article > 正文

idea里搭建spark、mapreduce开发环境_idea2022社区版配置mapreduce

idea2022社区版配置mapreduce

1、安装scala的idea插件:

file —— settings —— plugins ,输入scala,搜索插件下载安装,注意版本:

2、配置scala的SDK:

先下载解压scala,直接从linux服务器端解压一个就行

file —— project structure —— library,配置之后,new就可以看到scala class了:

配置spark和scala的环境变量:

分别下载hadoop,spark和scala解压,增加环境变量:

3、新建maven项目:

file —— new project —— maven ,

有2个xml配置文件如下:

(1)pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>Learn-BigData</groupId>
  7. <artifactId>bigdata</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <properties>
  10. <maven.compiler.source>1.8</maven.compiler.source>
  11. <maven.compiler.target>1.8</maven.compiler.target>
  12. <scala.version>2.11.8</scala.version>
  13. <spark.version>2.4.0</spark.version>
  14. <hadoop.version>2.8.5</hadoop.version>
  15. <encoding>UTF-8</encoding>
  16. </properties>
  17. <repositories>
  18. <repository>
  19. <id>nexus-aliyun</id>
  20. <name>Nexus aliyun</name>
  21. <url>http://maven.aliyun.com/nexus/content/groups/public</url>
  22. </repository>
  23. </repositories>
  24. <dependencies>
  25. <!-- 导入scala的依赖 -->
  26. <dependency>
  27. <groupId>org.scala-lang</groupId>
  28. <artifactId>scala-library</artifactId>
  29. <version>${scala.version}</version>
  30. <scope>compile</scope>
  31. </dependency>
  32. <!-- 导入spark的依赖 -->
  33. <dependency>
  34. <groupId>org.apache.spark</groupId>
  35. <artifactId>spark-core_2.11</artifactId>
  36. <version>${spark.version}</version>
  37. <scope>compile</scope>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.apache.spark</groupId>
  41. <artifactId>spark-sql_2.11</artifactId>
  42. <version>${spark.version}</version>
  43. <scope>compile</scope>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.apache.spark</groupId>
  47. <artifactId>spark-hive_2.11</artifactId>
  48. <version>${spark.version}</version>
  49. <scope>compile</scope>
  50. </dependency>
  51. <!-- 指定hadoop-client API的版本 -->
  52. <dependency>
  53. <groupId>org.apache.hadoop</groupId>
  54. <artifactId>hadoop-client</artifactId>
  55. <version>${hadoop.version}</version>
  56. <scope>compile</scope>
  57. </dependency>
  58. <dependency>
  59. <groupId>org.apache.hadoop</groupId>
  60. <artifactId>hadoop-common</artifactId>
  61. <version>${hadoop.version}</version>
  62. <scope>compile</scope>
  63. </dependency>
  64. <dependency>
  65. <groupId>org.apache.hadoop</groupId>
  66. <artifactId>hadoop-hdfs</artifactId>
  67. <version>${hadoop.version}</version>
  68. <scope>compile</scope>
  69. </dependency>
  70. <dependency>
  71. <groupId>org.apache.hadoop</groupId>
  72. <artifactId>hadoop-mapreduce-client-common</artifactId>
  73. <version>${hadoop.version}</version>
  74. <scope>compile</scope>
  75. </dependency>
  76. <dependency>
  77. <groupId>org.apache.hadoop</groupId>
  78. <artifactId>hadoop-mapreduce-client-core</artifactId>
  79. <version>${hadoop.version}</version>
  80. <scope>compile</scope>
  81. </dependency>
  82. <dependency>
  83. <groupId>org.apache.hadoop</groupId>
  84. <artifactId>hadoop-mapreduce-client-jobclient</artifactId>
  85. <version>${hadoop.version}</version>
  86. <scope>compile</scope>
  87. </dependency>
  88. <dependency>
  89. <groupId>commons-cli</groupId>
  90. <artifactId>commons-cli</artifactId>
  91. <version>1.3.1</version>
  92. <scope>compile</scope>
  93. </dependency>
  94. <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-streaming -->
  95. <dependency>
  96. <groupId>org.apache.spark</groupId>
  97. <artifactId>spark-streaming_2.11</artifactId>
  98. <version>${spark.version}</version>
  99. <scope>compile</scope>
  100. </dependency>
  101. <dependency>
  102. <groupId>org.apache.spark</groupId>
  103. <artifactId>spark-mllib_2.11</artifactId>
  104. <version>${spark.version}</version>
  105. <scope>compile</scope>
  106. </dependency>
  107. <dependency>
  108. <groupId>commons-configuration</groupId>
  109. <artifactId>commons-configuration</artifactId>
  110. <version>1.6</version>
  111. <scope>compile</scope>
  112. </dependency>
  113. </dependencies>
  114. <build>
  115. <pluginManagement>
  116. <plugins>
  117. <!-- 编译scala的插件 -->
  118. <plugin>
  119. <groupId>net.alchim31.maven</groupId>
  120. <artifactId>scala-maven-plugin</artifactId>
  121. <version>3.2.2</version>
  122. </plugin>
  123. <!-- 编译java的插件 -->
  124. <plugin>
  125. <groupId>org.apache.maven.plugins</groupId>
  126. <artifactId>maven-compiler-plugin</artifactId>
  127. <version>3.5.1</version>
  128. </plugin>
  129. </plugins>
  130. </pluginManagement>
  131. <plugins>
  132. <plugin>
  133. <groupId>net.alchim31.maven</groupId>
  134. <artifactId>scala-maven-plugin</artifactId>
  135. <executions>
  136. <execution>
  137. <id>scala-compile-first</id>
  138. <phase>process-resources</phase>
  139. <goals>
  140. <goal>add-source</goal>
  141. <goal>compile</goal>
  142. </goals>
  143. </execution>
  144. <execution>
  145. <id>scala-test-compile</id>
  146. <phase>process-test-resources</phase>
  147. <goals>
  148. <goal>testCompile</goal>
  149. </goals>
  150. </execution>
  151. </executions>
  152. </plugin>
  153. <plugin>
  154. <groupId>org.apache.maven.plugins</groupId>
  155. <artifactId>maven-compiler-plugin</artifactId>
  156. <executions>
  157. <execution>
  158. <phase>compile</phase>
  159. <goals>
  160. <goal>compile</goal>
  161. </goals>
  162. </execution>
  163. </executions>
  164. </plugin>
  165. <!-- 打jar插件 -->
  166. <plugin>
  167. <groupId>org.apache.maven.plugins</groupId>
  168. <artifactId>maven-shade-plugin</artifactId>
  169. <version>2.4.3</version>
  170. <configuration>
  171. <createDependencyReducedPom>false</createDependencyReducedPom>
  172. </configuration>
  173. <executions>
  174. <execution>
  175. <phase>package</phase>
  176. <goals>
  177. <goal>shade</goal>
  178. </goals>
  179. <configuration>
  180. <filters>
  181. <filter>
  182. <artifact>*:*</artifact>
  183. <excludes>
  184. <exclude>META-INF/*.SF</exclude>
  185. <exclude>META-INF/*.DSA</exclude>
  186. <exclude>META-INF/*.RSA</exclude>
  187. </excludes>
  188. </filter>
  189. </filters>
  190. </configuration>
  191. </execution>
  192. </executions>
  193. </plugin>
  194. </plugins>
  195. </build>
  196. </project>

(2)dependency-reduced-pom.xml,这个文件是打包时生成的,没啥用。

4、编写JavaWordCount

  1. package cn.edu360.spark;
  2. import org.apache.spark.SparkConf;
  3. import org.apache.spark.api.java.JavaPairRDD;
  4. import org.apache.spark.api.java.JavaRDD;
  5. import org.apache.spark.api.java.JavaSparkContext;
  6. import org.apache.spark.api.java.function.FlatMapFunction;
  7. import org.apache.spark.api.java.function.Function2;
  8. import org.apache.spark.api.java.function.PairFunction;
  9. import scala.Tuple2;
  10. import java.util.Arrays;
  11. import java.util.Iterator;
  12. public class JavaWordCount {
  13. public static void main(String[] args) {
  14. SparkConf conf = new SparkConf().setAppName("JavaWordCount");
  15. //创建sparkContext
  16. JavaSparkContext jsc = new JavaSparkContext(conf);
  17. //指定以后从哪里读取数据
  18. JavaRDD<String> lines = jsc.textFile(args[0]);
  19. //切分压平
  20. JavaRDD<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
  21. @Override
  22. public Iterator<String> call(String line) throws Exception {
  23. return Arrays.asList(line.split(" ")).iterator();
  24. }
  25. });
  26. //将单词和一组合在一起
  27. JavaPairRDD<String, Integer> wordAndOne = words.mapToPair(new PairFunction<String, String, Integer>() {
  28. @Override
  29. public Tuple2<String, Integer> call(String word) throws Exception {
  30. return new Tuple2<>(word, 1);
  31. }
  32. });
  33. //聚合
  34. JavaPairRDD<String, Integer> reduced = wordAndOne.reduceByKey(new Function2<Integer, Integer, Integer>() {
  35. @Override
  36. public Integer call(Integer v1, Integer v2) throws Exception {
  37. return v1 + v2;
  38. }
  39. });
  40. //调换顺序
  41. JavaPairRDD<Integer, String> swaped = reduced.mapToPair(new PairFunction<Tuple2<String, Integer>, Integer, String>() {
  42. @Override
  43. public Tuple2<Integer, String> call(Tuple2<String, Integer> tp) throws Exception {
  44. //return new Tuple2<>(tp._2, tp._1);
  45. return tp.swap();
  46. }
  47. });
  48. //排序
  49. JavaPairRDD<Integer, String> sorted = swaped.sortByKey(false);
  50. //调整顺序
  51. JavaPairRDD<String, Integer> result = sorted.mapToPair(new PairFunction<Tuple2<Integer, String>, String, Integer>() {
  52. @Override
  53. public Tuple2<String, Integer> call(Tuple2<Integer, String> tp) throws Exception {
  54. return tp.swap();
  55. }
  56. });
  57. //将数据保存到hdfs
  58. result.saveAsTextFile(args[1]);
  59. //释放资源
  60. jsc.stop();
  61. }
  62. }

5、打包:双击package打包:

view  --  tool windows  --   maven project,没有出现如下目录时,点击+号,去选中pom.xml文件:

6、打包成功后,选择这个:

7、进入上传到linux服务器上:

  1. 进入spark的安装目录的bin目录,执行以下代码:
  2. spark-submit --master spark://hdp-01:7077 --class cn.edu360.spark.JavaWordCount /root/learn_dh/original-SparkTest-1.0-SNAPSHOT.jar hdfs://hdp-01:9000/spark/input/test.txt hdfs://hdp-01:9000/spark/output/wc1005

命令解释:

1、--master spark://hdp-01:7077 ,指定spark集群的master

2、--class cn.edu360.spark.JavaWordCount,指定java类名全路径

3、/root/learn_dh/original-SparkTest-1.0-SNAPSHOT.jar,指定这个jar包在linux服务器上jar的绝对路径

4、hdfs://hdp-01:9000/spark/input/test.txt ,HDFS上输入文件的路径

5、hdfs://hdp-01:9000/spark/output/wc1005,HDFS上输出文件路径。(这路径不能是已经存在的,否则会报错

可以在http://hdp-01:8080/这里查看执行情况。

8、本地运行spark程序,则:

setMaster为local,

本地运行时,要配置输入输出文件的路径:

9、idea打开项目时,需要选中到src这一级目录,否则,打开后看不见项目结构图,这个坑的很啊:

例如,直接选中项目根目录打开是这样的,初学者注意下,有点莫名其妙的。

10、本地运行mapreduce和spark程序:

配置好上面的pom文件之后,不用再按照网上说的添加spark和hadoop的jar包,spark设置setMaster("local")就可以运行。

二步设置:

1、设置运行环境,edit configuration:

2、

新建maven项目,配置好pom后,不用再添加其他spark和hadoop的jar包,否则容易报莫名其妙的错误,估计是依赖冲突造成的。

在windows下配置好scala、hadoop、spark的环境变量之后:

在cmd下,输入,scala、spark-shell,可直接在本地编写scala、spark程序

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/427229
推荐阅读
相关标签
  

闽ICP备14008679号