一、下载依赖jar包
具体可以参考:SparkStreaming整合kafka编程
二、创建Java工程
太简单,略。
三、实际例子
spark的安装包里面有好多例子,具体路径:spark-2.1.1-bin-hadoop2.7examples。
JavaDirectKafkaWordCount.java
- package com.spark.test;
- 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 JavaDirectKafkaWordCount {
- public static void main(String[] args) throws Exception {
- //String brokers = args[0];
- // String topics = args[1];
- // Create context with a 2 seconds batch interval
- /**
- * setMaster("local[2]"),至少要指定两个线程,一条用于用于接收消息,一条线程用于处理消息
- */
- SparkConf sparkConf = new SparkConf().setAppName("JavaDirectKafkaWordCount").setMaster("local[2]");
- JavaStreamingContext jssc = new JavaStreamingContext(sparkConf, Durations.seconds(2));
- 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
- JavaDStream<String> lines = messages.map(new Function<Tuple2<String, String>, String>() {
- @Override
- public String call(Tuple2<String, String> tuple2) {
- return tuple2._2();
- }
- });
- JavaDStream<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
- @Override
- public Iterator<String> call(String line) {
- return Arrays.asList(line.split(" ")).iterator();
- }
- });
- JavaPairDStream<String, Integer> wordCounts = words.mapToPair(
- new PairFunction<String, String, Integer>() {
- @Override
- public Tuple2<String, Integer> call(String s) {
- return new Tuple2<>(s, 1);
- }
- }).reduceByKey(
- new Function2<Integer, Integer, Integer>() {
- @Override
- public Integer call(Integer i1, Integer i2) {
- return i1 + i2;
- }
- });
- wordCounts.print();
- // Start the computation
- jssc.start();
- jssc.awaitTermination();
- }
- }
JavaKafkaWordCount.java
- package com.spark.test;
- import java.util.Arrays;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.HashMap;
- import java.util.regex.Pattern;
- import scala.Tuple2;
- import org.apache.spark.SparkConf;
- import org.apache.spark.api.java.function.FlatMapFunction;
- import org.apache.spark.api.java.function.Function;
- import org.apache.spark.api.java.function.Function2;
- import org.apache.spark.api.java.function.PairFunction;
- import org.apache.spark.streaming.Duration;
- import org.apache.spark.streaming.api.java.JavaDStream;
- import org.apache.spark.streaming.api.java.JavaPairDStream;
- import org.apache.spark.streaming.api.java.JavaPairReceiverInputDStream;
- import org.apache.spark.streaming.api.java.JavaStreamingContext;
- import org.apache.spark.streaming.kafka.KafkaUtils;
- public class JavaKafkaWordCount{
- public static void main(String[] args) throws InterruptedException {
- SparkConf sparkConf = new SparkConf().setAppName("JavaKafkaWordCount").setMaster("local[2]");
- // Create the context with 2 seconds batch size
- JavaStreamingContext jssc = new JavaStreamingContext(sparkConf, new Duration(2000));
- int numThreads = Integer.parseInt("2");
- Map<String, Integer> topicMap = new HashMap<>();
- String[] topics = "test".split(",");
- for (String topic: topics) {
- topicMap.put(topic, numThreads);
- }
- JavaPairReceiverInputDStream<String, String> messages =
- KafkaUtils.createStream(jssc, "192.168.168.200:2181", "test-group", topicMap);
- JavaDStream<String> lines = messages.map(new Function<Tuple2<String, String>, String>() {
- @Override
- public String call(Tuple2<String, String> tuple2) {
- return tuple2._2();
- }
- });
- JavaDStream<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
- @Override
- public Iterator<String> call(String line) {
- return Arrays.asList(line.split(" ")).iterator();
- }
- });
- JavaPairDStream<String, Integer> wordCounts = words.mapToPair(
- new PairFunction<String, String, Integer>() {
- @Override
- public Tuple2<String, Integer> call(String s) {
- return new Tuple2<>(s, 1);
- }
- }).reduceByKey(new Function2<Integer, Integer, Integer>() {
- @Override
- public Integer call(Integer i1, Integer i2) {
- return i1 + i2;
- }
- });
- wordCounts.print();
- jssc.start();
- jssc.awaitTermination();
- }
- }
JavaLocalWordCount.java
- package com.spark.test;
- import java.util.Arrays;
- import java.util.Iterator;
- import org.apache.spark.SparkConf;
- import org.apache.spark.api.java.JavaPairRDD;
- import org.apache.spark.api.java.JavaRDD;
- import org.apache.spark.api.java.JavaSparkContext;
- import org.apache.spark.api.java.function.FlatMapFunction;
- import org.apache.spark.api.java.function.Function2;
- import org.apache.spark.api.java.function.PairFunction;
- import org.apache.spark.api.java.function.VoidFunction;
- import scala.Tuple2;
- public class JavaLocalWordCount {
- public static void main(String[] args) {
- /**
- * 第一步,创建Spark的配置对象SparkConf,设置Spark程序的运行时的配置信息,
- * 例如说通过setMaster来设置程序要链接的Spark集群的Master的URL,
- * 如果设置为local,则代表Spark程序在本地运行,特别适合于机器配置较差的情况
- */
- SparkConf sparkConf = new SparkConf().setAppName("LocalWordCountByJava").setMaster("local");
- /**
- * 第二步,创建SparkContext对象
- * SparkContext是Spark程序所有功能的唯一入口,无论是采用Scala,java,python,R等都
- * 必须有一个SparkContext(不同语言具体类名称不同,如果是Java的话,则为JavaSparkContext)
- * 同时还会负责Spark程序在Master注册程序等
- * SparkContext是整个Spark应用程序至关重要的一个对象
- */
- JavaSparkContext jsc = new JavaSparkContext(sparkConf);//其底层实际上是Scala的SparkContext
- /**
- * 第三步,根据具体的数据来源(HDFS,HBase,Local,FS,DB,S3等),通过JavaSparkContext来创建JavaRDD
- * JavaRDD的创建方式有三种:根据外部数据来源(例如HDFS),
- * 根据Scala集合,由其他的RDD操作数据会将RDD划分成一系列Partition,
- * 分配到每个Partition的数据属于一个Task处理范畴
- */
- JavaRDD<String> lines = jsc.textFile("words.txt");
- //如果是Scala,由于SAM转化,所以可以写成val words=lines.flatMap{line =>line.split(" ")}
- JavaRDD<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
- @Override
- public Iterator<String> call(String line) {
- return Arrays.asList(line.split(" ")).iterator();
- }
- });
- /**
- * 第4步:对初始的JavaRDD进行Transformation级别的处理,例如map,filter等高阶函数等的编程,来进行具体的数据计算
- * 第4.1步:在单词拆分的基础上对每个单词实例进行计数为1,也就是word =>(word,1)
- */
- JavaPairRDD<String, Integer> pairs = words.mapToPair(new PairFunction<String, String, Integer>() {
- public Tuple2<String, Integer> call(String word) throws Exception{
- return new Tuple2<String, Integer>(word, 1);
- }
- });
- /**
- * 统计总次数
- */
- JavaPairRDD<String,Integer> wordCount=pairs.reduceByKey(new Function2<Integer,Integer,Integer>()
- {
- public Integer call(Integer v1,Integer v2)throws Exception
- {
- return v1+v2;
- }
- });
- wordCount.foreach(new VoidFunction<Tuple2<String,Integer>>(){
- public void call(Tuple2<String,Integer> pairs) throws Exception {
- System.out.println(pairs._1()+":"+pairs._2());
- }
- });
- jsc.close();
- }
- }
JavaClusterWordCount.java
- package com.spark.test;
- import java.util.Arrays;
- import java.util.Iterator;
- import org.apache.spark.SparkConf;
- import org.apache.spark.api.java.JavaPairRDD;
- import org.apache.spark.api.java.JavaRDD;
- import org.apache.spark.api.java.JavaSparkContext;
- import org.apache.spark.api.java.function.FlatMapFunction;
- import org.apache.spark.api.java.function.Function2;
- import org.apache.spark.api.java.function.PairFunction;
- import org.apache.spark.api.java.function.VoidFunction;
- import scala.Tuple2;
- public class JavaClusterWordCount {
- public static void main(String[] args) {
- /**
- * 第一步,创建Spark的配置对象SparkConf,设置Spark程序的运行时的配置信息,
- * 例如说通过setMaster来设置程序要链接的Spark集群的Master的URL,
- * 如果设置为local,则代表Spark程序在本地运行,特别适合于机器配置较差的情况
- */
- SparkConf sparkConf = new SparkConf().setAppName("LocalWordCountByJava").setMaster("local");
- /**
- * 第二步,创建SparkContext对象
- * SparkContext是Spark程序所有功能的唯一入口,无论是采用Scala,java,python,R等都
- * 必须有一个SparkContext(不同语言具体类名称不同,如果是Java的话,则为JavaSparkContext)
- * 同时还会负责Spark程序在Master注册程序等
- * SparkContext是整个Spark应用程序至关重要的一个对象
- */
- JavaSparkContext jsc = new JavaSparkContext(sparkConf);//其底层实际上是Scala的SparkContext
- /**
- * 第三步,根据具体的数据来源(HDFS,HBase,Local,FS,DB,S3等),通过JavaSparkContext来创建JavaRDD
- * JavaRDD的创建方式有三种:根据外部数据来源(例如HDFS),
- * 根据Scala集合,由其他的RDD操作数据会将RDD划分成一系列Partition,
- * 分配到每个Partition的数据属于一个Task处理范畴
- */
- JavaRDD<String> lines = jsc.textFile("hdfs://192.168.168.200:9000/input/words.txt");
- //如果是Scala,由于SAM转化,所以可以写成val words=lines.flatMap{line =>line.split(" ")}
- JavaRDD<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
- @Override
- public Iterator<String> call(String line) {
- return Arrays.asList(line.split(" ")).iterator();
- }
- });
- /**
- * 第4步:对初始的JavaRDD进行Transformation级别的处理,例如map,filter等高阶函数等的编程,来进行具体的数据计算
- * 第4.1步:在单词拆分的基础上对每个单词实例进行计数为1,也就是word =>(word,1)
- */
- JavaPairRDD<String, Integer> pairs = words.mapToPair(new PairFunction<String, String, Integer>() {
- public Tuple2<String, Integer> call(String word) throws Exception{
- return new Tuple2<String, Integer>(word, 1);
- }
- });
- /**
- * 统计总次数
- */
- JavaPairRDD<String,Integer> wordCount=pairs.reduceByKey(new Function2<Integer,Integer,Integer>()
- {
- public Integer call(Integer v1,Integer v2)throws Exception
- {
- return v1+v2;
- }
- });
- wordCount.foreach(new VoidFunction<Tuple2<String,Integer>>(){
- public void call(Tuple2<String,Integer> pairs) throws Exception {
- System.out.println(pairs._1()+":"+pairs._2());
- }
- });
- jsc.close();
- }
- }