1、pom.xml文件配置,直接上代碼。
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>mvnProject</groupId> <artifactId>mvnProject</artifactId> <version>1.0-SNAPSHOT</version> <name>mvnProject</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <!-- 配置以下可以解決 在jdk1.8環境下打包時報錯 “-source 1.5 中不支持 lambda 表達式” --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!-- Spark-core --> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.11</artifactId> <version>2.3.1</version> </dependency> <!-- SparkSQL --> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2.11</artifactId> <version>2.3.1</version> </dependency> <!-- SparkSQL ON Hive--> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-hive_2.11</artifactId> <version>2.3.1</version> </dependency> <!--mysql依賴的jar包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--SparkStreaming--> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-streaming_2.11</artifactId> <version>2.3.1</version> <!--<scope>provided</scope>--> </dependency> <!-- SparkStreaming + Kafka --> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-streaming-kafka-0-10_2.11</artifactId> <version>2.3.1</version> </dependency> <!-- 向kafka 生產數據需要包 --> <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients</artifactId> <version>0.10.0.0</version> </dependency> <!--連接 Redis 需要的包--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.1</version> </dependency> <!-- Scala 包--> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.11.7</version> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-compiler</artifactId> <version>2.11.7</version> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-reflect</artifactId> <version>2.11.7</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <dependency> <groupId>com.google.collections</groupId> <artifactId>google-collections</artifactId> <version>1.0</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-mllib-local --> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-mllib-local_2.11</artifactId> <version>2.3.1</version> </dependency> </dependencies> <build> <plugins> <!-- 在maven項目中既有java又有scala代碼時配置 maven-scala-plugin 插件打包時可以將兩類代碼一起打包 --> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <version>2.15.2</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> <!-- maven 打jar包需要插件 --> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <!-- 設置false后是去掉 MySpark-1.0-SNAPSHOT-jar-with-dependencies.jar 后的 “-jar-with-dependencies” --> <!--<appendAssemblyId>false</appendAssemblyId>--> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.bjsxt.scalaspark.sql.windows.OverFunctionOnHive</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </execution> </executions> </plugin> <!-- 以上assembly可以將依賴的包打入到一個jar包中,下面這種方式是使用maven原生的方式打jar包,不將依賴的包打入到最終的jar包中 --> <!--<plugin>--> <!--<groupId>org.apache.maven.plugins</groupId>--> <!--<artifactId>maven-jar-plugin</artifactId>--> <!--<version>2.4</version>--> <!--<configuration>--> <!--<archive>--> <!--<manifest>--> <!--<addClasspath>true</addClasspath>--> <!--<!– 指定當前主類運行時找依賴的jar包時 所有依賴的jar包存放路徑的前綴 –>--> <!--<classpathPrefix>/alljars/lib</classpathPrefix>--> <!--<mainClass>com.bjsxt.javaspark.sql.CreateDataSetFromHive</mainClass>--> <!--</manifest>--> <!--</archive>--> <!--</configuration>--> <!--</plugin>--> <!-- 拷貝依賴的jar包到lib目錄 --> <!--<plugin>--> <!--<groupId>org.apache.maven.plugins</groupId>--> <!--<artifactId>maven-dependency-plugin</artifactId>--> <!--<executions>--> <!--<execution>--> <!--<id>copy</id>--> <!--<phase>package</phase>--> <!--<goals>--> <!--<goal>copy-dependencies</goal>--> <!--</goals>--> <!--<configuration>--> <!--<outputDirectory>--> <!--<!– 將依賴的jar 包復制到target/lib下–>--> <!--${project.build.directory}/lib--> <!--</outputDirectory>--> <!--</configuration>--> <!--</execution>--> <!--</executions>--> <!--</plugin>--> </plugins> </build> </project>