第一步:打开虚拟机 如果没安装的可以安装或更新一下nc
yum install -y nc
第二步:启动nc 端口号
nc -lk 9999
第三步:idea 创建对象等一系列操作
package com.zhp.day12
import org.apache.kafka.common.serialization.StringDeserializer
import org.apache.spark.SparkConf
import org.apache.spark.streaming.dstream.DStream
import org.apache.spark.streaming.kafka010.ConsumerStrategies.Subscribe
import org.apache.spark.streaming.kafka010.LocationStrategies.PreferConsistent
import org.apache.spark.streaming.kafka010._
import org.apache.spark.streaming.{Seconds, StreamingContext}
object Test02 extends App {
//设置配置文件local 本地运行模式 和设置线程数
private val conf: SparkConf = new SparkConf().setAppName("testx").setMaster("local[2]")
//创建对象 Seconds 设置 接受消息间隔时间
private val ssc = new StreamingContext(conf, Seconds(5))
//设置接受主机 和端口
val lines = ssc.socketTextStream("zhp1", 9999)
lines.print()
ssc.start() // Start the computation
ssc.awaitTermination() // Wait for the computation to terminate
}
版权说明
文章采用: 《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权。版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!