mirror of
https://github.com/apache/sqoop.git
synced 2025-05-05 06:12:25 +08:00
SQOOP-2508: Sqoop2: Findbugs: Fix warnings in connector-sdk module
(Colin Ma via Jarek Jarcec Cecho)
This commit is contained in:
parent
d1c47b22d8
commit
5b5042c1f2
@ -74,6 +74,7 @@ public static Schema toAvroFieldType(Column column) throws IllegalArgumentExcept
|
|||||||
switch (column.getType()) {
|
switch (column.getType()) {
|
||||||
case ARRAY:
|
case ARRAY:
|
||||||
case SET:
|
case SET:
|
||||||
|
assert column instanceof AbstractComplexListType;
|
||||||
AbstractComplexListType listColumn = (AbstractComplexListType) column;
|
AbstractComplexListType listColumn = (AbstractComplexListType) column;
|
||||||
return Schema.createArray(toAvroFieldType(listColumn.getListType()));
|
return Schema.createArray(toAvroFieldType(listColumn.getListType()));
|
||||||
case UNKNOWN:
|
case UNKNOWN:
|
||||||
@ -93,20 +94,21 @@ public static Schema toAvroFieldType(Column column) throws IllegalArgumentExcept
|
|||||||
case ENUM:
|
case ENUM:
|
||||||
return createEnumSchema(column);
|
return createEnumSchema(column);
|
||||||
case FIXED_POINT:
|
case FIXED_POINT:
|
||||||
Long byteSize = ((FixedPoint) column).getByteSize();
|
|
||||||
if (SqoopIDFUtils.isInteger(column)) {
|
if (SqoopIDFUtils.isInteger(column)) {
|
||||||
return Schema.create(Schema.Type.INT);
|
return Schema.create(Schema.Type.INT);
|
||||||
} else {
|
} else {
|
||||||
return Schema.create(Schema.Type.LONG);
|
return Schema.create(Schema.Type.LONG);
|
||||||
}
|
}
|
||||||
case FLOATING_POINT:
|
case FLOATING_POINT:
|
||||||
byteSize = ((FloatingPoint) column).getByteSize();
|
assert column instanceof FloatingPoint;
|
||||||
|
Long byteSize = ((FloatingPoint) column).getByteSize();
|
||||||
if (byteSize != null && byteSize <= (Float.SIZE/Byte.SIZE)) {
|
if (byteSize != null && byteSize <= (Float.SIZE/Byte.SIZE)) {
|
||||||
return Schema.create(Schema.Type.FLOAT);
|
return Schema.create(Schema.Type.FLOAT);
|
||||||
} else {
|
} else {
|
||||||
return Schema.create(Schema.Type.DOUBLE);
|
return Schema.create(Schema.Type.DOUBLE);
|
||||||
}
|
}
|
||||||
case MAP:
|
case MAP:
|
||||||
|
assert column instanceof org.apache.sqoop.schema.type.Map;
|
||||||
org.apache.sqoop.schema.type.Map mapColumn = (org.apache.sqoop.schema.type.Map) column;
|
org.apache.sqoop.schema.type.Map mapColumn = (org.apache.sqoop.schema.type.Map) column;
|
||||||
return Schema.createArray(toAvroFieldType(mapColumn.getValue()));
|
return Schema.createArray(toAvroFieldType(mapColumn.getValue()));
|
||||||
case TEXT:
|
case TEXT:
|
||||||
@ -117,6 +119,7 @@ public static Schema toAvroFieldType(Column column) throws IllegalArgumentExcept
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Schema createEnumSchema(Column column) {
|
public static Schema createEnumSchema(Column column) {
|
||||||
|
assert column instanceof org.apache.sqoop.schema.type.Enum;
|
||||||
Set<String> options = ((org.apache.sqoop.schema.type.Enum) column).getOptions();
|
Set<String> options = ((org.apache.sqoop.schema.type.Enum) column).getOptions();
|
||||||
List<String> listOptions = new ArrayList<String>(options);
|
List<String> listOptions = new ArrayList<String>(options);
|
||||||
return Schema.createEnum(column.getName(), null, SQOOP_SCHEMA_NAMESPACE, listOptions);
|
return Schema.createEnum(column.getName(), null, SQOOP_SCHEMA_NAMESPACE, listOptions);
|
||||||
|
@ -49,7 +49,7 @@
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.regex.Matcher;
|
import java.util.Collections;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility methods for connectors to encode data into the sqoop expected formats
|
* Utility methods for connectors to encode data into the sqoop expected formats
|
||||||
@ -68,7 +68,7 @@ public class SqoopIDFUtils {
|
|||||||
// implementation.
|
// implementation.
|
||||||
public static final String BYTE_FIELD_CHARSET = "ISO-8859-1";
|
public static final String BYTE_FIELD_CHARSET = "ISO-8859-1";
|
||||||
|
|
||||||
public static final Map<Character, String> ORIGINALS = new TreeMap<Character, String>();
|
private static final Map<Character, String> ORIGINALS = new TreeMap<Character, String>();
|
||||||
|
|
||||||
public static final char CSV_SEPARATOR_CHARACTER = ',';
|
public static final char CSV_SEPARATOR_CHARACTER = ',';
|
||||||
public static final char ESCAPE_CHARACTER = '\\';
|
public static final char ESCAPE_CHARACTER = '\\';
|
||||||
@ -77,19 +77,19 @@ public class SqoopIDFUtils {
|
|||||||
private static final Map<Character, Character> REPLACEMENTS = new TreeMap<Character, Character>();
|
private static final Map<Character, Character> REPLACEMENTS = new TreeMap<Character, Character>();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
ORIGINALS.put(new Character((char)0x00), new String(new char[] { ESCAPE_CHARACTER, '0' }));
|
ORIGINALS.put(Character.valueOf((char)0x00), new String(new char[] { ESCAPE_CHARACTER, '0' }));
|
||||||
ORIGINALS.put(new Character((char)0x0A), new String(new char[] { ESCAPE_CHARACTER, 'n' }));
|
ORIGINALS.put(Character.valueOf((char)0x0A), new String(new char[] { ESCAPE_CHARACTER, 'n' }));
|
||||||
ORIGINALS.put(new Character((char)0x0D), new String(new char[] { ESCAPE_CHARACTER, 'r' }));
|
ORIGINALS.put(Character.valueOf((char)0x0D), new String(new char[] { ESCAPE_CHARACTER, 'r' }));
|
||||||
ORIGINALS.put(new Character((char)0x1A), new String(new char[] { ESCAPE_CHARACTER, 'Z' }));
|
ORIGINALS.put(Character.valueOf((char)0x1A), new String(new char[] { ESCAPE_CHARACTER, 'Z' }));
|
||||||
ORIGINALS.put(new Character((char)0x22), new String(new char[] { ESCAPE_CHARACTER, '"' }));
|
ORIGINALS.put(Character.valueOf((char)0x22), new String(new char[] { ESCAPE_CHARACTER, '"' }));
|
||||||
ORIGINALS.put(new Character((char)0x27), new String(new char[] { ESCAPE_CHARACTER, '\'' }));
|
ORIGINALS.put(Character.valueOf((char)0x27), new String(new char[] { ESCAPE_CHARACTER, '\'' }));
|
||||||
|
|
||||||
REPLACEMENTS.put('0', new Character((char)0x00));
|
REPLACEMENTS.put('0', Character.valueOf((char)0x00));
|
||||||
REPLACEMENTS.put('n', new Character((char)0x0A));
|
REPLACEMENTS.put('n', Character.valueOf((char)0x0A));
|
||||||
REPLACEMENTS.put('r', new Character((char)0x0D));
|
REPLACEMENTS.put('r', Character.valueOf((char)0x0D));
|
||||||
REPLACEMENTS.put('Z', new Character((char)0x1A));
|
REPLACEMENTS.put('Z', Character.valueOf((char)0x1A));
|
||||||
REPLACEMENTS.put('"', new Character((char)0x22));
|
REPLACEMENTS.put('"', Character.valueOf((char)0x22));
|
||||||
REPLACEMENTS.put('\'', new Character((char)0x27));
|
REPLACEMENTS.put('\'', Character.valueOf((char)0x27));
|
||||||
}
|
}
|
||||||
|
|
||||||
// http://www.joda.org/joda-time/key_format.html provides details on the
|
// http://www.joda.org/joda-time/key_format.html provides details on the
|
||||||
@ -106,14 +106,15 @@ public class SqoopIDFUtils {
|
|||||||
public static final DateTimeFormatter tfWithFraction = DateTimeFormat.forPattern("HH:mm:ss.SSS");
|
public static final DateTimeFormatter tfWithFraction = DateTimeFormat.forPattern("HH:mm:ss.SSS");
|
||||||
public static final DateTimeFormatter tfWithNoFraction = DateTimeFormat.forPattern("HH:mm:ss");
|
public static final DateTimeFormatter tfWithNoFraction = DateTimeFormat.forPattern("HH:mm:ss");
|
||||||
|
|
||||||
public static final String[] TRUE_BIT_VALUES = new String[] { "1", "true", "TRUE" };
|
private static final String[] TRUE_BIT_VALUES = new String[] { "1", "true", "TRUE" };
|
||||||
public static final Set<String> TRUE_BIT_SET = new HashSet<String>(Arrays.asList(TRUE_BIT_VALUES));
|
public static final Set<String> TRUE_BIT_SET = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(TRUE_BIT_VALUES)));
|
||||||
public static final String[] FALSE_BIT_VALUES = new String[] { "0", "false", "FALSE" };
|
private static final String[] FALSE_BIT_VALUES = new String[] { "0", "false", "FALSE" };
|
||||||
public static final Set<String> FALSE_BIT_SET = new HashSet<String>(Arrays.asList(FALSE_BIT_VALUES));
|
public static final Set<String> FALSE_BIT_SET = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(FALSE_BIT_VALUES)));
|
||||||
|
|
||||||
// ******** Number Column Type utils***********
|
// ******** Number Column Type utils***********
|
||||||
|
|
||||||
public static boolean isInteger(Column column) {
|
public static boolean isInteger(Column column) {
|
||||||
|
assert column instanceof FixedPoint;
|
||||||
Long byteSize = ((FixedPoint) column).getByteSize();
|
Long byteSize = ((FixedPoint) column).getByteSize();
|
||||||
Boolean signed = ((FixedPoint) column).isSigned();
|
Boolean signed = ((FixedPoint) column).isSigned();
|
||||||
|
|
||||||
@ -129,15 +130,15 @@ public static boolean isInteger(Column column) {
|
|||||||
public static String toCSVFixedPoint(Object obj, Column column) {
|
public static String toCSVFixedPoint(Object obj, Column column) {
|
||||||
if (isInteger(column)) {
|
if (isInteger(column)) {
|
||||||
if (obj instanceof Number) {
|
if (obj instanceof Number) {
|
||||||
return new Integer(((Number)obj).intValue()).toString();
|
return Integer.toString(((Number) obj).intValue());
|
||||||
} else {
|
} else {
|
||||||
return new Integer(obj.toString()).toString();
|
return Integer.valueOf(obj.toString()).toString();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (obj instanceof Number) {
|
if (obj instanceof Number) {
|
||||||
return new Long(((Number)obj).longValue()).toString();
|
return Long.toString(((Number) obj).longValue());
|
||||||
} else {
|
} else {
|
||||||
return new Long(obj.toString()).toString();
|
return Long.valueOf(obj.toString()).toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -153,6 +154,7 @@ public static Object toFixedPoint(String csvString, Column column) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String toCSVFloatingPoint(Object obj, Column column) {
|
public static String toCSVFloatingPoint(Object obj, Column column) {
|
||||||
|
assert column instanceof FloatingPoint;
|
||||||
Long byteSize = ((FloatingPoint) column).getByteSize();
|
Long byteSize = ((FloatingPoint) column).getByteSize();
|
||||||
if (byteSize != null && byteSize <= (Float.SIZE / Byte.SIZE)) {
|
if (byteSize != null && byteSize <= (Float.SIZE / Byte.SIZE)) {
|
||||||
return ((Float) obj).toString();
|
return ((Float) obj).toString();
|
||||||
@ -163,6 +165,7 @@ public static String toCSVFloatingPoint(Object obj, Column column) {
|
|||||||
|
|
||||||
public static Object toFloatingPoint(String csvString, Column column) {
|
public static Object toFloatingPoint(String csvString, Column column) {
|
||||||
Object returnValue;
|
Object returnValue;
|
||||||
|
assert column instanceof FloatingPoint;
|
||||||
Long byteSize = ((FloatingPoint) column).getByteSize();
|
Long byteSize = ((FloatingPoint) column).getByteSize();
|
||||||
if (byteSize != null && byteSize <= (Float.SIZE / Byte.SIZE)) {
|
if (byteSize != null && byteSize <= (Float.SIZE / Byte.SIZE)) {
|
||||||
returnValue = Float.valueOf(csvString);
|
returnValue = Float.valueOf(csvString);
|
||||||
@ -177,6 +180,7 @@ public static String toCSVDecimal(Object obj) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Object toDecimal(String csvString, Column column) {
|
public static Object toDecimal(String csvString, Column column) {
|
||||||
|
assert column instanceof org.apache.sqoop.schema.type.Decimal;
|
||||||
Integer precision = ((org.apache.sqoop.schema.type.Decimal) column).getPrecision();
|
Integer precision = ((org.apache.sqoop.schema.type.Decimal) column).getPrecision();
|
||||||
Integer scale = ((org.apache.sqoop.schema.type.Decimal) column).getScale();
|
Integer scale = ((org.apache.sqoop.schema.type.Decimal) column).getScale();
|
||||||
BigDecimal bd = null;
|
BigDecimal bd = null;
|
||||||
@ -190,7 +194,7 @@ public static Object toDecimal(String csvString, Column column) {
|
|||||||
// we have decided to use the default MathContext DEFAULT_ROUNDINGMODE
|
// we have decided to use the default MathContext DEFAULT_ROUNDINGMODE
|
||||||
// which is RoundingMode.HALF_UP,
|
// which is RoundingMode.HALF_UP,
|
||||||
// we are aware that there may be some loss
|
// we are aware that there may be some loss
|
||||||
bd.setScale(scale, RoundingMode.HALF_UP);
|
bd = bd.setScale(scale, RoundingMode.HALF_UP);
|
||||||
}
|
}
|
||||||
return bd;
|
return bd;
|
||||||
}
|
}
|
||||||
@ -218,11 +222,14 @@ public static Object toBit(String csvString) {
|
|||||||
// *********** DATE and TIME Column Type utils **********
|
// *********** DATE and TIME Column Type utils **********
|
||||||
|
|
||||||
public static String toCSVDate(Object obj) {
|
public static String toCSVDate(Object obj) {
|
||||||
|
assert obj instanceof org.joda.time.LocalDate;
|
||||||
org.joda.time.LocalDate date = (org.joda.time.LocalDate) obj;
|
org.joda.time.LocalDate date = (org.joda.time.LocalDate) obj;
|
||||||
return encloseWithQuotes(df.print(date));
|
return encloseWithQuotes(df.print(date));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String toCSVTime(Object obj, Column col) {
|
public static String toCSVTime(Object obj, Column col) {
|
||||||
|
assert col instanceof org.apache.sqoop.schema.type.Time;
|
||||||
|
assert obj instanceof org.joda.time.LocalTime;
|
||||||
if (((org.apache.sqoop.schema.type.Time) col).hasFraction()) {
|
if (((org.apache.sqoop.schema.type.Time) col).hasFraction()) {
|
||||||
return encloseWithQuotes(tfWithFraction.print((org.joda.time.LocalTime) obj));
|
return encloseWithQuotes(tfWithFraction.print((org.joda.time.LocalTime) obj));
|
||||||
} else {
|
} else {
|
||||||
@ -241,6 +248,8 @@ public static Object toTime(String csvString, Column column) {
|
|||||||
// *********** DATE TIME Column Type utils **********
|
// *********** DATE TIME Column Type utils **********
|
||||||
|
|
||||||
public static String toCSVLocalDateTime(Object obj, Column col) {
|
public static String toCSVLocalDateTime(Object obj, Column col) {
|
||||||
|
assert obj instanceof org.joda.time.LocalDateTime;
|
||||||
|
assert col instanceof org.apache.sqoop.schema.type.DateTime;
|
||||||
org.joda.time.LocalDateTime localDateTime = (org.joda.time.LocalDateTime) obj;
|
org.joda.time.LocalDateTime localDateTime = (org.joda.time.LocalDateTime) obj;
|
||||||
org.apache.sqoop.schema.type.DateTime column = (org.apache.sqoop.schema.type.DateTime) col;
|
org.apache.sqoop.schema.type.DateTime column = (org.apache.sqoop.schema.type.DateTime) col;
|
||||||
if (column.hasFraction()) {
|
if (column.hasFraction()) {
|
||||||
@ -251,6 +260,8 @@ public static String toCSVLocalDateTime(Object obj, Column col) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String toCSVDateTime(Object obj, Column col) {
|
public static String toCSVDateTime(Object obj, Column col) {
|
||||||
|
assert obj instanceof org.joda.time.DateTime;
|
||||||
|
assert col instanceof org.apache.sqoop.schema.type.DateTime;
|
||||||
org.joda.time.DateTime dateTime = (org.joda.time.DateTime) obj;
|
org.joda.time.DateTime dateTime = (org.joda.time.DateTime) obj;
|
||||||
org.apache.sqoop.schema.type.DateTime column = (org.apache.sqoop.schema.type.DateTime) col;
|
org.apache.sqoop.schema.type.DateTime column = (org.apache.sqoop.schema.type.DateTime) col;
|
||||||
if (column.hasFraction() && column.hasTimezone()) {
|
if (column.hasFraction() && column.hasTimezone()) {
|
||||||
@ -267,7 +278,8 @@ public static String toCSVDateTime(Object obj, Column col) {
|
|||||||
public static Object toDateTime(String csvString, Column column) {
|
public static Object toDateTime(String csvString, Column column) {
|
||||||
Object returnValue;
|
Object returnValue;
|
||||||
String dateTime = removeQuotes(csvString);
|
String dateTime = removeQuotes(csvString);
|
||||||
org.apache.sqoop.schema.type.DateTime col = ((org.apache.sqoop.schema.type.DateTime) column);
|
assert column instanceof org.apache.sqoop.schema.type.DateTime;
|
||||||
|
org.apache.sqoop.schema.type.DateTime col = (org.apache.sqoop.schema.type.DateTime) column;
|
||||||
if (col.hasFraction() && col.hasTimezone()) {
|
if (col.hasFraction() && col.hasTimezone()) {
|
||||||
// After calling withOffsetParsed method, a string
|
// After calling withOffsetParsed method, a string
|
||||||
// '2004-06-09T10:20:30-08:00' will create a datetime with a zone of
|
// '2004-06-09T10:20:30-08:00' will create a datetime with a zone of
|
||||||
@ -288,7 +300,8 @@ public static Object toDateTime(String csvString, Column column) {
|
|||||||
public static Long toDateTimeInMillis(String csvString, Column column) {
|
public static Long toDateTimeInMillis(String csvString, Column column) {
|
||||||
long returnValue;
|
long returnValue;
|
||||||
String dateTime = removeQuotes(csvString);
|
String dateTime = removeQuotes(csvString);
|
||||||
org.apache.sqoop.schema.type.DateTime col = ((org.apache.sqoop.schema.type.DateTime) column);
|
assert column instanceof org.apache.sqoop.schema.type.DateTime;
|
||||||
|
org.apache.sqoop.schema.type.DateTime col = (org.apache.sqoop.schema.type.DateTime) column;
|
||||||
if (col.hasFraction() && col.hasTimezone()) {
|
if (col.hasFraction() && col.hasTimezone()) {
|
||||||
// After calling withOffsetParsed method, a string
|
// After calling withOffsetParsed method, a string
|
||||||
// '2004-06-09T10:20:30-08:00' will create a datetime with a zone of
|
// '2004-06-09T10:20:30-08:00' will create a datetime with a zone of
|
||||||
@ -370,6 +383,7 @@ else if (value instanceof JSONObject) {
|
|||||||
public static String toCSVList(Object[] list, Column column) {
|
public static String toCSVList(Object[] list, Column column) {
|
||||||
List<Object> elementList = new ArrayList<Object>();
|
List<Object> elementList = new ArrayList<Object>();
|
||||||
for (int n = 0; n < list.length; n++) {
|
for (int n = 0; n < list.length; n++) {
|
||||||
|
assert column instanceof AbstractComplexListType;
|
||||||
Column listType = ((AbstractComplexListType) column).getListType();
|
Column listType = ((AbstractComplexListType) column).getListType();
|
||||||
// 2 level nesting supported
|
// 2 level nesting supported
|
||||||
if (isColumnListType(listType)) {
|
if (isColumnListType(listType)) {
|
||||||
@ -751,7 +765,7 @@ public static Object[] fromCSV(String csvText, Schema schema) {
|
|||||||
|
|
||||||
if (csvArray.length != columns.length) {
|
if (csvArray.length != columns.length) {
|
||||||
throw new SqoopException(IntermediateDataFormatError.INTERMEDIATE_DATA_FORMAT_0001,
|
throw new SqoopException(IntermediateDataFormatError.INTERMEDIATE_DATA_FORMAT_0001,
|
||||||
"The data " + csvArray + " has the wrong number of fields.");
|
"The data " + Arrays.toString(csvArray) + " has the wrong number of fields.");
|
||||||
}
|
}
|
||||||
|
|
||||||
Object[] objectArray = new Object[csvArray.length];
|
Object[] objectArray = new Object[csvArray.length];
|
||||||
|
@ -120,6 +120,7 @@ public Object[] getObjectData() {
|
|||||||
public void write(DataOutput out) throws IOException {
|
public void write(DataOutput out) throws IOException {
|
||||||
// do we need to write the schema?
|
// do we need to write the schema?
|
||||||
DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(avroSchema);
|
DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(avroSchema);
|
||||||
|
assert out instanceof DataOutputStream;
|
||||||
BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder((DataOutputStream) out, null);
|
BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder((DataOutputStream) out, null);
|
||||||
writer.write(data, encoder);
|
writer.write(data, encoder);
|
||||||
}
|
}
|
||||||
@ -130,6 +131,7 @@ public void write(DataOutput out) throws IOException {
|
|||||||
@Override
|
@Override
|
||||||
public void read(DataInput in) throws IOException {
|
public void read(DataInput in) throws IOException {
|
||||||
DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(avroSchema);
|
DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(avroSchema);
|
||||||
|
assert in instanceof InputStream;
|
||||||
Decoder decoder = DecoderFactory.get().binaryDecoder((InputStream) in, null);
|
Decoder decoder = DecoderFactory.get().binaryDecoder((InputStream) in, null);
|
||||||
data = reader.read(null, decoder);
|
data = reader.read(null, decoder);
|
||||||
}
|
}
|
||||||
@ -238,7 +240,7 @@ private GenericRecord toAVRO(Object[] objectArray) {
|
|||||||
|
|
||||||
if (objectArray.length != columns.length) {
|
if (objectArray.length != columns.length) {
|
||||||
throw new SqoopException(IntermediateDataFormatError.INTERMEDIATE_DATA_FORMAT_0001,
|
throw new SqoopException(IntermediateDataFormatError.INTERMEDIATE_DATA_FORMAT_0001,
|
||||||
"The data " + objectArray.toString() + " has the wrong number of fields.");
|
"The data " + Arrays.toString(objectArray) + " has the wrong number of fields.");
|
||||||
}
|
}
|
||||||
// get avro schema from sqoop schema
|
// get avro schema from sqoop schema
|
||||||
GenericRecord avroObject = new GenericData.Record(avroSchema);
|
GenericRecord avroObject = new GenericData.Record(avroSchema);
|
||||||
@ -257,9 +259,6 @@ private GenericRecord toAVRO(Object[] objectArray) {
|
|||||||
case SET:
|
case SET:
|
||||||
avroObject.put(columns[i].getName(), toList((Object[]) objectArray[i]));
|
avroObject.put(columns[i].getName(), toList((Object[]) objectArray[i]));
|
||||||
break;
|
break;
|
||||||
case MAP:
|
|
||||||
avroObject.put(columns[i].getName(), objectArray[i]);
|
|
||||||
break;
|
|
||||||
case ENUM:
|
case ENUM:
|
||||||
GenericData.EnumSymbol enumValue = new GenericData.EnumSymbol(createEnumSchema(columns[i]),
|
GenericData.EnumSymbol enumValue = new GenericData.EnumSymbol(createEnumSchema(columns[i]),
|
||||||
(String) objectArray[i]);
|
(String) objectArray[i]);
|
||||||
@ -272,6 +271,7 @@ private GenericRecord toAVRO(Object[] objectArray) {
|
|||||||
case UNKNOWN:
|
case UNKNOWN:
|
||||||
avroObject.put(columns[i].getName(), ByteBuffer.wrap((byte[]) objectArray[i]));
|
avroObject.put(columns[i].getName(), ByteBuffer.wrap((byte[]) objectArray[i]));
|
||||||
break;
|
break;
|
||||||
|
case MAP:
|
||||||
case FIXED_POINT:
|
case FIXED_POINT:
|
||||||
case FLOATING_POINT:
|
case FLOATING_POINT:
|
||||||
avroObject.put(columns[i].getName(), objectArray[i]);
|
avroObject.put(columns[i].getName(), objectArray[i]);
|
||||||
@ -298,7 +298,7 @@ private GenericRecord toAVRO(Object[] objectArray) {
|
|||||||
.getTime());
|
.getTime());
|
||||||
break;
|
break;
|
||||||
case BIT:
|
case BIT:
|
||||||
avroObject.put(columns[i].getName(), Boolean.valueOf((Boolean) objectArray[i]));
|
avroObject.put(columns[i].getName(), Boolean.valueOf(objectArray[i].toString()));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new SqoopException(IntermediateDataFormatError.INTERMEDIATE_DATA_FORMAT_0001,
|
throw new SqoopException(IntermediateDataFormatError.INTERMEDIATE_DATA_FORMAT_0001,
|
||||||
@ -412,13 +412,12 @@ private Object[] toObject(GenericRecord record) {
|
|||||||
case SET:
|
case SET:
|
||||||
object[nameIndex] = toObjectArray((List<Object>) obj);
|
object[nameIndex] = toObjectArray((List<Object>) obj);
|
||||||
break;
|
break;
|
||||||
case MAP:
|
|
||||||
object[nameIndex] = obj;
|
|
||||||
break;
|
|
||||||
case ENUM:
|
case ENUM:
|
||||||
// stored as enum symbol
|
// stored as enum symbol
|
||||||
case TEXT:
|
case TEXT:
|
||||||
// stored as UTF8
|
// stored as UTF8
|
||||||
|
case DECIMAL:
|
||||||
|
// stored as string
|
||||||
object[nameIndex] = obj.toString();
|
object[nameIndex] = obj.toString();
|
||||||
break;
|
break;
|
||||||
case BINARY:
|
case BINARY:
|
||||||
@ -426,15 +425,12 @@ private Object[] toObject(GenericRecord record) {
|
|||||||
// stored as byte buffer
|
// stored as byte buffer
|
||||||
object[nameIndex] = getBytesFromByteBuffer(obj);
|
object[nameIndex] = getBytesFromByteBuffer(obj);
|
||||||
break;
|
break;
|
||||||
|
case MAP:
|
||||||
case FIXED_POINT:
|
case FIXED_POINT:
|
||||||
case FLOATING_POINT:
|
case FLOATING_POINT:
|
||||||
// stored as java objects in avro as well
|
// stored as java objects in avro as well
|
||||||
object[nameIndex] = obj;
|
object[nameIndex] = obj;
|
||||||
break;
|
break;
|
||||||
case DECIMAL:
|
|
||||||
// stored as string
|
|
||||||
object[nameIndex] = obj.toString();
|
|
||||||
break;
|
|
||||||
case DATE:
|
case DATE:
|
||||||
Long dateInMillis = (Long) obj;
|
Long dateInMillis = (Long) obj;
|
||||||
object[nameIndex] = new org.joda.time.LocalDate(dateInMillis);
|
object[nameIndex] = new org.joda.time.LocalDate(dateInMillis);
|
||||||
|
@ -37,6 +37,7 @@
|
|||||||
import java.io.DataInput;
|
import java.io.DataInput;
|
||||||
import java.io.DataOutput;
|
import java.io.DataOutput;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@ -223,7 +224,7 @@ private JSONObject toJSON(Object[] objectArray) {
|
|||||||
Column[] columns = schema.getColumnsArray();
|
Column[] columns = schema.getColumnsArray();
|
||||||
|
|
||||||
if (objectArray.length != columns.length) {
|
if (objectArray.length != columns.length) {
|
||||||
throw new SqoopException(IntermediateDataFormatError.INTERMEDIATE_DATA_FORMAT_0001, "The data " + objectArray.toString()
|
throw new SqoopException(IntermediateDataFormatError.INTERMEDIATE_DATA_FORMAT_0001, "The data " + Arrays.toString(objectArray)
|
||||||
+ " has the wrong number of fields.");
|
+ " has the wrong number of fields.");
|
||||||
}
|
}
|
||||||
JSONObject json = new JSONObject();
|
JSONObject json = new JSONObject();
|
||||||
@ -251,14 +252,12 @@ private JSONObject toJSON(Object[] objectArray) {
|
|||||||
jsonObject.putAll(map);
|
jsonObject.putAll(map);
|
||||||
json.put(columns[i].getName(), jsonObject);
|
json.put(columns[i].getName(), jsonObject);
|
||||||
break;
|
break;
|
||||||
case ENUM:
|
|
||||||
case TEXT:
|
|
||||||
json.put(columns[i].getName(), objectArray[i]);
|
|
||||||
break;
|
|
||||||
case BINARY:
|
case BINARY:
|
||||||
case UNKNOWN:
|
case UNKNOWN:
|
||||||
json.put(columns[i].getName(), Base64.encodeBase64String((byte[]) objectArray[i]));
|
json.put(columns[i].getName(), Base64.encodeBase64String((byte[]) objectArray[i]));
|
||||||
break;
|
break;
|
||||||
|
case ENUM:
|
||||||
|
case TEXT:
|
||||||
case FIXED_POINT:
|
case FIXED_POINT:
|
||||||
case FLOATING_POINT:
|
case FLOATING_POINT:
|
||||||
case DECIMAL:
|
case DECIMAL:
|
||||||
@ -363,13 +362,12 @@ private Object[] toObject(JSONObject json) {
|
|||||||
}
|
}
|
||||||
Column[] columns = schema.getColumnsArray();
|
Column[] columns = schema.getColumnsArray();
|
||||||
Object[] object = new Object[columns.length];
|
Object[] object = new Object[columns.length];
|
||||||
|
Set<Map.Entry<String,Object>> entrySet = json.entrySet();
|
||||||
Set<String> jsonKeyNames = json.keySet();
|
for (Map.Entry<String,Object> entry : entrySet) {
|
||||||
for (String name : jsonKeyNames) {
|
Integer nameIndex = schema.getColumnNameIndex(entry.getKey());
|
||||||
Integer nameIndex = schema.getColumnNameIndex(name);
|
|
||||||
Column column = columns[nameIndex];
|
Column column = columns[nameIndex];
|
||||||
|
|
||||||
Object obj = json.get(name);
|
Object obj = entry.getValue();
|
||||||
// null is a possible value
|
// null is a possible value
|
||||||
if (obj == null && !column.isNullable()) {
|
if (obj == null && !column.isNullable()) {
|
||||||
throw new SqoopException(IntermediateDataFormatError.INTERMEDIATE_DATA_FORMAT_0005,
|
throw new SqoopException(IntermediateDataFormatError.INTERMEDIATE_DATA_FORMAT_0005,
|
||||||
|
@ -328,16 +328,16 @@ public void testToDecimaPointReturnsDecimal() {
|
|||||||
Decimal col = new Decimal("dd", 4, 2);
|
Decimal col = new Decimal("dd", 4, 2);
|
||||||
assertTrue(toDecimal(text, col) instanceof BigDecimal);
|
assertTrue(toDecimal(text, col) instanceof BigDecimal);
|
||||||
BigDecimal bd = (BigDecimal) toDecimal(text, col);
|
BigDecimal bd = (BigDecimal) toDecimal(text, col);
|
||||||
assertEquals("23.44", toCSVDecimal(bd));
|
assertEquals(toCSVDecimal(bd), "23.44");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToDecimaPoint2ReturnsDecimal() {
|
public void testToDecimaPoint2ReturnsDecimal() {
|
||||||
String text = "23.44444444";
|
String text = "123456.44444444";
|
||||||
Decimal col = new Decimal("dd", 8, 2);
|
Decimal col = new Decimal("dd", 8, 2);
|
||||||
assertTrue(toDecimal(text, col) instanceof BigDecimal);
|
assertTrue(toDecimal(text, col) instanceof BigDecimal);
|
||||||
BigDecimal bd = (BigDecimal) toDecimal(text, col);
|
BigDecimal bd = (BigDecimal) toDecimal(text, col);
|
||||||
assertEquals("23.444444", toCSVDecimal(bd));
|
assertEquals(toCSVDecimal(bd), "123456.44");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -346,7 +346,7 @@ public void testToDecimaPointNoScaleNoPrecisionReturnsDecimal() {
|
|||||||
Decimal col = new Decimal("dd", null, null);
|
Decimal col = new Decimal("dd", null, null);
|
||||||
assertTrue(toDecimal(text, col) instanceof BigDecimal);
|
assertTrue(toDecimal(text, col) instanceof BigDecimal);
|
||||||
BigDecimal bd = (BigDecimal) toDecimal(text, col);
|
BigDecimal bd = (BigDecimal) toDecimal(text, col);
|
||||||
assertEquals("23.44444444", toCSVDecimal(bd));
|
assertEquals(toCSVDecimal(bd), "23.44444444");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user