【发布时间】:2018-07-26 05:26:51
【问题描述】:
我正在尝试从 Kafka 获取 Stream 消息并使用 spark 发送到 ElasticSearch。 Spark 以各种系统的 df 大小的形式从 kafka 获取消息,并为不同的内存使用生成消息并将其推送到 ElasticSearch。我得到的问题是索引正在创建,但消息没有出现在 Elastic 中。我是新来的。
package rnd
import com.sun.rowset.internal.Row
import org.apache.spark.sql.SQLContext
import org.apache.spark.streaming.{Minutes, Seconds, StreamingContext}
import org.apache.spark.{SparkConf, SparkContext}
import org.elasticsearch.spark.sql._
import org.elasticsearch.spark._
object WordFind {
def main(args: Array[String]) {
}
import org.apache.spark.SparkConf
val conf = new SparkConf().setMaster("local[*]").setAppName("KafkaReceiver")
val sc = new SparkContext(conf)
//val checkpointDir = "/usr/local/kafka/kafka_2.11-0.11.0.2/checkpoint/"
import org.apache.spark.streaming.StreamingContext
import org.apache.spark.streaming.Seconds
val batchIntervalSeconds = 2
//val ssc = new StreamingContext(conf, Seconds(10))
import org.apache.spark.streaming.kafka.KafkaUtils
import org.apache.spark.streaming.dstream.ReceiverInputDStream
val ssc = new StreamingContext(sc, Seconds(batchIntervalSeconds))
val kafkaStream: ReceiverInputDStream[(String, String)] = KafkaUtils.createStream(ssc, "localhost:2181",
"spark-streaming-consumer-group", Map("wordcounttopic" -> 5))
import org.apache.spark.streaming.dstream.DStream
val filteredStream: DStream[Array[String]] = kafkaStream
.filter(!_._2.contains("Filesystem")) // eliminate header
.map(_._2.split("\\s+")) // split with space
val outputDStream: DStream[String] = filteredStream.map {
row =>
val useIdx = row.length - 2
val useSystemInfo = row.length - 6
// if Use%>70 for any case> Message: Increase ROM size by 20%
// if Use%<30% for any case> Message: Decrease ROM size by 25%
val sysName = row(useSystemInfo).toString
val usePercent = row(useIdx).replace("%", "").toInt
usePercent match {
case x if x > 70 => sysName + " Increase ROM size by 20%"
case x if x < 30 => sysName + "Decrease ROM size by 25%"
case _ => "Undefined"
usePercent.toString
}
}
import org.elasticsearch.spark.sql._
// outputDStream.print()
//outputDStream.print()
val config: Map[String,String] = Map("es.index.auto.create" -> "yes")
outputDStream.foreachRDD{messageRDD =>
//messageRDD.saveToEs("dfvaluemessage_v1/km")
messageRDD.saveToEs("dfvaluemessage_v1/km", config)
}
//outputDStream.foreachRDD{messageRDD =>
//messageRDD.saveToEs("dfvaluemessage_v1/km")
//}
//outputDStream.saveToEs("kafkawordcount_v1/kwc")
// To make sure data is not deleted by the time we query it interactively
ssc.remember(Minutes(1))
//ssc.checkpoint(checkpointDir)
ssc
// }
// This starts the streaming context in the background.
ssc.start()
// This is to ensure that we wait for some time before the background streaming job starts. This will put this cell on hold for 5 times the batchIntervalSeconds.
ssc.awaitTerminationOrTimeout(batchIntervalSeconds * 5 * 1000)
}
下面的 ElasticSearch 输出:
【问题讨论】:
-
你看到印有
outputDStream.print()的消息了吗 -
是的。我修好了它。我必须在数据框中创建来存储。谢谢。
-
@s.c.如果您已经解决了问题,请在此处发布您的答案(如果您认为这对其他人有帮助)。否则你可以删除问题。
-
请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
标签: apache-spark elasticsearch apache-kafka spark-streaming