PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 129 lines | 73 code | 15 blank | 41 comment | 2 complexity | 0ee675d659d7865d73890695c52e342a 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 java.io.OutputStream;
  21. import java.util.Properties;
  22. import org.apache.hadoop.fs.FileSystem;
  23. import org.apache.hadoop.fs.Path;
  24. import org.apache.hadoop.hive.ql.exec.Utilities;
  25. import org.apache.hadoop.hive.ql.exec.FileSinkOperator.RecordWriter;
  26. import org.apache.hadoop.hive.serde.Constants;
  27. import org.apache.hadoop.io.BytesWritable;
  28. import org.apache.hadoop.io.Text;
  29. import org.apache.hadoop.io.Writable;
  30. import org.apache.hadoop.io.WritableComparable;
  31. import org.apache.hadoop.mapred.JobConf;
  32. import org.apache.hadoop.mapred.Reporter;
  33. import org.apache.hadoop.mapred.TextOutputFormat;
  34. import org.apache.hadoop.util.Progressable;
  35. /**
  36. * HiveIgnoreKeyTextOutputFormat replaces key with null before feeding the <key,
  37. * value> to TextOutputFormat.RecordWriter.
  38. *
  39. */
  40. public class HiveIgnoreKeyTextOutputFormat<K extends WritableComparable, V extends Writable>
  41. extends TextOutputFormat<K, V> implements HiveOutputFormat<K, V> {
  42. /**
  43. * create the final out file, and output row by row. After one row is
  44. * appended, a configured row separator is appended
  45. *
  46. * @param jc
  47. * the job configuration file
  48. * @param outPath
  49. * the final output file to be created
  50. * @param valueClass
  51. * the value class used for create
  52. * @param isCompressed
  53. * whether the content is compressed or not
  54. * @param tableProperties
  55. * the tableProperties of this file's corresponding table
  56. * @param progress
  57. * progress used for status report
  58. * @return the RecordWriter
  59. */
  60. @Override
  61. public RecordWriter getHiveRecordWriter(JobConf jc, Path outPath,
  62. Class<? extends Writable> valueClass, boolean isCompressed,
  63. Properties tableProperties, Progressable progress) throws IOException {
  64. int rowSeparator = 0;
  65. String rowSeparatorString = tableProperties.getProperty(
  66. Constants.LINE_DELIM, "\n");
  67. try {
  68. rowSeparator = Byte.parseByte(rowSeparatorString);
  69. } catch (NumberFormatException e) {
  70. rowSeparator = rowSeparatorString.charAt(0);
  71. }
  72. final int finalRowSeparator = rowSeparator;
  73. FileSystem fs = outPath.getFileSystem(jc);
  74. final OutputStream outStream = Utilities.createCompressedStream(jc, fs
  75. .create(outPath), isCompressed);
  76. return new RecordWriter() {
  77. public void write(Writable r) throws IOException {
  78. if (r instanceof Text) {
  79. Text tr = (Text) r;
  80. outStream.write(tr.getBytes(), 0, tr.getLength());
  81. outStream.write(finalRowSeparator);
  82. } else {
  83. // DynamicSerDe always writes out BytesWritable
  84. BytesWritable bw = (BytesWritable) r;
  85. outStream.write(bw.get(), 0, bw.getSize());
  86. outStream.write(finalRowSeparator);
  87. }
  88. }
  89. public void close(boolean abort) throws IOException {
  90. outStream.close();
  91. }
  92. };
  93. }
  94. protected static class IgnoreKeyWriter<K extends WritableComparable, V extends Writable>
  95. implements org.apache.hadoop.mapred.RecordWriter<K, V> {
  96. private final org.apache.hadoop.mapred.RecordWriter<K, V> mWriter;
  97. public IgnoreKeyWriter(org.apache.hadoop.mapred.RecordWriter<K, V> writer) {
  98. this.mWriter = writer;
  99. }
  100. public synchronized void write(K key, V value) throws IOException {
  101. this.mWriter.write(null, value);
  102. }
  103. public void close(Reporter reporter) throws IOException {
  104. this.mWriter.close(reporter);
  105. }
  106. }
  107. @Override
  108. public org.apache.hadoop.mapred.RecordWriter<K, V> getRecordWriter(
  109. FileSystem ignored, JobConf job, String name, Progressable progress)
  110. throws IOException {
  111. return new IgnoreKeyWriter<K, V>(super.getRecordWriter(ignored, job, name,
  112. progress));
  113. }
  114. }