|
| 1 | +package com.javahelps.orc; |
| 2 | + |
| 3 | +import org.apache.hadoop.conf.Configuration; |
| 4 | +import org.apache.hadoop.fs.Path; |
| 5 | +import org.apache.hadoop.hive.ql.exec.vector.*; |
| 6 | +import org.apache.orc.OrcFile; |
| 7 | +import org.apache.orc.Reader; |
| 8 | +import org.apache.orc.RecordReader; |
| 9 | +import org.apache.orc.TypeDescription; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.*; |
| 13 | +import java.util.function.BiFunction; |
| 14 | + |
| 15 | +public class OrcFileReader { |
| 16 | + private static final int BATCH_SIZE = 2048; |
| 17 | + |
| 18 | + public static void main(String[] args) throws IOException { |
| 19 | + List<Map<String, Object>> rows = read(new Configuration(), "orders.orc"); |
| 20 | + for (Map<String, Object> row : rows) { |
| 21 | + System.out.println(row); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + public static List<Map<String, Object>> read(Configuration configuration, String path) |
| 26 | + throws IOException { |
| 27 | + // Create a list to collect rows |
| 28 | + List<Map<String, Object>> rows = new LinkedList<>(); |
| 29 | + |
| 30 | + // Create an ORC reader using the Hadoop fileSystem and path |
| 31 | + try (Reader reader = OrcFile.createReader(new Path(path), OrcFile.readerOptions(configuration))) { |
| 32 | + // Extract the schema and metadata from the reader |
| 33 | + TypeDescription schema = reader.getSchema(); |
| 34 | + List<String> fieldNames = schema.getFieldNames(); |
| 35 | + List<TypeDescription> columnTypes = schema.getChildren(); |
| 36 | + |
| 37 | + // Select only order_id and price |
| 38 | + List<Integer> selectedColumns = new ArrayList<>(); |
| 39 | + boolean[] columnsToRead = createColumnsToRead(schema, Set.of("order_id", "item_name", "price"), selectedColumns); |
| 40 | + |
| 41 | + // Get the column vector references |
| 42 | + int size = fieldNames.size(); |
| 43 | + BiFunction<ColumnVector, Integer, Object>[] mappers = new BiFunction[size]; |
| 44 | + for (int i : selectedColumns) { |
| 45 | + TypeDescription type = columnTypes.get(i); |
| 46 | + mappers[i] = createColumnReader(type); |
| 47 | + } |
| 48 | + |
| 49 | + // Pass the columnsToRead to the reader to read only the selected columns |
| 50 | + try (RecordReader records = reader.rows(reader.options().include(columnsToRead))) { |
| 51 | + // Read rows in batch for better performance. |
| 52 | + VectorizedRowBatch batch = reader.getSchema().createRowBatch(BATCH_SIZE); |
| 53 | + while (records.nextBatch(batch)) { |
| 54 | + for (int row = 0; row < batch.size; row++) { |
| 55 | + // Read rows from the batch |
| 56 | + Map<String, Object> map = new HashMap<>(selectedColumns.size()); |
| 57 | + for (int col : selectedColumns) { |
| 58 | + ColumnVector columnVector = batch.cols[col]; |
| 59 | + if (columnVector.isNull[row]) { |
| 60 | + map.put(fieldNames.get(col), null); |
| 61 | + } else { |
| 62 | + Object value = mappers[col].apply(columnVector, row); |
| 63 | + map.put(fieldNames.get(col), value); |
| 64 | + } |
| 65 | + } |
| 66 | + rows.add(map); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + return rows; |
| 72 | + } |
| 73 | + |
| 74 | + public static boolean[] createColumnsToRead(TypeDescription schema, Set<String> columns, List<Integer> indices) { |
| 75 | + // Create an array of boolean |
| 76 | + boolean[] columnsToRead = new boolean[schema.getMaximumId() + 1]; |
| 77 | + List<String> fieldNames = schema.getFieldNames(); |
| 78 | + List<TypeDescription> columnTypes = schema.getChildren(); |
| 79 | + for (int i = 0; i < fieldNames.size(); i++) { |
| 80 | + if (columns.contains(fieldNames.get(i))) { |
| 81 | + indices.add(i); |
| 82 | + TypeDescription type = columnTypes.get(i); |
| 83 | + for (int id = type.getId(); id <= type.getMaximumId(); id++) { |
| 84 | + columnsToRead[id] = true; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return columnsToRead; |
| 89 | + } |
| 90 | + |
| 91 | + public static BiFunction<ColumnVector, Integer, Object> createColumnReader(TypeDescription description) { |
| 92 | + // Reference: https://orc.apache.org/docs/core-java.html |
| 93 | + String type = description.getCategory().getName(); |
| 94 | + BiFunction<ColumnVector, Integer, Object> mapper; |
| 95 | + if ("tinyint".equals(type)) { |
| 96 | + mapper = (columnVector, row) -> (byte) ((LongColumnVector) columnVector).vector[row]; |
| 97 | + } else if ("smallint".equals(type)) { |
| 98 | + mapper = (columnVector, row) -> (short) ((LongColumnVector) columnVector).vector[row]; |
| 99 | + } else if ("int".equals(type) || "date".equals(type)) { |
| 100 | + // Date is represented as int epoch days |
| 101 | + mapper = (columnVector, row) -> (int) ((LongColumnVector) columnVector).vector[row]; |
| 102 | + } else if ("bigint".equals(type)) { |
| 103 | + mapper = (columnVector, row) -> ((LongColumnVector) columnVector).vector[row]; |
| 104 | + } else if ("boolean".equals(type)) { |
| 105 | + mapper = (columnVector, row) -> ((LongColumnVector) columnVector).vector[row] == 1; |
| 106 | + } else if ("float".equals(type)) { |
| 107 | + mapper = (columnVector, row) -> (float) ((DoubleColumnVector) columnVector).vector[row]; |
| 108 | + } else if ("double".equals(type)) { |
| 109 | + mapper = (columnVector, row) -> ((DoubleColumnVector) columnVector).vector[row]; |
| 110 | + } else if ("decimal".equals(type)) { |
| 111 | + mapper = (columnVector, row) -> ((DecimalColumnVector) columnVector).vector[row].getHiveDecimal().bigDecimalValue(); |
| 112 | + } else if ("string".equals(type) || type.startsWith("varchar")) { |
| 113 | + mapper = (columnVector, row) -> ((BytesColumnVector) columnVector).toString(row); |
| 114 | + } else if ("char".equals(type)) { |
| 115 | + mapper = (columnVector, row) -> ((BytesColumnVector) columnVector).toString(row).charAt(0); |
| 116 | + } else if ("timestamp".equals(type)) { |
| 117 | + mapper = (columnVector, row) -> ((TimestampColumnVector) columnVector).getTimestampAsLong(row); |
| 118 | + } else { |
| 119 | + throw new RuntimeException("Unsupported type " + type); |
| 120 | + } |
| 121 | + return mapper; |
| 122 | + } |
| 123 | +} |
0 commit comments