Idea配置spark环境及编写scala代码

由于之前在往上没有找到比较好的关于IDEA配置spark环境,现在分享一下

1、打开IDEA

create New Project

2、创建项目,如下图所示

3、配置maven文件,即使你本地没有spark、scala、hadoop环境也没有关系,maven会自动下载及加载到maven里面,只需要你指定相应版本就好,指定好maven之后,自动下载过程需要等待一段时间,如下图所示:

4、New scala class编写代码测试,如下图所示:

注:附maven文件,你可以和我的maven文件一样,下次再创建项目工程的时候,还可以配置相同版本的环境maven就不会重新下载啦,它会自动加载已经下载好的环境!

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <groupId>com_bupt_ai</groupId>  
  4.   <artifactId>bupt_at</artifactId>  
  5.   <version>1.0-SNAPSHOT</version>  
  6.   <inceptionYear>2008</inceptionYear>  
  7.   
  8.   <!-- 声明子项目公用的配置属性 -->  
  9.   <properties>  
  10.     <scala.version>2.11.8</scala.version>  
  11.     <hadoop.version>2.6.0</hadoop.version>  
  12.     <spark.version>2.0.1</spark.version>  
  13.     <hbase.version>1.2.0</hbase.version>  
  14.     <kafka.version>0.8.2.2</kafka.version>  
  15.     <jedis.version>2.9.0</jedis.version>  
  16.     <mysql.version>5.1.6</mysql.version>  
  17.     <log4j.version>1.2.17</log4j.version>  
  18.     <slf4j.version>1.7.22</slf4j.version>  
  19.     <pool.version>2.4.2</pool.version>  
  20.     <jackson.version>2.8.6</jackson.version>  
  21.     <net.sf.json.version>2.4</net.sf.json.version>  
  22.     <joda.version>2.9.9</joda.version>  
  23.     <postgresql.version>42.1.4</postgresql.version>  
  24.     <javax.mail>1.6.2</javax.mail>  
  25.     <qconf.version>1.2.2</qconf.version>  
  26.     <xgboost4j.version>0.72</xgboost4j.version>  
  27.     <elasticsearch.version>6.0.1</elasticsearch.version>  
  28.     <druid.version>1.1.10</druid.version>  
  29.   </properties>  
  30.   
  31.   <!-- 声明并引入子项目共有的依赖 -->  
  32.   <dependencies>  
  33.     <!-- 引入Spark相关的Jar包 -->  
  34.     <dependency>  
  35.       <groupId>org.apache.spark</groupId>  
  36.       <artifactId>spark-core_2.11</artifactId>  
  37.       <version>${spark.version}</version>  
  38.       <!--<scope>provided</scope>-->  
  39.     </dependency>  
  40.     <dependency>  
  41.       <groupId>org.apache.spark</groupId>  
  42.       <artifactId>spark-sql_2.11</artifactId>  
  43.       <version>${spark.version}</version>  
  44.       <!--<scope>provided</scope>-->  
  45.     </dependency>  
  46.     <dependency>  
  47.       <groupId>org.apache.spark</groupId>  
  48.       <artifactId>spark-streaming_2.11</artifactId>  
  49.       <version>${spark.version}</version>  
  50.       <!--<scope>provided</scope>-->  
  51.     </dependency>  
  52.     <dependency>  
  53.       <groupId>org.scala-lang</groupId>  
  54.       <artifactId>scala-library</artifactId>  
  55.       <version>${scala.version}</version>  
  56.       <!--<scope>provided</scope>-->  
  57.     </dependency>  
  58.     <dependency>  
  59.       <groupId>org.apache.spark</groupId>  
  60.       <artifactId>spark-hive_2.11</artifactId>  
  61.       <version>${spark.version}</version>  
  62.       <!--<scope>provided</scope>-->  
  63.     </dependency>  
  64.     <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->  
  65.     <dependency>  
  66.       <groupId>org.apache.hbase</groupId>  
  67.       <artifactId>hbase-client</artifactId>  
  68.       <version>${hbase.version}</version>  
  69.       <!--<scope>provided</scope>-->  
  70.     </dependency>  
  71.     <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server -->  
  72.     <dependency>  
  73.       <groupId>org.apache.hbase</groupId>  
  74.       <artifactId>hbase-server</artifactId>  
  75.       <version>${hbase.version}</version>  
  76.       <!--<scope>provided</scope>-->  
  77.     </dependency>  
  78.     <!-- 用来连接Kafka的工具类 -->  
  79.     <dependency>  
  80.       <groupId>org.apache.kafka</groupId>  
  81.       <artifactId>kafka-clients</artifactId>  
  82.       <version>${kafka.version}</version>  
  83.       <!--<scope>provided</scope>-->  
  84.     </dependency>  
  85.     <dependency>  
  86.       <groupId>org.apache.spark</groupId>  
  87.       <artifactId>spark-streaming-kafka-0-8_2.11</artifactId>  
  88.       <version>${spark.version}</version>  
  89.       <!--<scope>provided</scope>-->  
  90.     </dependency>  
  91.     <dependency>  
  92.       <groupId>redis.clients</groupId>  
  93.       <artifactId>jedis</artifactId>  
  94.       <version>${jedis.version}</version>  
  95.     </dependency>  
  96.     <dependency>  
  97.       <groupId>mysql</groupId>  
  98.       <artifactId>mysql-connector-java</artifactId>  
  99.       <version>${mysql.version}</version>  
  100.     </dependency>  
  101.     <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->  
  102.     <dependency>  
  103.       <groupId>org.postgresql</groupId>  
  104.       <artifactId>postgresql</artifactId>  
  105.       <version>${postgresql.version}</version>  
  106.     </dependency>  
  107.     <dependency>  
  108.       <groupId>com.sun.mail</groupId>  
  109.       <artifactId>javax.mail</artifactId>  
  110.       <version>${javax.mail}</version>  
  111.     </dependency>  
  112.   
  113.     <!-- https://mvnrepository.com/artifact/ml.dmlc/xgboost4j -->  
  114.     <dependency>  
  115.       <groupId>ml.dmlc</groupId>  
  116.       <artifactId>xgboost4j-spark</artifactId>  
  117.       <version>${xgboost4j.version}</version>  
  118.     </dependency>  
  119.   
  120.     <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-graphx -->  
  121.     <dependency>  
  122.       <groupId>org.apache.spark</groupId>  
  123.       <artifactId>spark-graphx_2.11</artifactId>  
  124.       <version>${spark.version}</version>  
  125.     </dependency>  
  126.   
  127.     <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-mllib -->  
  128.     <dependency>  
  129.       <groupId>org.apache.spark</groupId>  
  130.       <artifactId>spark-mllib_2.11</artifactId>  
  131.       <version>${spark.version}</version>  
  132.     </dependency>  
  133.   
  134.     <!-- https://mvnrepository.com/artifact/org.scalanlp/breeze-viz -->  
  135.     <dependency>  
  136.       <groupId>org.scalanlp</groupId>  
  137.       <artifactId>breeze-viz_2.10</artifactId>  
  138.       <version>0.12</version>  
  139.     </dependency>  
  140.   
  141.     <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch-spark-20 -->  
  142.     <dependency>  
  143.       <groupId>org.elasticsearch</groupId>  
  144.       <artifactId>elasticsearch-spark-20_2.11</artifactId>  
  145.       <version>${elasticsearch.version}</version>  
  146.     </dependency>  
  147.   
  148.     <!-- https://mvnrepository.com/artifact/jfree/jfreechart -->  
  149.     <dependency>  
  150.       <groupId>jfree</groupId>  
  151.       <artifactId>jfreechart</artifactId>  
  152.       <version>1.0.13</version>  
  153.     </dependency>  
  154.   
  155.     <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->  
  156.     <dependency>  
  157.       <groupId>com.alibaba</groupId>  
  158.       <artifactId>druid</artifactId>  
  159.       <version>${druid.version}</version>  
  160.     </dependency>  
  161.   
  162.     <dependency>  
  163.       <groupId>com.zaxxer</groupId>  
  164.       <artifactId>HikariCP</artifactId>  
  165.       <version>2.7.8</version>  
  166.     </dependency>  
  167.   
  168.   </dependencies>  
  169.   
  170.   <build>  
  171.     <plugins>  
  172.       <plugin>  
  173.         <groupId>net.alchim31.maven</groupId>  
  174.         <artifactId>scala-maven-plugin</artifactId>  
  175.         <version>3.2.2</version>  
  176.         <executions>  
  177.           <execution>  
  178.             <goals>  
  179.               <goal>compile</goal>  
  180.               <goal>testCompile</goal>  
  181.             </goals>  
  182.           </execution>  
  183.         </executions>  
  184.       </plugin>  
  185.     </plugins>  
  186.   
  187.     <pluginManagement>  
  188.       <plugins>  
  189.         <plugin>  
  190.           <groupId>org.apache.maven.plugins</groupId>  
  191.           <artifactId>maven-assembly-plugin</artifactId>  
  192.           <version>3.0.0</version>  
  193.           <executions>  
  194.             <execution>  
  195.               <id>make-assembly</id>  
  196.               <phase>package</phase>  
  197.               <goals>  
  198.                 <goal>single</goal>  
  199.               </goals>  
  200.             </execution>  
  201.           </executions>  
  202.         </plugin>  
  203.       </plugins>  
  204.     </pluginManagement>  
  205.   </build>  
  206.   
  207. </project>  

猜你喜欢

转载自blog.csdn.net/weixin_41362649/article/details/87966027