这是一个简单的程序:
- 将记录写入 Orc 文件
- 然后尝试使用谓词下推 (
searchArgument
( 读取文件
问题:
- 这是在兽人中使用谓词下推的正确方法吗?
read(..)
方法似乎返回所有记录,完全忽略searchArguments
。为什么?
笔记:
我找不到任何有用的单元测试来演示谓词下推在 Orc(GitHub 上的 Orc(中的工作原理。我也找不到有关此功能的任何明确文档。尝试查看Spark和Presto代码,但我找不到任何有用的东西。
下面的代码是 https://github.com/melanio/codecheese-blog-examples/tree/master/orc-examples/src/main/java/codecheese/blog/examples/orc 的修改版本
public class TestRoundTrip {
public static void main(String[] args) throws IOException {
final String file = "tmp/test-round-trip.orc";
new File(file).delete();
final long highestX = 10000L;
final Configuration conf = new Configuration();
write(file, highestX, conf);
read(file, highestX, conf);
}
private static void read(String file, long highestX, Configuration conf) throws IOException {
Reader reader = OrcFile.createReader(
new Path(file),
OrcFile.readerOptions(conf)
);
//Retrieve x that is "highestX - 1000". So, only 1 value should've been retrieved.
Options readerOptions = new Options(conf)
.searchArgument(
SearchArgumentFactory
.newBuilder()
.equals("x", Type.LONG, highestX - 1000)
.build(),
new String[]{"x"}
);
RecordReader rows = reader.rows(readerOptions);
VectorizedRowBatch batch = reader.getSchema().createRowBatch();
while (rows.nextBatch(batch)) {
LongColumnVector x = (LongColumnVector) batch.cols[0];
LongColumnVector y = (LongColumnVector) batch.cols[1];
for (int r = 0; r < batch.size; r++) {
long xValue = x.vector[r];
long yValue = y.vector[r];
System.out.println(xValue + ", " + yValue);
}
}
rows.close();
}
private static void write(String file, long highestX, Configuration conf) throws IOException {
TypeDescription schema = TypeDescription.fromString("struct<x:int,y:int>");
Writer writer = OrcFile.createWriter(
new Path(file),
OrcFile.writerOptions(conf).setSchema(schema)
);
VectorizedRowBatch batch = schema.createRowBatch();
LongColumnVector x = (LongColumnVector) batch.cols[0];
LongColumnVector y = (LongColumnVector) batch.cols[1];
for (int r = 0; r < highestX; ++r) {
int row = batch.size++;
x.vector[row] = r;
y.vector[row] = r * 3;
// If the batch is full, write it out and start over.
if (batch.size == batch.getMaxSize()) {
writer.addRowBatch(batch);
batch.reset();
}
}
if (batch.size != 0) {
writer.addRowBatch(batch);
batch.reset();
}
writer.close();
}
}
这个问题很老,但也许答案对某人有用。(而且我刚刚看到Mac几个小时前写了一条评论说和我基本一样,但我认为单独的答案更明显(
Orc 在内部将数据分隔到所谓的"行组"(每个行组默认有 10000 行(,其中每个行组都有自己的索引。搜索参数仅用于筛选出其中没有行可以与搜索参数匹配的行组。但是,它不会筛选出单个行。甚至可能是索引声明行组与搜索参数匹配,而其中没有一行实际上与搜索匹配。这是因为行组索引主要由行组中每列的最小值和最大值组成。
因此,您必须遍历返回的行并跳过与您的搜索条件不匹配的行。
我遇到了同样的问题,我认为它已通过更改来纠正
.equals("x", Type.LONG,
自
.equals("x",PredicateLeaf.Type.LONG
在使用它时,读取器似乎只返回具有相关行的批次,而不仅仅是我们要求的一次。