1

我一直在尝试为 SPARK 0.9 实现接收器。我已经使用 Jnetpcap 库捕获了数据包,需要将其传递给 Scala 中的 spark。在“def receive()”方法中写入数据包的捕获部分是否足够?

编辑:这是来自此链接的代码,它使用 Jnetpcap 库捕获数据包:

import java.util.Date
import java.lang.StringBuilder
import org.jnetpcap.Pcap
import org.jnetpcap.packet.PcapPacket
import org.jnetpcap.packet.PcapPacketHandler

object PacketCapture1 {
  def main(args: Array[String]){
    val snaplen = 64 * 1024 // Capture all packets, no trucation
    val flags = Pcap.MODE_PROMISCUOUS // capture all packets
    val timeout = 10 * 1000
    //val errbuf = new StringBuilder()

    val jsb = new java.lang.StringBuilder()
    val errbuf = new StringBuilder(jsb);
    val pcap = Pcap.openLive("eth0", snaplen, flags, timeout, errbuf)
    if (pcap == null) {
      println("Error : " + errbuf.toString())
    }
    println(pcap)
    val jpacketHandler = new PcapPacketHandler[String]() {

      def nextPacket(packet: PcapPacket, user: String) {
        println("Received packet at %s caplen=%4d len=%4d %s\n", new Date(packet.getCaptureHeader.timestampInMillis()),
          packet.getCaptureHeader.caplen(), packet.getCaptureHeader.wirelen(), user)
      }
    }
    pcap.loop(30, jpacketHandler, "jNetPcap works!")
    pcap.close()

  }
}

如何为使用此代码捕获的数据包实现火花接收器?

4

1 回答 1

5

您必须创建一个自定义 NetworkReceiver(或 Spark 1.0+ 中的 Receiver)并实现该onStart()方法。对于 Spark 0.9,请参阅http://spark.apache.org/docs/0.9.1/streaming-custom-receivers.html

对于 spark 1.0(强烈推荐),请参阅http://spark.apache.org/docs/latest/streaming-custom-receivers.html

于 2014-07-10T19:05:42.543 回答