[英]spark streaming fileStream
我正在使用火花流編程但是在scala上遇到了一些問題。 我正在嘗試使用StreamingContext.fileStream函數
這個函數的定義是這樣的:
def fileStream[K, V, F <: InputFormat[K, V]](directory: String)(implicit arg0: ClassManifest[K], arg1: ClassManifest[V], arg2: ClassManifest[F]): DStream[(K, V)]
創建一個輸入流,監視與Hadoop兼容的文件系統以獲取新文件,並使用給定的鍵值類型和輸入格式讀取它們。 文件名以。開頭。 被忽略了。 K用於讀取HDFS文件的密鑰類型V用於讀取HDFS文件的值類型F用於讀取HDFS文件目錄的輸入格式用於監視新文件的HDFS目錄
我不知道如何傳遞Key和Value的類型。 火花流中的我的代碼:
val ssc = new StreamingContext(args(0), "StreamingReceiver", Seconds(1),
System.getenv("SPARK_HOME"), Seq("/home/mesos/StreamingReceiver.jar"))
// Create a NetworkInputDStream on target ip:port and count the
val lines = ssc.fileStream("/home/sequenceFile")
用於編寫hadoop文件的Java代碼:
public class MyDriver {
private static final String[] DATA = { "One, two, buckle my shoe",
"Three, four, shut the door", "Five, six, pick up sticks",
"Seven, eight, lay them straight", "Nine, ten, a big fat hen" };
public static void main(String[] args) throws IOException {
String uri = args[0];
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(uri), conf);
Path path = new Path(uri);
IntWritable key = new IntWritable();
Text value = new Text();
SequenceFile.Writer writer = null;
try {
writer = SequenceFile.createWriter(fs, conf, path, key.getClass(),
value.getClass());
for (int i = 0; i < 100; i++) {
key.set(100 - i);
value.set(DATA[i % DATA.length]);
System.out.printf("[%s]\t%s\t%s\n", writer.getLength(), key,
value);
writer.append(key, value);
}
} finally {
IOUtils.closeStream(writer);
}
}
}
如果你想使用fileStream
,你將不得不在調用它時為它提供所有3種類型的參數。 在調用它之前,您需要知道Key
, Value
和InputFormat
類型是什么。 如果您的類型是LongWritable
, Text
和TextInputFormat
,您可以像這樣調用fileStream
:
val lines = ssc.fileStream[LongWritable, Text, TextInputFormat]("/home/sequenceFile")
如果這3種類型恰好是您的類型,那么您可能希望使用textFileStream
因為它不需要任何類型的params並使用我提到的3種類型委托給fileStream
。 使用它看起來像這樣:
val lines = ssc.textFileStream("/home/sequenceFile")
val filterF = new Function[Path, Boolean] {
def apply(x: Path): Boolean = {
val flag = if(x.toString.split("/").last.split("_").last.toLong < System.currentTimeMillis) true else false
return flag
}
}
val streamed_rdd = ssc.fileStream[LongWritable, Text, TextInputFormat]("/user/hdpprod/temp/spark_streaming_input",filterF,false).map(_._2.toString).map(u => u.split('\t'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.