PageRenderTime 43ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/ql/src/java/org/apache/hadoop/hive/ql/io/RCFileRecordReader.java

#
Java | 139 lines | 88 code | 23 blank | 28 comment | 8 complexity | 2dda00a07d8ccf3d141805d764a8d4ff MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hadoop.hive.ql.io;
  19. import java.io.IOException;
  20. import org.apache.hadoop.conf.Configuration;
  21. import org.apache.hadoop.fs.FileSystem;
  22. import org.apache.hadoop.fs.Path;
  23. import org.apache.hadoop.hive.ql.io.RCFile.Reader;
  24. import org.apache.hadoop.hive.serde2.columnar.BytesRefArrayWritable;
  25. import org.apache.hadoop.io.LongWritable;
  26. import org.apache.hadoop.mapred.FileSplit;
  27. import org.apache.hadoop.mapred.RecordReader;
  28. import org.apache.hadoop.util.ReflectionUtils;
  29. /**
  30. * RCFileRecordReader.
  31. *
  32. * @param <K>
  33. * @param <V>
  34. */
  35. public class RCFileRecordReader<K extends LongWritable, V extends BytesRefArrayWritable>
  36. implements RecordReader<LongWritable, BytesRefArrayWritable> {
  37. private final Reader in;
  38. private final long start;
  39. private final long end;
  40. private boolean more = true;
  41. protected Configuration conf;
  42. public RCFileRecordReader(Configuration conf, FileSplit split)
  43. throws IOException {
  44. Path path = split.getPath();
  45. FileSystem fs = path.getFileSystem(conf);
  46. this.in = new RCFile.Reader(fs, path, conf);
  47. this.end = split.getStart() + split.getLength();
  48. this.conf = conf;
  49. if (split.getStart() > in.getPosition()) {
  50. in.sync(split.getStart()); // sync to start
  51. }
  52. this.start = in.getPosition();
  53. more = start < end;
  54. }
  55. public Class<?> getKeyClass() {
  56. return LongWritable.class;
  57. }
  58. public Class<?> getValueClass() {
  59. return BytesRefArrayWritable.class;
  60. }
  61. public LongWritable createKey() {
  62. return (LongWritable) ReflectionUtils.newInstance(getKeyClass(), conf);
  63. }
  64. public BytesRefArrayWritable createValue() {
  65. return (BytesRefArrayWritable) ReflectionUtils.newInstance(getValueClass(),
  66. conf);
  67. }
  68. @Override
  69. public boolean next(LongWritable key, BytesRefArrayWritable value)
  70. throws IOException {
  71. more = next(key);
  72. if (more) {
  73. in.getCurrentRow(value);
  74. }
  75. return more;
  76. }
  77. protected boolean next(LongWritable key) throws IOException {
  78. if (!more) {
  79. return false;
  80. }
  81. more = in.next(key);
  82. if (!more) {
  83. return false;
  84. }
  85. long lastSeenSyncPos = in.lastSeenSyncPos();
  86. if (lastSeenSyncPos >= end) {
  87. more = false;
  88. return more;
  89. }
  90. return more;
  91. }
  92. /**
  93. * Return the progress within the input split.
  94. *
  95. * @return 0.0 to 1.0 of the input byte range
  96. */
  97. public float getProgress() throws IOException {
  98. if (end == start) {
  99. return 0.0f;
  100. } else {
  101. return Math.min(1.0f, (in.getPosition() - start) / (float) (end - start));
  102. }
  103. }
  104. public long getPos() throws IOException {
  105. return in.getPosition();
  106. }
  107. protected void seek(long pos) throws IOException {
  108. in.seek(pos);
  109. }
  110. public long getStart() {
  111. return start;
  112. }
  113. public void close() throws IOException {
  114. in.close();
  115. }
  116. }