我一直在尝试为 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()
}
}
如何为使用此代码捕获的数据包实现火花接收器?