Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
HdfsCompression.java
Go to the documentation of this file.
1 // Copyright (c) 2012 Cloudera, Inc. All rights reserved.
2 package com.cloudera.impala.catalog;
3 
4 import com.cloudera.impala.thrift.THdfsCompression;
5 import com.google.common.base.Preconditions;
6 import com.google.common.collect.ImmutableMap;
7 
15 // TODO: Add LZ4?
16 public enum HdfsCompression {
22  LZO,
23  LZO_INDEX; //Lzo index file.
24 
25  /* Map from a suffix to a compression type */
26  private static final ImmutableMap<String, HdfsCompression> SUFFIX_MAP =
27  ImmutableMap.<String, HdfsCompression>builder().
28  put("deflate", DEFLATE).
29  put("gz", GZIP).
30  put("bz2", BZIP2).
31  put("snappy", SNAPPY).
32  put("lzo", LZO).
33  put("index", LZO_INDEX).
34  build();
35 
36  /* Given a file name return its compression type, if any. */
37  public static HdfsCompression fromFileName(String fileName) {
38  int index = fileName.lastIndexOf(".");
39  if (index == -1) {
40  return NONE;
41  }
42 
43  String suffix = fileName.substring(index + 1);
44  HdfsCompression compression = SUFFIX_MAP.get(suffix.toLowerCase());
45  return compression == null ? NONE : compression;
46  }
47 
48  public THdfsCompression toThrift() {
49  switch (this) {
50  case NONE: return THdfsCompression.NONE;
51  case DEFLATE: return THdfsCompression.DEFLATE;
52  case GZIP: return THdfsCompression.GZIP;
53  case BZIP2: return THdfsCompression.BZIP2;
54  case SNAPPY: return THdfsCompression.SNAPPY_BLOCKED;
55  case LZO: return THdfsCompression.LZO;
56  default: throw new IllegalStateException("Unexpected codec: " + this);
57  }
58  }
59 
60  /* Returns a compression type based on (Hive's) intput format. Special case for LZO. */
61  public static HdfsCompression fromHdfsInputFormatClass(String inputFormatClass) {
62  // TODO: Remove when we have the native LZO writer.
63  Preconditions.checkNotNull(inputFormatClass);
64  if (inputFormatClass.equals(HdfsFileFormat.LZO_TEXT_INPUT_FORMAT)) {
65  return LZO;
66  }
67  return NONE;
68  }
69 }
static HdfsCompression fromFileName(String fileName)
static HdfsCompression fromHdfsInputFormatClass(String inputFormatClass)