版本号:

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.8

基础环境:

Maven3.5.0安装与配置+Eclipse应用

Maven下载项目依赖jar包和使用方法

maven中把依赖的JAR包一起打包

MAVEN Scope使用

一、指定JDK为1.8

在pom.xml配置文件中添加以下参数即可:


  1. <properties>
  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3. <encoding>UTF-8</encoding>
  4. <java.version>1.8</java.version>
  5. <maven.compiler.source>1.8</maven.compiler.source>
  6. <maven.compiler.target>1.8</maven.compiler.target>
  7. </properties>

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-compiler-plugin</artifactId>
  4. <configuration>
  5. <source>1.8</source>
  6. <target>1.8</target>
  7. </configuration>
  8. </plugin>

配置之后的pom.xml文件如下:


  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>Test</groupId>
  5. <artifactId>test</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>test</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <encoding>UTF-8</encoding>
  13. <!-- 配置JDK为1.8 -->
  14. <java.version>1.8</java.version>
  15. <maven.compiler.source>1.8</maven.compiler.source>
  16. <maven.compiler.target>1.8</maven.compiler.target>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>junit</groupId>
  21. <artifactId>junit</artifactId>
  22. <version>3.8.1</version>
  23. <scope>test</scope>
  24. </dependency>
  25. </dependencies>
  26. <build>
  27. <plugins>
  28. <!-- 配置JDK为1.8 -->
  29. <plugin>
  30. <groupId>org.apache.maven.plugins</groupId>
  31. <artifactId>maven-compiler-plugin</artifactId>
  32. <configuration>
  33. <source>1.8</source>
  34. <target>1.8</target>
  35. </configuration>
  36. </plugin>
  37. <!-- 配置打包依赖包maven-assembly-plugin -->
  38. <plugin>
  39. <artifactId> maven-assembly-plugin </artifactId>
  40. <configuration>
  41. <descriptorRefs>
  42. <descriptorRef>jar-with-dependencies</descriptorRef>
  43. </descriptorRefs>
  44. <archive>
  45. <manifest>
  46. <mainClass></mainClass>
  47. </manifest>
  48. </archive>
  49. </configuration>
  50. <executions>
  51. <execution>
  52. <id>make-assembly</id>
  53. <phase>package</phase>
  54. <goals>
  55. <goal>assembly</goal>
  56. </goals>
  57. </execution>
  58. </executions>
  59. </plugin>
  60. </plugins>
  61. </build>
  62. </project>

二、配置Spark依赖包

查看spark-2.1.1-bin-hadoop2.7/jars目录下的jar包版本

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

1、配置spark-core_2.11-2.1.1.jar

往pom.xml文件中添加以下配置:


  1. <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core_2.11 -->
  2. <dependency>
  3. <groupId>org.apache.spark</groupId>
  4. <artifactId>spark-core_2.11</artifactId>
  5. <version>2.1.1</version>
  6. <scope>runtime</scope>
  7. </dependency>

为了后面打包时把依赖包也一起打包,需要把<scope>provided</scope>配置成<scope>runtime</scope>。

2、配置spark-streaming_2.11-2.1.1.jar

往pom.xml文件中添加以下配置:


  1. <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-streaming_2.11 -->
  2. <dependency>
  3. <groupId>org.apache.spark</groupId>
  4. <artifactId>spark-streaming_2.11</artifactId>
  5. <version>2.1.1</version>
  6. <scope>runtime</scope>
  7. </dependency>

为了后面打包时把依赖包也一起打包,需要把<scope>provided</scope>配置成<scope>runtime</scope>。

三、配置Spark+Kafka

1、配置spark-streaming-kafka-0-8_2.11-2.1.1.jar

往pom.xml文件中添加以下配置:


  1. <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-streaming-kafka-0-8_2.11 -->
  2. <dependency>
  3. <groupId>org.apache.spark</groupId>
  4. <artifactId>spark-streaming-kafka-0-8_2.11</artifactId>
  5. <version>2.1.1</version>
  6. </dependency>

四、pom.xml完整配置内容


  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>Test</groupId>
  5. <artifactId>test</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>test</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <encoding>UTF-8</encoding>
  13. <!-- 配置JDK为1.8 -->
  14. <java.version>1.8</java.version>
  15. <maven.compiler.source>1.8</maven.compiler.source>
  16. <maven.compiler.target>1.8</maven.compiler.target>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>junit</groupId>
  21. <artifactId>junit</artifactId>
  22. <version>3.8.1</version>
  23. <scope>test</scope>
  24. </dependency>
  25. <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core_2.11 -->
  26. <dependency>
  27. <groupId>org.apache.spark</groupId>
  28. <artifactId>spark-core_2.11</artifactId>
  29. <version>2.1.1</version>
  30. <scope>runtime</scope>
  31. </dependency>
  32. <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-streaming_2.11 -->
  33. <dependency>
  34. <groupId>org.apache.spark</groupId>
  35. <artifactId>spark-streaming_2.11</artifactId>
  36. <version>2.1.1</version>
  37. <scope>runtime</scope>
  38. </dependency>
  39. <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-streaming-kafka-0-8_2.11 -->
  40. <dependency>
  41. <groupId>org.apache.spark</groupId>
  42. <artifactId>spark-streaming-kafka-0-8_2.11</artifactId>
  43. <version>2.1.1</version>
  44. </dependency>
  45. </dependencies>
  46. <build>
  47. <plugins>
  48. <!-- 配置JDK为1.8 -->
  49. <plugin>
  50. <groupId>org.apache.maven.plugins</groupId>
  51. <artifactId>maven-compiler-plugin</artifactId>
  52. <configuration>
  53. <source>1.8</source>
  54. <target>1.8</target>
  55. </configuration>
  56. </plugin>
  57. <!-- 配置打包依赖包maven-assembly-plugin -->
  58. <plugin>
  59. <artifactId> maven-assembly-plugin </artifactId>
  60. <configuration>
  61. <descriptorRefs>
  62. <descriptorRef>jar-with-dependencies</descriptorRef>
  63. </descriptorRefs>
  64. <archive>
  65. <manifest>
  66. <mainClass></mainClass>
  67. </manifest>
  68. </archive>
  69. </configuration>
  70. <executions>
  71. <execution>
  72. <id>make-assembly</id>
  73. <phase>package</phase>
  74. <goals>
  75. <goal>assembly</goal>
  76. </goals>
  77. </execution>
  78. </executions>
  79. </plugin>
  80. </plugins>
  81. </build>
  82. </project>

五、本地开发spark代码上传spark集群服务并运行

JavaDirectKafkaCompare.java


  1. package com.spark.main;
  2. import java.util.HashMap;
  3. import java.util.HashSet;
  4. import java.util.Arrays;
  5. import java.util.Iterator;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import java.util.regex.Pattern;
  9. import scala.Tuple2;
  10. import kafka.serializer.StringDecoder;
  11. import org.apache.spark.SparkConf;
  12. import org.apache.spark.api.java.function.*;
  13. import org.apache.spark.streaming.api.java.*;
  14. import org.apache.spark.streaming.kafka.KafkaUtils;
  15. import org.apache.spark.streaming.Durations;
  16. public class JavaDirectKafkaCompare {
  17. public static void main(String[] args) throws Exception {
  18. /**
  19. * setMaster("local[2]"),至少要指定两个线程,一条用于用于接收消息,一条线程用于处理消息
  20. *  Durations.seconds(2)每两秒读取一次kafka
  21. */
  22. SparkConf sparkConf = new SparkConf().setAppName("JavaDirectKafkaWordCount").setMaster("local[2]");
  23. JavaStreamingContext jssc = new JavaStreamingContext(sparkConf, Durations.seconds(2));
  24. /**
  25. * checkpoint("hdfs://192.168.168.200:9000/checkpoint")防止数据丢包
  26. */
  27. jssc.checkpoint("hdfs://192.168.168.200:9000/checkpoint");
  28. /**
  29. * 配置连接kafka的相关参数
  30. */
  31. Set<String> topicsSet = new HashSet<>(Arrays.asList("test"));
  32. Map<String, String> kafkaParams = new HashMap<>();
  33. kafkaParams.put("metadata.broker.list", "192.168.168.200:9092");
  34. // Create direct kafka stream with brokers and topics
  35. JavaPairInputDStream<String, String> messages = KafkaUtils.createDirectStream(
  36. jssc,
  37. String.class,
  38. String.class,
  39. StringDecoder.class,
  40. StringDecoder.class,
  41. kafkaParams,
  42. topicsSet
  43. );
  44. // Get the lines, split them into words, count the words and print
  45. /**
  46. * _2()获取第二个对象的值
  47. */
  48. JavaDStream<String> lines = messages.map(new Function<Tuple2<String, String>, String>() {
  49. @Override
  50. public String call(Tuple2<String, String> tuple2) {
  51. return tuple2._2();
  52. }
  53. });
  54. String sfzh = "432922196105276721";
  55. JavaDStream<String> wordCounts = lines.filter(new Function<String, Boolean>(){
  56. @Override
  57. public Boolean call(String s) throws Exception {
  58. // TODO Auto-generated method stub
  59. /**
  60. * 通过身份证号筛选出相关数据
  61. */
  62. if(s.contains(sfzh)){
  63. System.out.println("比对出来的结果:" + s);
  64. return true;
  65. }
  66. return false;
  67. }
  68. });
  69. wordCounts.print();
  70. // Start the computation
  71. jssc.start();
  72. jssc.awaitTermination();
  73. }
  74. }

右键Run As ------>Maven install,运行成功之后,会在target目录生成一个test-0.0.1-SNAPSHOT-jar-with-dependencies.jar,把该jar包复制到LInux集群环境下的SPARK_HOME/myApp目录下:

执行命令:


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

六、附上离线Maven仓库

下载地址:  链接:http://pan.baidu.com/s/1eS7Ywme 密码:y3qz

Maven+Eclipse+SparkStreaming+Kafka整合的更多相关文章

  1. SparkStreaming+Kafka整合

    SparkStreaming+Kafka整合 1.需求 使用SparkStreaming,并且结合Kafka,获取实时道路交通拥堵情况信息. 2.目的 对监控点平均车速进行监控,可以实时获取交通拥堵情 ...

  2. 【SparkStreaming学习之三】 SparkStreaming和kafka整合

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 scala-2.10.4(依赖jdk1.8) spark ...

  3. 【转】Spark Streaming和Kafka整合开发指南

    基于Receivers的方法 这个方法使用了Receivers来接收数据.Receivers的实现使用到Kafka高层次的消费者API.对于所有的Receivers,接收到的数据将会保存在Spark ...

  4. Spring Kafka整合Spring Boot创建生产者客户端案例

    每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 创建一个kafka-producer-master的maven工程.整个项目结构如下: ...

  5. spark第十篇:Spark与Kafka整合

    spark与kafka整合需要引入spark-streaming-kafka.jar,该jar根据kafka版本有2个分支,分别是spark-streaming-kafka-0-8和spark-str ...

  6. Flink+Kafka整合的实例

    Flink+Kafka整合实例 1.使用工具Intellig IDEA新建一个maven项目,为项目命名为kafka01. 2.我的pom.xml文件配置如下. <?xml version=&q ...

  7. Spark Streaming和Kafka整合开发指南(二)

    在本博客的<Spark Streaming和Kafka整合开发指南(一)>文章中介绍了如何使用基于Receiver的方法使用Spark Streaming从Kafka中接收数据.本文将介绍 ...

  8. Spark Streaming和Kafka整合开发指南(一)

    Apache Kafka是一个分布式的消息发布-订阅系统.可以说,任何实时大数据处理工具缺少与Kafka整合都是不完整的.本文将介绍如何使用Spark Streaming从Kafka中接收数据,这里将 ...

  9. [Maven]Eclipse插件之Maven配置及问题解析.

    前言:今天在自己环境装了Maven环境, 并且安装了Eclipse插件, 在查找插件过程中确实遇到一些问题, 好不容易找到一个  却又有问题.装好了插件之后, 用Eclipse创建Maven项目却出现 ...

随机推荐

  1. Getting started with 3G | ip.access nano3G+OpenBSC+Osmocom-bb Part 1

    English Version could be find at Osmocom.org https://osmocom.org/projects/cellular-infrastructure/wi ...

  2. Day13作业及默写

    1. 整理今天的博客,写课上代码,整理流程图. 博客链接--博客园 2. 写一个函数完成三次登陆功能: 用户的用户名密码从一个文件register中取出. register文件包含多个用户名,密码,用 ...

  3. ngxinx 配置

    vim 复制操作 1.复制 1)单行复制 在命令模式下,将光标移动到将要复制的行处,按“yy”进行复制: 2)多行复制 在命令模式下,将光标移动到将要复制的首行处,按“nyy”复制n行: 其中n为1. ...

  4. 【webdriver自动化】使用数据驱动的方式实现登录多个163账号

    练习1:使用数据驱动的方式,登录多个邮箱账号 login_info.txt: youxiang_99@163.com,XXXX youxiang_100@163.com,XXXX main.py: f ...

  5. XML Schema——笔记整理

    什么是 XML Schema? 定义可出现在文档中的元素 定义可出现在文档中的属性 定义哪个元素是子元素 定义子元素的次序 定义子元素的数目 定义元素是否为空,或者是否可包含文本 定义元素和属性的数据 ...

  6. easyUI 下拉组件转义

    <labelclass="label"for="belongWidget">归属组件:</label> <inputid=&quo ...

  7. Kesci: Keras 实现 LSTM——时间序列预测

    博主之前参与的一个科研项目是用 LSTM 结合 Attention 机制依据作物生长期内气象环境因素预测作物产量.本篇博客将介绍如何用 keras 深度学习的框架搭建 LSTM 模型对时间序列做预测. ...

  8. Java IntelliJ IDEA 不能显示项目里的文件结构

    方法一: 关闭IDEA, 然后删除项目文件夹下的.idea文件夹 重新用IDEA工具打开项目 方法二: 菜单:File -> Invalidate Caches / Restart

  9. python函数完整语法和分类

    函数初级 简介 # 函数是一系列代码的集合,用来完成某项特定的功能 优点 '''1. 避免代码的冗余2. 让程序代码结构更加清晰3. 让代码具有复用性,便于维护''' 函数四部分 '''1. 函数名: ...

  10. Linux使用sshfs挂载远程目录到本地

    1安装sshfs [root@iZwz9hy7gff0kpg1swp1d3Z ~]# yum install sshfs 2创建本地目录 [root@iZwz9hy7gff0kpg1swp1d3Z ~ ...