版本号:
maven3.5.0 scala IDE for Eclipse:版本(4.6.1) spark-2.1.1-bin-hadoop2.7 kafka_2.11-0.8.2.1 JDK1.8java
基础环境:
1、指定JDK为1.8
在pom.xml配置文件中添加如下参数便可:apache
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <encoding>UTF-8</encoding>
- <java.version>1.8</java.version>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
- </properties>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.8</source>
- <target>1.8</target>
- </configuration>
- </plugin>
配置以后的pom.xml文件以下:api
- <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>Test</groupId>
- <artifactId>test</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
-
- <name>test</name>
- <url>http://maven.apache.org</url>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <encoding>UTF-8</encoding>
- <!-- 配置JDK为1.8 -->
- <java.version>1.8</java.version>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <!-- 配置JDK为1.8 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.8</source>
- <target>1.8</target>
- </configuration>
- </plugin>
-
- <!-- 配置打包依赖包maven-assembly-plugin -->
- <plugin>
- <artifactId> maven-assembly-plugin </artifactId>
- <configuration>
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
- <archive>
- <manifest>
- <mainClass></mainClass>
- </manifest>
- </archive>
- </configuration>
- <executions>
- <execution>
- <id>make-assembly</id>
- <phase>package</phase>
- <goals>
- <goal>assembly</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </project>
2、配置Spark依赖包
查看spark-2.1.1-bin-hadoop2.7/jars目录下的jar包版本maven

到maven远程仓库http://mvnrepository.com中搜索对应jar包便可。ide

一、配置spark-core_2.11-2.1.1.jar
往pom.xml文件中添加如下配置:oop
- <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core_2.11 -->
- <dependency>
- <groupId>org.apache.spark</groupId>
- <artifactId>spark-core_2.11</artifactId>
- <version>2.1.1</version>
- <scope>runtime</scope>
- </dependency>
为了后面打包时把依赖包也一块儿打包,须要把<scope>provided</scope>配置成<scope>runtime</scope>。ui
二、配置spark-streaming_2.11-2.1.1.jar
往pom.xml文件中添加如下配置:url
- <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-streaming_2.11 -->
- <dependency>
- <groupId>org.apache.spark</groupId>
- <artifactId>spark-streaming_2.11</artifactId>
- <version>2.1.1</version>
- <scope>runtime</scope>
- </dependency>
为了后面打包时把依赖包也一块儿打包,须要把<scope>provided</scope>配置成<scope>runtime</scope>。spa
3、配置Spark+Kafka
一、配置spark-streaming-kafka-0-8_2.11-2.1.1.jar
往pom.xml文件中添加如下配置:.net
- <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-streaming-kafka-0-8_2.11 -->
- <dependency>
- <groupId>org.apache.spark</groupId>
- <artifactId>spark-streaming-kafka-0-8_2.11</artifactId>
- <version>2.1.1</version>
- </dependency>
4、pom.xml完整配置内容
- <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>Test</groupId>
- <artifactId>test</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
-
- <name>test</name>
- <url>http://maven.apache.org</url>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <encoding>UTF-8</encoding>
- <!-- 配置JDK为1.8 -->
- <java.version>1.8</java.version>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core_2.11 -->
- <dependency>
- <groupId>org.apache.spark</groupId>
- <artifactId>spark-core_2.11</artifactId>
- <version>2.1.1</version>
- <scope>runtime</scope>
- </dependency>
- <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-streaming_2.11 -->
- <dependency>
- <groupId>org.apache.spark</groupId>
- <artifactId>spark-streaming_2.11</artifactId>
- <version>2.1.1</version>
- <scope>runtime</scope>
- </dependency>
- <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-streaming-kafka-0-8_2.11 -->
- <dependency>
- <groupId>org.apache.spark</groupId>
- <artifactId>spark-streaming-kafka-0-8_2.11</artifactId>
- <version>2.1.1</version>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <!-- 配置JDK为1.8 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.8</source>
- <target>1.8</target>
- </configuration>
- </plugin>
-
- <!-- 配置打包依赖包maven-assembly-plugin -->
- <plugin>
- <artifactId> maven-assembly-plugin </artifactId>
- <configuration>
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
- <archive>
- <manifest>
- <mainClass></mainClass>
- </manifest>
- </archive>
- </configuration>
- <executions>
- <execution>
- <id>make-assembly</id>
- <phase>package</phase>
- <goals>
- <goal>assembly</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </project>
5、本地开发spark代码上传spark集群服务并运行
JavaDirectKafkaCompare.java
- package com.spark.main;
-
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Arrays;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- import java.util.regex.Pattern;
-
- import scala.Tuple2;
- import kafka.serializer.StringDecoder;
-
- import org.apache.spark.SparkConf;
- import org.apache.spark.api.java.function.*;
- import org.apache.spark.streaming.api.java.*;
- import org.apache.spark.streaming.kafka.KafkaUtils;
- import org.apache.spark.streaming.Durations;
-
- public class JavaDirectKafkaCompare {
-
- public static void main(String[] args) throws Exception {
- /**
- * setMaster("local[2]"),至少要指定两个线程,一条用于用于接收消息,一条线程用于处理消息
- * Durations.seconds(2)每两秒读取一次kafka
- */
- SparkConf sparkConf = new SparkConf().setAppName("JavaDirectKafkaWordCount").setMaster("local[2]");
- JavaStreamingContext jssc = new JavaStreamingContext(sparkConf, Durations.seconds(2));
- /**
- * checkpoint("hdfs://192.168.168.200:9000/checkpoint")防止数据丢包
- */
- jssc.checkpoint("hdfs://192.168.168.200:9000/checkpoint");
- /**
- * 配置链接kafka的相关参数
- */
- Set<String> topicsSet = new HashSet<>(Arrays.asList("test"));
- Map<String, String> kafkaParams = new HashMap<>();
- kafkaParams.put("metadata.broker.list", "192.168.168.200:9092");
-
- // Create direct kafka stream with brokers and topics
- JavaPairInputDStream<String, String> messages = KafkaUtils.createDirectStream(
- jssc,
- String.class,
- String.class,
- StringDecoder.class,
- StringDecoder.class,
- kafkaParams,
- topicsSet
- );
-
- // Get the lines, split them into words, count the words and print
- /**
- * _2()获取第二个对象的值
- */
- JavaDStream<String> lines = messages.map(new Function<Tuple2<String, String>, String>() {
- @Override
- public String call(Tuple2<String, String> tuple2) {
- return tuple2._2();
- }
- });
-
- String sfzh = "432922196105276721";
- JavaDStream<String> wordCounts = lines.filter(new Function<String, Boolean>(){
- @Override
- public Boolean call(String s) throws Exception {
- // TODO Auto-generated method stub
- /**
- * 经过身份证号筛选出相关数据
- */
- if(s.contains(sfzh)){
- System.out.println("比对出来的结果:" + s);
- return true;
- }
- return false;
- }
- });
- wordCounts.print();
- // Start the computation
- jssc.start();
- jssc.awaitTermination();
- }
-
- }
右键Run As ------>Maven install,运行成功以后,会在target目录生成一个test-0.0.1-SNAPSHOT-jar-with-dependencies.jar,把该jar包复制到LInux集群环境下的SPARK_HOME/myApp目录下:

执行命令:
- cd /usr/local/spark/spark-2.1.1-bin-hadoop2.7;
- bin/spark-submit --class "com.spark.main.JavaDirectKafkaCompare" --master local[4] myApp/test-0.0.1-SNAPSHOT-jar-with-dependencies.jar;

6、附上离线Maven仓库
下载地址: 连接:http://pan.baidu.com/s/1eS7Ywme 密码:y3qz