當前位置: 首頁>>代碼示例>>Java>>正文


Java GenericData.EnumSymbol方法代碼示例

本文整理匯總了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());
}
 
開發者ID:streamsets,項目名稱:datacollector,代碼行數:20,代碼來源:TestAvroTypeUtil.java

示例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()));
    }
}
 
開發者ID:joker1007,項目名稱:embulk-formatter-avro,代碼行數:9,代碼來源:AvroEnumConverter.java

示例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;
}
 
開發者ID:C0urante,項目名稱:avro-random-generator,代碼行數:28,代碼來源:Generator.java

示例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;
}
 
開發者ID:axbaretto,項目名稱:drill,代碼行數:30,代碼來源:AvroTestUtil.java

示例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)];
}
 
開發者ID:Talend,項目名稱:components,代碼行數:13,代碼來源:GeneratorFunctions.java

示例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())));
}
 
開發者ID:C0urante,項目名稱:avro-random-generator,代碼行數:6,代碼來源:Generator.java

示例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();
	}
}
 
開發者ID:axbaretto,項目名稱:flink,代碼行數:55,代碼來源:AvroRecordInputFormatTest.java

示例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();
	}
}
 
開發者ID:axbaretto,項目名稱:flink,代碼行數:55,代碼來源:AvroRecordInputFormatTest.java

示例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;
}
 
開發者ID:vybs,項目名稱:sqoop-on-spark,代碼行數:57,代碼來源:AVROIntermediateDataFormat.java

示例10: convertField

import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
@Override
Object convertField(JsonElement value) {
  return new GenericData.EnumSymbol(schema, value.getAsString());
}
 
開發者ID:Hanmourang,項目名稱:Gobblin,代碼行數:5,代碼來源:JsonElementConversionFactory.java


注:本文中的org.apache.avro.generic.GenericData.EnumSymbol方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。