Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
RowFormat.java
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.catalog;
16 
17 import java.util.Map;
18 
19 import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
20 
22 import com.cloudera.impala.thrift.TTableRowFormat;
23 import com.google.common.base.Preconditions;
24 
30 public class RowFormat {
31  // Default row format
32  public final static RowFormat DEFAULT_ROW_FORMAT = new RowFormat(null, null, null);
33 
34  private final String fieldDelimiter_;
35  private final String lineDelimiter_;
36  private final String escapeChar_;
37 
38  private RowFormat(String fieldDelimiter, String lineDelimiter, String escapeChar,
39  boolean unescape) {
40  if (unescape) {
41  fieldDelimiter_ = getUnescapedValueOrNull(fieldDelimiter);
42  lineDelimiter_ = getUnescapedValueOrNull(lineDelimiter);
44  } else {
45  fieldDelimiter_ = fieldDelimiter;
46  lineDelimiter_ = lineDelimiter;
47  escapeChar_ = escapeChar;
48  }
49  }
50 
55  public RowFormat(String fieldDelimiter, String lineDelimiter, String escapeChar) {
56  this(fieldDelimiter, lineDelimiter, escapeChar, true);
57  }
58 
59  public String getFieldDelimiter() {
60  return fieldDelimiter_;
61  }
62 
63  public String getLineDelimiter() {
64  return lineDelimiter_;
65  }
66 
67  public String getEscapeChar() {
68  return escapeChar_;
69  }
70 
71  public boolean isDefault() {
72  return fieldDelimiter_ == null && lineDelimiter_ == null && escapeChar_ == null;
73  }
74 
75  private static String getUnescapedValueOrNull(String value) {
76  return value == null ? null : new StringLiteral(value).getUnescapedValue();
77  }
78 
79  public TTableRowFormat toThrift() {
80  TTableRowFormat tableRowFormat = new TTableRowFormat();
81  tableRowFormat.setField_terminator(getFieldDelimiter());
82  tableRowFormat.setLine_terminator(getLineDelimiter());
83  tableRowFormat.setEscaped_by(getEscapeChar());
84  return tableRowFormat;
85  }
86 
87  public static RowFormat fromThrift(TTableRowFormat tableRowFormat) {
88  if (tableRowFormat == null) {
90  }
91  // When creating a RowFormat from thrift, don't unescape the values, they should have
92  // already been unescaped.
93  return new RowFormat(tableRowFormat.getField_terminator(),
94  tableRowFormat.getLine_terminator(), tableRowFormat.getEscaped_by(), false);
95  }
96 
100  public static RowFormat fromStorageDescriptor(StorageDescriptor sd) {
101  Preconditions.checkNotNull(sd);
102  Map<String, String> params = sd.getSerdeInfo().getParameters();
103  return new RowFormat(params.get("field.delim"), params.get("line.delim"),
104  params.get("escape.delim"));
105  }
106 }
static RowFormat fromStorageDescriptor(StorageDescriptor sd)
Definition: RowFormat.java:100
static final RowFormat DEFAULT_ROW_FORMAT
Definition: RowFormat.java:32
RowFormat(String fieldDelimiter, String lineDelimiter, String escapeChar)
Definition: RowFormat.java:55
static String getUnescapedValueOrNull(String value)
Definition: RowFormat.java:75
static RowFormat fromThrift(TTableRowFormat tableRowFormat)
Definition: RowFormat.java:87
RowFormat(String fieldDelimiter, String lineDelimiter, String escapeChar, boolean unescape)
Definition: RowFormat.java:38