Skip to content
This repository was archived by the owner on Jul 9, 2021. It is now read-only.

Commit 7752a95

Browse files
committed
SQOOP-324: Allow use of Avro Data Files with Hive import
1 parent 20fe120 commit 7752a95

File tree

5 files changed

+635
-24
lines changed

5 files changed

+635
-24
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
javaSourceCompatibilityVersion=1.8
2121

22-
avroVersion=1.8.1
22+
avroVersion=1.8.2
2323
parquetVersion=1.9.0
2424
hadoopVersion=2.8.0
2525
aspectjVersion=1.7.4

src/java/org/apache/sqoop/hive/TableDefWriter.java

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.File;
2222
import java.io.IOException;
2323
import java.util.ArrayList;
24+
import java.util.Arrays;
2425
import java.util.Collections;
2526
import java.util.HashMap;
2627
import java.util.List;
@@ -167,27 +168,36 @@ public String getCreateTableStmt() throws IOException {
167168
}
168169

169170
boolean first = true;
170-
String partitionKey = options.getHivePartitionKey();
171-
for (String col : colNames) {
172-
if (col.equals(partitionKey)) {
173-
throw new IllegalArgumentException("Partition key " + col + " cannot "
174-
+ "be a column to import.");
175-
}
171+
final String partitionKey = options.getHivePartitionKey();
172+
if (Arrays.asList(colNames).contains(partitionKey)) {
173+
throw new IllegalArgumentException(
174+
"Partition key " + partitionKey + " cannot be a column to import.");
175+
}
176+
for (final String col : colNames) {
176177

177178
if (!first) {
178179
sb.append(", ");
179180
}
180181

181182
first = false;
182183

183-
String hiveColType;
184-
if (options.getFileLayout() == SqoopOptions.FileLayout.TextFile) {
184+
final String hiveColType;
185+
switch (options.getFileLayout()) {
186+
case TextFile:
187+
case AvroDataFile:
185188
Integer colType = columnTypes.get(col);
186189
hiveColType = getHiveColumnTypeForTextTable(userMapping, col, colType);
187-
} else if (options.getFileLayout() == SqoopOptions.FileLayout.ParquetFile) {
188-
hiveColType = HiveTypes.toHiveType(columnNameToAvroFieldSchema.get(col), options);
189-
} else {
190-
throw new RuntimeException("File format is not supported for Hive tables.");
190+
break;
191+
case ParquetFile:
192+
hiveColType =
193+
HiveTypes.toHiveType(columnNameToAvroFieldSchema.get(col), options);
194+
break;
195+
case BinaryFile:
196+
case SequenceFile:
197+
default:
198+
throw new RuntimeException(
199+
"File format is not supported for Hive tables: "
200+
+ options.getFileLayout());
191201
}
192202

193203
sb.append('`').append(col).append("` ").append(hiveColType);
@@ -208,9 +218,14 @@ public String getCreateTableStmt() throws IOException {
208218
.append(" STRING) ");
209219
}
210220

211-
if (SqoopOptions.FileLayout.ParquetFile.equals(options.getFileLayout())) {
221+
switch(options.getFileLayout()) {
222+
case ParquetFile:
212223
sb.append("STORED AS PARQUET");
213-
} else {
224+
break;
225+
case AvroDataFile:
226+
sb.append("STORED AS AVRO");
227+
break;
228+
case TextFile: {
214229
sb.append("ROW FORMAT DELIMITED FIELDS TERMINATED BY '");
215230
sb.append(getHiveOctalCharCode((int) options.getOutputFieldDelim()));
216231
sb.append("' LINES TERMINATED BY '");
@@ -226,14 +241,21 @@ public String getCreateTableStmt() throws IOException {
226241
sb.append("' STORED AS TEXTFILE");
227242
}
228243
}
244+
break;
245+
default:
246+
throw new RuntimeException(
247+
"File format is not supported for Hive tables: "
248+
+ options.getFileLayout());
249+
}
229250

230251
if (isHiveExternalTableSet) {
231252
// add location
232253
sb.append(" LOCATION '"+options.getHiveExternalTableDir()+"'");
233254
}
234255

235-
LOG.debug("Create statement: " + sb.toString());
236-
return sb.toString();
256+
final String tableCreateStatement = sb.toString();
257+
LOG.debug("Create statement: " + tableCreateStatement);
258+
return tableCreateStatement;
237259
}
238260

239261
private Map<String, Schema> getColumnNameToAvroTypeMapping() {

src/java/org/apache/sqoop/tool/BaseSqoopTool.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,12 +1580,6 @@ protected void validateHiveOptions(SqoopOptions options)
15801580
+ " option." + HELP_STR);
15811581
}
15821582

1583-
if (options.doHiveImport()
1584-
&& options.getFileLayout() == SqoopOptions.FileLayout.AvroDataFile) {
1585-
throw new InvalidOptionsException("Hive import is not compatible with "
1586-
+ "importing into AVRO format.");
1587-
}
1588-
15891583
if (options.doHiveImport()
15901584
&& options.getFileLayout() == SqoopOptions.FileLayout.SequenceFile) {
15911585
throw new InvalidOptionsException("Hive import is not compatible with "

0 commit comments

Comments
 (0)