【发布时间】:2015-10-21 16:55:26
【问题描述】:
我正在搜索一些数据文件 (~20GB)。我想在该数据中找到一些特定的术语并标记匹配的偏移量。有没有办法让 Spark 识别我正在操作的数据块的偏移量?
import org.apache.spark.api.java.*;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.Function;
import java.util.regex.*;
public class Grep {
public static void main( String args[] ) {
SparkConf conf = new SparkConf().setMaster( "spark://ourip:7077" );
JavaSparkContext jsc = new JavaSparkContext( conf );
JavaRDD<String> data = jsc.textFile( "hdfs://ourip/test/testdata.txt" ); // load the data from HDFS
JavaRDD<String> filterData = data.filter( new Function<String, Boolean>() {
// I'd like to do something here to get the offset in the original file of the string "babe ruth"
public Boolean call( String s ) { return s.toLowerCase().contains( "babe ruth" ); } // case insens matching
});
long matches = filterData.count(); // count the hits
// execute the RDD filter
System.out.println( "Lines with search terms: " + matches );
);
} // end main
} // end class Grep
我想在“filter”操作中做一些事情来计算原始文件中“babe ruth”的偏移量。我可以得到当前行中“babe ruth”的偏移量,但是告诉我该行在文件中的偏移量的过程或函数是什么?
【问题讨论】:
-
我不确定这里的偏移是什么意思。你能说得更具体点吗?
-
我正在寻找文件中的字节偏移量。例如,如果我有文本: 1 a
2 b
3 c
我想在文件中找到字符“b”的字节偏移量。 (本例为6(空格+'\n')。如果Spark不参与这个过程,这很简单,但是当Spark读取这些文件时,它们会被分块成行。所以,代码上面可能会收到“2 b”作为输入。它可以计算相对于该行的字节偏移量,但是如何获得相对于文件的字节偏移量?
标签: java apache-spark offset