本文整理匯總了Java中org.apache.avro.generic.GenericData.EnumSymbol方法的典型用法代碼示例。如果您正苦於以下問題:Java GenericData.EnumSymbol方法的具體用法?Java GenericData.EnumSymbol怎麽用?Java GenericData.EnumSymbol使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.avro.generic.GenericData
的用法示例。
在下文中一共展示了GenericData.EnumSymbol方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testCreateEnumField
import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
@Test
public void testCreateEnumField() throws Exception {
String schema = "{ \"type\": \"enum\",\n" +
" \"name\": \"Suit\",\n" +
" \"symbols\" : [\"SPADES\", \"HEARTS\", \"DIAMONDS\", \"CLUBS\"]\n" +
"}";
Schema avroSchema = new Schema.Parser().parse(schema);
Record record = RecordCreator.create();
GenericData.EnumSymbol enumSymbol = new GenericData.EnumSymbol(avroSchema, "CLUBS");
Field field = AvroTypeUtil.avroToSdcField(record, avroSchema, enumSymbol);
Assert.assertEquals(Field.Type.STRING, field.getType());
Assert.assertEquals("CLUBS", field.getValueAsString());
record.set(field);
Object avroObject = AvroTypeUtil.sdcRecordToAvro(record, avroSchema, new HashMap<String, Object>());
Assert.assertTrue(avroObject instanceof GenericData.EnumSymbol);
Assert.assertEquals("CLUBS", avroObject.toString());
}
示例2: stringColumn
import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
@Override
public GenericEnumSymbol stringColumn(String value) {
if (enumSymbols.contains(value)) {
return new GenericData.EnumSymbol(avroSchema, value);
} else {
throw new RuntimeException(String.format("%s is not in %s", value, enumSymbols.toString()));
}
}
示例3: wrapOption
import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
private Object wrapOption(Schema schema, Object option) {
if (schema.getType() == Schema.Type.BYTES && option instanceof String) {
option = ByteBuffer.wrap(((String) option).getBytes(Charset.defaultCharset()));
} else if (schema.getType() == Schema.Type.FLOAT && option instanceof Double) {
option = ((Double) option).floatValue();
} else if (schema.getType() == Schema.Type.LONG && option instanceof Integer) {
option = ((Integer) option).longValue();
} else if (schema.getType() == Schema.Type.ARRAY && option instanceof Collection) {
option = new GenericData.Array(schema, (Collection) option);
} else if (schema.getType() == Schema.Type.ENUM && option instanceof String) {
option = new GenericData.EnumSymbol(schema, (String) option);
} else if (schema.getType() == Schema.Type.FIXED && option instanceof String) {
option =
new GenericData.Fixed(schema, ((String) option).getBytes(Charset.defaultCharset()));
} else if (schema.getType() == Schema.Type.RECORD && option instanceof Map) {
Map optionMap = (Map) option;
GenericRecordBuilder optionBuilder = new GenericRecordBuilder(schema);
for (Schema.Field field : schema.getFields()) {
if (optionMap.containsKey(field.name())) {
optionBuilder.set(field, optionMap.get(field.name()));
}
}
option = optionBuilder.build();
}
return option;
}
示例4: convertAvroValToDrill
import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
private Object convertAvroValToDrill(Object value, boolean root) {
if (value instanceof ByteBuffer) {
ByteBuffer bb = ((ByteBuffer)value);
byte[] drillVal = new byte[((ByteBuffer)value).remaining()];
bb.get(drillVal);
bb.position(0);
value = drillVal;
} else if (!root && value instanceof CharSequence) {
value = new Text(value.toString());
} else if (value instanceof GenericData.Array) {
GenericData.Array array = ((GenericData.Array) value);
final JsonStringArrayList<Object> drillList = new JsonStringArrayList<>();
for (Object o : array) {
drillList.add(convertAvroValToDrill(o, false));
}
value = drillList;
} else if (value instanceof GenericData.EnumSymbol) {
value = value.toString();
} else if (value instanceof GenericData.Record) {
GenericData.Record rec = ((GenericData.Record) value);
final JsonStringHashMap<String, Object> newRecord = new JsonStringHashMap<>();
for (Schema.Field field : rec.getSchema().getFields()) {
Object val = rec.get(field.name());
newRecord.put(field.name(), convertAvroValToDrill(val, false));
}
value = newRecord;
}
return value;
}
示例5: apply
import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
public GenericEnumSymbol apply(GeneratorContext input) {
if (constants == null) {
if (schema == null) {
schema = new Schema.Parser().parse(jsonSchema);
}
constants = new GenericEnumSymbol[schema.getEnumSymbols().size()];
for (int i = 0; i < constants.length; i++) {
constants[i] = new GenericData.EnumSymbol(schema, schema.getEnumSymbols().get(i));
}
}
return constants[input.getRandom().nextInt(constants.length)];
}
示例6: generateEnumSymbol
import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
private GenericEnumSymbol generateEnumSymbol(Schema schema) {
List<String> enums = schema.getEnumSymbols();
return new
GenericData.EnumSymbol(schema, enums.get(random.nextInt(enums.size())));
}
示例7: doTestDeserializationGenericRecord
import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
/**
* Helper method to test GenericRecord serialisation.
*
* @param format
* the format to test
* @param parameters
* the configuration to use
* @throws IOException
* thrown id there is a issue
*/
@SuppressWarnings("unchecked")
private void doTestDeserializationGenericRecord(final AvroInputFormat<GenericRecord> format,
final Configuration parameters) throws IOException {
try {
format.configure(parameters);
FileInputSplit[] splits = format.createInputSplits(1);
assertEquals(splits.length, 1);
format.open(splits[0]);
GenericRecord u = format.nextRecord(null);
assertNotNull(u);
assertEquals("The schemas should be equal", userSchema, u.getSchema());
String name = u.get("name").toString();
assertNotNull("empty record", name);
assertEquals("name not equal", TEST_NAME, name);
// check arrays
List<CharSequence> sl = (List<CharSequence>) u.get("type_array_string");
assertEquals("element 0 not equal", TEST_ARRAY_STRING_1, sl.get(0).toString());
assertEquals("element 1 not equal", TEST_ARRAY_STRING_2, sl.get(1).toString());
List<Boolean> bl = (List<Boolean>) u.get("type_array_boolean");
assertEquals("element 0 not equal", TEST_ARRAY_BOOLEAN_1, bl.get(0));
assertEquals("element 1 not equal", TEST_ARRAY_BOOLEAN_2, bl.get(1));
// check enums
GenericData.EnumSymbol enumValue = (GenericData.EnumSymbol) u.get("type_enum");
assertEquals("enum not equal", TEST_ENUM_COLOR.toString(), enumValue.toString());
// check maps
Map<CharSequence, Long> lm = (Map<CharSequence, Long>) u.get("type_map");
assertEquals("map value of key 1 not equal", TEST_MAP_VALUE1, lm.get(new Utf8(TEST_MAP_KEY1)).longValue());
assertEquals("map value of key 2 not equal", TEST_MAP_VALUE2, lm.get(new Utf8(TEST_MAP_KEY2)).longValue());
assertFalse("expecting second element", format.reachedEnd());
assertNotNull("expecting second element", format.nextRecord(u));
assertNull(format.nextRecord(u));
assertTrue(format.reachedEnd());
} finally {
format.close();
}
}
示例8: doTestDeserializationGenericRecord
import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
/**
* Helper method to test GenericRecord serialisation
*
* @param format
* the format to test
* @param parameters
* the configuration to use
* @throws IOException
* thrown id there is a issue
*/
@SuppressWarnings("unchecked")
private void doTestDeserializationGenericRecord(final AvroInputFormat<GenericRecord> format,
final Configuration parameters) throws IOException {
try {
format.configure(parameters);
FileInputSplit[] splits = format.createInputSplits(1);
assertEquals(splits.length, 1);
format.open(splits[0]);
GenericRecord u = format.nextRecord(null);
assertNotNull(u);
assertEquals("The schemas should be equal", userSchema, u.getSchema());
String name = u.get("name").toString();
assertNotNull("empty record", name);
assertEquals("name not equal", TEST_NAME, name);
// check arrays
List<CharSequence> sl = (List<CharSequence>) u.get("type_array_string");
assertEquals("element 0 not equal", TEST_ARRAY_STRING_1, sl.get(0).toString());
assertEquals("element 1 not equal", TEST_ARRAY_STRING_2, sl.get(1).toString());
List<Boolean> bl = (List<Boolean>) u.get("type_array_boolean");
assertEquals("element 0 not equal", TEST_ARRAY_BOOLEAN_1, bl.get(0));
assertEquals("element 1 not equal", TEST_ARRAY_BOOLEAN_2, bl.get(1));
// check enums
GenericData.EnumSymbol enumValue = (GenericData.EnumSymbol) u.get("type_enum");
assertEquals("enum not equal", TEST_ENUM_COLOR.toString(), enumValue.toString());
// check maps
Map<CharSequence, Long> lm = (Map<CharSequence, Long>) u.get("type_map");
assertEquals("map value of key 1 not equal", TEST_MAP_VALUE1, lm.get(new Utf8(TEST_MAP_KEY1)).longValue());
assertEquals("map value of key 2 not equal", TEST_MAP_VALUE2, lm.get(new Utf8(TEST_MAP_KEY2)).longValue());
assertFalse("expecting second element", format.reachedEnd());
assertNotNull("expecting second element", format.nextRecord(u));
assertNull(format.nextRecord(u));
assertTrue(format.reachedEnd());
} finally {
format.close();
}
}
示例9: toAVRO
import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
private Object toAVRO(String csvString, Column column) {
Object returnValue = null;
switch (column.getType()) {
case ARRAY:
case SET:
Object[] list = toList(csvString);
// store as a java collection
returnValue = Arrays.asList(list);
break;
case MAP:
// store as a map
returnValue = toMap(csvString);
break;
case ENUM:
returnValue = new GenericData.EnumSymbol(createEnumSchema(column), (removeQuotes(csvString)));
break;
case TEXT:
returnValue = new Utf8(removeQuotes(csvString));
break;
case BINARY:
case UNKNOWN:
// avro accepts byte buffer for binary data
returnValue = ByteBuffer.wrap(toByteArray(csvString));
break;
case FIXED_POINT:
returnValue = toFixedPoint(csvString, column);
break;
case FLOATING_POINT:
returnValue = toFloatingPoint(csvString, column);
break;
case DECIMAL:
// TODO: store as FIXED in SQOOP-16161
returnValue = removeQuotes(csvString);
break;
case DATE:
// until 1.8 avro store as long
returnValue = ((LocalDate) toDate(csvString, column)).toDate().getTime();
break;
case TIME:
// until 1.8 avro store as long
returnValue = ((LocalTime) toTime(csvString, column)).toDateTimeToday().getMillis();
break;
case DATE_TIME:
// until 1.8 avro store as long
returnValue = toDateTimeInMillis(csvString, column);
break;
case BIT:
returnValue = Boolean.valueOf(removeQuotes(csvString));
break;
default:
throw new SqoopException(IntermediateDataFormatError.INTERMEDIATE_DATA_FORMAT_0004,
"Column type from schema was not recognized for " + column.getType());
}
return returnValue;
}
示例10: convertField
import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
@Override
Object convertField(JsonElement value) {
return new GenericData.EnumSymbol(schema, value.getAsString());
}