jintaoguan commented on a change in pull request #2999: URL: https://github.com/apache/hudi/pull/2999#discussion_r647202110
########## File path: hudi-common/src/test/java/org/apache/hudi/io/storage/TestHoodieOrcReader.java ########## @@ -0,0 +1,159 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hudi.io.storage; + +import org.apache.avro.Schema; +import org.apache.avro.generic.GenericData; +import org.apache.avro.generic.GenericRecord; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.Path; +import org.apache.hudi.common.util.AvroOrcUtils; +import org.apache.orc.CompressionKind; +import org.apache.orc.OrcFile; +import org.apache.orc.TypeDescription; +import org.apache.orc.Writer; +import org.apache.orc.storage.ql.exec.vector.BytesColumnVector; +import org.apache.orc.storage.ql.exec.vector.ColumnVector; +import org.apache.orc.storage.ql.exec.vector.LongColumnVector; +import org.apache.orc.storage.ql.exec.vector.VectorizedRowBatch; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import static org.apache.hudi.common.testutils.SchemaTestUtil.getSchemaFromResource; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestHoodieOrcReader { Review comment: Updated. ########## File path: hudi-common/src/main/java/org/apache/hudi/common/util/OrcUtils.java ########## @@ -0,0 +1,283 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hudi.common.util; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.avro.Schema; +import org.apache.avro.generic.GenericRecord; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.Path; +import org.apache.hudi.avro.HoodieAvroUtils; +import org.apache.hudi.common.fs.FSUtils; +import org.apache.hudi.common.model.HoodieKey; +import org.apache.orc.storage.ql.exec.vector.BytesColumnVector; +import org.apache.orc.storage.ql.exec.vector.VectorizedRowBatch; +import org.apache.hudi.avro.HoodieAvroWriteSupport; +import org.apache.hudi.common.bloom.BloomFilter; +import org.apache.hudi.common.bloom.BloomFilterFactory; +import org.apache.hudi.common.bloom.BloomFilterTypeCode; +import org.apache.hudi.common.model.HoodieRecord; +import org.apache.hudi.exception.HoodieException; +import org.apache.hudi.exception.HoodieIOException; +import org.apache.hudi.exception.MetadataNotFoundException; +import org.apache.orc.OrcFile; +import org.apache.orc.OrcProto.UserMetadataItem; +import org.apache.orc.Reader; +import org.apache.orc.Reader.Options; +import org.apache.orc.RecordReader; +import org.apache.orc.TypeDescription; + +/** + * Utility functions for ORC files. + */ +public class OrcUtils { + + /** + * Fetch {@link HoodieKey}s from the given ORC file. + * + * @param filePath The ORC file path. + * @param configuration configuration to build fs object + * @return {@link List} of {@link HoodieKey}s fetched from the ORC file + */ + public List<HoodieKey> fetchRecordKeyPartitionPathFromOrc(Configuration configuration, Path filePath) { + List<HoodieKey> hoodieKeys = new ArrayList<>(); + try { + if (!filePath.getFileSystem(configuration).exists(filePath)) { + return new ArrayList<>(); + } + + Configuration conf = new Configuration(configuration); + conf.addResource(FSUtils.getFs(filePath.toString(), conf).getConf()); + Reader reader = OrcFile.createReader(filePath, OrcFile.readerOptions(conf)); + + Schema readSchema = HoodieAvroUtils.getRecordKeyPartitionPathSchema(); + TypeDescription orcSchema = AvroOrcUtils.createOrcSchema(readSchema); + List<String> fieldNames = orcSchema.getFieldNames(); + VectorizedRowBatch batch = orcSchema.createRowBatch(); + RecordReader recordReader = reader.rows(new Options(conf).schema(orcSchema)); + + // column indices for the RECORD_KEY_METADATA_FIELD, PARTITION_PATH_METADATA_FIELD fields + int keyCol = -1; + int partitionCol = -1; + for (int i = 0; i < fieldNames.size(); i++) { + if (fieldNames.get(i).equals(HoodieRecord.RECORD_KEY_METADATA_FIELD)) { + keyCol = i; + } + if (fieldNames.get(i).equals(HoodieRecord.PARTITION_PATH_METADATA_FIELD)) { + partitionCol = i; + } + } + if (keyCol == -1 || partitionCol == -1) { + throw new HoodieException(String.format("Couldn't find row keys or partition path in %s.", filePath)); + } + while (recordReader.nextBatch(batch)) { + BytesColumnVector rowKeys = (BytesColumnVector) batch.cols[keyCol]; + BytesColumnVector partitionPaths = (BytesColumnVector) batch.cols[partitionCol]; + for (int i = 0; i < batch.size; i++) { + String rowKey = rowKeys.toString(i); + String partitionPath = partitionPaths.toString(i); + hoodieKeys.add(new HoodieKey(rowKey, partitionPath)); + } + } + } catch (IOException e) { + throw new HoodieIOException("Failed to read from ORC file " + filePath, e); + } + return hoodieKeys; + } + + /** + * NOTE: This literally reads the entire file contents, thus should be used with caution. + */ + public List<GenericRecord> readAvroRecords(Configuration configuration, Path filePath) { + List<GenericRecord> records = new ArrayList<>(); + try { + Reader reader = OrcFile.createReader(filePath, OrcFile.readerOptions(configuration)); + TypeDescription orcSchema = reader.getSchema(); + Schema avroSchema = AvroOrcUtils.createAvroSchema(orcSchema); + RecordReader recordReader = reader.rows(new Options(configuration).schema(orcSchema)); + OrcReaderIterator<GenericRecord> iterator = new OrcReaderIterator<>(recordReader, avroSchema, orcSchema); + while (iterator.hasNext()) { + GenericRecord record = iterator.next(); + records.add(record); + } + } catch (IOException io) { + throw new HoodieIOException("Unable to read Avro records from an ORC file.", io); + } + return records; + } + + /** + * NOTE: This literally reads the entire file contents, thus should be used with caution. + */ + public List<GenericRecord> readAvroRecords(Configuration configuration, Path filePath, Schema avroSchema) { Review comment: Updated. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
