ConfigurationDataTypes { public static DataType getSparkDataType(String typeString) { DataType type; String prec_scale_regex_groups = "\\s*(decimal)\\s*\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)\\s*"; Pattern prec_scale_regex_pattern = Pattern.compile(prec_scale_regex_groups); Matcher prec_scale_regex_matcher = prec_scale_regex_pattern.matcher(typeString); if (prec_scale_regex_matcher.matches()) { int precision = Integer.parseInt(prec_scale_regex_matcher.group(2)); int scale = Integer.parseInt(prec_scale_regex_matcher.group(3)); type = DataTypes.createDecimalType(precision, scale); } else { switch (typeString) { case DECIMAL: type = DataTypes.createDecimalType(); break; case STRING: type = DataTypes.StringType; break; case FLOAT: type = DataTypes.FloatType; break; case DOUBLE: type = DataTypes.DoubleType; break; case BYTE: type = DataTypes.ByteType; break; case SHORT: type = DataTypes.ShortType; break; case INT: type = DataTypes.IntegerType; break; case LONG: type = DataTypes.LongType; break; case BOOLEAN: type = DataTypes.BooleanType; break; case BINARY: type = DataTypes.BinaryType; break; case DATE: type = DataTypes.DateType; break; case TIMESTAMP: type = DataTypes.TimestampType; break; default: throw new RuntimeException("Unsupported or unrecognized field type: " + typeString); } } return type; }  static String getConfigurationDataType(DataType type); static DataType getSparkDataType(String typeString); static final String DECIMAL; static final String STRING; static final String FLOAT; static final String DOUBLE; static final String BYTE; static final String SHORT; static final String INT; static final String LONG; static final String BOOLEAN; static final String BINARY; static final String DATE; static final String TIMESTAMP; }
@Test public void testGetSparkDataTypeValid() { assertEquals(ConfigurationDataTypes.getSparkDataType(ConfigurationDataTypes.DECIMAL), new DecimalType()); assertEquals(ConfigurationDataTypes.getSparkDataType("decimal(38,38)"), new DecimalType(38,38)); assertEquals(ConfigurationDataTypes.getSparkDataType("decimal ( 38 , 38 ) "), new DecimalType(38,38)); assertEquals(ConfigurationDataTypes.getSparkDataType(ConfigurationDataTypes.STRING), DataTypes.StringType); assertEquals(ConfigurationDataTypes.getSparkDataType(ConfigurationDataTypes.FLOAT), DataTypes.FloatType); assertEquals(ConfigurationDataTypes.getSparkDataType(ConfigurationDataTypes.DOUBLE), DataTypes.DoubleType); assertEquals(ConfigurationDataTypes.getSparkDataType(ConfigurationDataTypes.BYTE), DataTypes.ByteType); assertEquals(ConfigurationDataTypes.getSparkDataType(ConfigurationDataTypes.SHORT), DataTypes.ShortType); assertEquals(ConfigurationDataTypes.getSparkDataType(ConfigurationDataTypes.INT), DataTypes.IntegerType); assertEquals(ConfigurationDataTypes.getSparkDataType(ConfigurationDataTypes.LONG), DataTypes.LongType); assertEquals(ConfigurationDataTypes.getSparkDataType(ConfigurationDataTypes.BOOLEAN), DataTypes.BooleanType); assertEquals(ConfigurationDataTypes.getSparkDataType(ConfigurationDataTypes.BINARY), DataTypes.BinaryType); assertEquals(ConfigurationDataTypes.getSparkDataType(ConfigurationDataTypes.DATE), DataTypes.DateType); assertEquals(ConfigurationDataTypes.getSparkDataType(ConfigurationDataTypes.TIMESTAMP), DataTypes.TimestampType); }
@Test (expected = RuntimeException.class) public void testGetSparkDataTypeInvalid() { ConfigurationDataTypes.getSparkDataType("array"); }