【问题标题】:ElasticSearch index getting created but message not comingElasticSearch 索引已创建但消息未出现
【发布时间】: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 输出:

【问题讨论】:

标签: apache-spark elasticsearch apache-kafka spark-streaming


【解决方案1】:

问题是我试图使用 foreachRDD 并使用 savetoES 将消息推送到 Elasticsearch。但这只能通过创建数据框来完成。我进行了更改,效果很好。

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")
  val sqlContext = new SQLContext(sc)
  import sqlContext.implicits._
  outputDStream.foreachRDD{messageRDD =>

val df = messageRDD.toDF("messages")
    //messageRDD.saveToEs("dfvaluemessage_v1/km")
    df.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)
}

【讨论】:

  • 为了将来帮助人们,请添加一个介绍性段落来解释问题所在以及您所做的更改。也没有必要添加“希望这有帮助”——这实际上并没有改变它的帮助程度,而且可以想象,它可以毫无意义地添加到 Stack Overflow 上的每个问题中。
  • 嘿,我对堆栈很陌生。对于那个很抱歉。一定会牢记这一点。谢谢
  • 您可以edit该帖子,并添加所需的解释。
  • 是的,请编辑。我暂时将其标记为 Review Queue 的注意事项;如果它被删除,它仍然可以编辑和恢复。我相信您可以在这种情况下提出一个自定义 mod 标志并要求将其取消删除。
  • 已编辑。再次感谢您
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
  • 1970-01-01
  • 2021-05-06
  • 2019-02-04
  • 2015-05-27
  • 2017-01-16
相关资源
最近更新 更多