2013-05-15 71 views
8

我使用火花流编程,但在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兼容的文件系统,并使用它们读取一个输入流给出键值类型和输入格式。文件名称以。被忽略。 ķ 关键型读取HDFS文件 V 值类型读取HDFS文件 ˚F 输入格式读取HDFS文件 目录 HDFS目录监视新文件

我不知道怎么打发键和值的类型。 我的火花流代码:

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") 

Java代码编写Hadoop的文件:

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); 
    } 
} 

}

+0

你看到哪些问题?你有编译错误吗?如果是这样,他们是什么?当你运行你的代码时,你会得到错误/意外的行为吗?如果您提供了更多的背景知识,您发现哪些错误/意外行为更有可能得到有用的答案。 – cmbaxter

回答

5

如果你想使用fileStream,你将必须提供所有3调用它时会输入参数。在调用它之前,您需要知道您的KeyValueInputFormat类型。如果你的类型是LongWritableTextTextInputFormat,你会叫fileStream像这样:

val lines = ssc.fileStream[LongWritable, Text, TextInputFormat]("/home/sequenceFile") 

如果这些3种碰巧是你的类型,那么你可能想使用textFileStream,而不是因为它不需要任何类型params和代表fileStream使用我提到的那3种类型。使用应该是这样的:

val lines = ssc.textFileStream("/home/sequenceFile") 
+0

嘿,我正在尝试做同样的事情,但与二进制文件,我已经按照这里的指示,不幸的是它不工作。请你能提出一些建议吗? https://stackoverflow.com/questions/45778016/reading-binaryfile-with-spark-streaming – MaatDeamon

2
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'))