Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.hadoop.hive.ql.io.orc;
0019 
0020 import org.apache.hadoop.conf.Configuration;
0021 import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
0022 import org.apache.hadoop.io.NullWritable;
0023 import org.apache.hadoop.mapreduce.InputSplit;
0024 import org.apache.hadoop.mapreduce.TaskAttemptContext;
0025 
0026 import java.io.IOException;
0027 
0028 /**
0029  * This is based on hive-exec-1.2.1
0030  * {@link org.apache.hadoop.hive.ql.io.orc.OrcNewInputFormat.OrcRecordReader}.
0031  * This class exposes getObjectInspector which can be used for reducing
0032  * NameNode calls in OrcRelation.
0033  */
0034 public class SparkOrcNewRecordReader extends
0035     org.apache.hadoop.mapreduce.RecordReader<NullWritable, OrcStruct> {
0036   private final org.apache.hadoop.hive.ql.io.orc.RecordReader reader;
0037   private final int numColumns;
0038   OrcStruct value;
0039   private float progress = 0.0f;
0040   private ObjectInspector objectInspector;
0041 
0042   public SparkOrcNewRecordReader(Reader file, Configuration conf,
0043       long offset, long length) throws IOException {
0044     if (file.getTypes().isEmpty()) {
0045       numColumns = 0;
0046     } else {
0047       numColumns = file.getTypes().get(0).getSubtypesCount();
0048     }
0049     value = new OrcStruct(numColumns);
0050     this.reader = OrcInputFormat.createReaderFromFile(file, conf, offset,
0051         length);
0052     this.objectInspector = file.getObjectInspector();
0053   }
0054 
0055   @Override
0056   public void close() throws IOException {
0057     reader.close();
0058   }
0059 
0060   @Override
0061   public NullWritable getCurrentKey() throws IOException,
0062       InterruptedException {
0063     return NullWritable.get();
0064   }
0065 
0066   @Override
0067   public OrcStruct getCurrentValue() throws IOException,
0068       InterruptedException {
0069     return value;
0070   }
0071 
0072   @Override
0073   public float getProgress() throws IOException, InterruptedException {
0074     return progress;
0075   }
0076 
0077   @Override
0078   public void initialize(InputSplit split, TaskAttemptContext context)
0079       throws IOException, InterruptedException {
0080   }
0081 
0082   @Override
0083   public boolean nextKeyValue() throws IOException, InterruptedException {
0084     if (reader.hasNext()) {
0085       reader.next(value);
0086       progress = reader.getProgress();
0087       return true;
0088     } else {
0089       return false;
0090     }
0091   }
0092 
0093   public ObjectInspector getObjectInspector() {
0094     return objectInspector;
0095   }
0096 }