下面是一个简单的程序:
将记录写入orc文件
然后尝试使用 predicate 下推读取文件( searchArgument
)
问题:
这是在orc中使用 predicate 下推的正确方法吗?
这个 read(..)
方法似乎返回所有记录,完全忽略 searchArguments
. 为什么?
笔记:
我还没有找到任何有用的单元测试来演示orc(github上的orc)中 predicate 下推的工作方式。我也找不到任何关于这个特性的清晰文档。我试着查看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();
}
}
2条答案
按热度按时间noj0wjuj1#
我知道这个问题由来已久,但也许答案对某些人有用(我刚刚看到mac写了一条评论,说基本上和我几个小时前一样,但我认为一个单独的答案更明显)
orc在内部将数据分成所谓的“行组”(每个默认值有10000行),其中每个行组都有自己的索引。search参数仅用于筛选出没有行可以与search参数匹配的行组。但是,它不会过滤出单独的行。甚至可能是索引表示行组与搜索参数匹配,而其中没有一行与搜索参数匹配。这是因为行组索引主要由行组中每列的最小值和最大值组成。
因此,您必须遍历返回的行,并跳过与搜索条件不匹配的行。
lnxxn5zx2#
我也遇到了同样的问题,我认为是通过改变
.equals("x", Type.LONG,
到.equals("x",PredicateLeaf.Type.LONG
在使用这个函数时,读取器似乎只返回包含相关行的批,而不是只返回我们要求的一次。