本文整理匯總了Java中org.apache.avro.Schema.Type類的典型用法代碼示例。如果您正苦於以下問題:Java Type類的具體用法?Java Type怎麽用?Java Type使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Type類屬於org.apache.avro.Schema包,在下文中一共展示了Type類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: recursiveRecord
import org.apache.avro.Schema.Type; //導入依賴的package包/類
@Test
public void recursiveRecord() throws Exception {
Schema schema = SchemaBuilder
.record("X")
.fields()
.name("a")
.type(SchemaBuilder
.record("R")
.fields()
.name("b")
.type(Schema.create(Type.BYTES))
.noDefault()
.name("r")
.type("R")
.noDefault()
.endRecord())
.noDefault()
.endRecord();
assertThat(SchemaValidator.isValid(schema), is(true));
}
示例2: findField
import org.apache.avro.Schema.Type; //導入依賴的package包/類
private static Field findField(Schema schema, String name) {
if (schema.getField(name) != null) {
return schema.getField(name);
}
Field foundField = null;
for (Field field : schema.getFields()) {
Schema fieldSchema = field.schema();
if (Type.RECORD.equals(fieldSchema.getType())) {
foundField = findField(fieldSchema, name);
} else if (Type.ARRAY.equals(fieldSchema.getType())) {
foundField = findField(fieldSchema.getElementType(), name);
} else if (Type.MAP.equals(fieldSchema.getType())) {
foundField = findField(fieldSchema.getValueType(), name);
}
if (foundField != null) {
return foundField;
}
}
return foundField;
}
示例3: getSinglePrimitiveTypeOfUnion
import org.apache.avro.Schema.Type; //導入依賴的package包/類
public static PrimitiveType getSinglePrimitiveTypeOfUnion(Schema fieldSchema) {
PrimitiveType primitiveType = null;
List<Schema> unionTypes = fieldSchema.getTypes();
for (Schema unionType : unionTypes) {
if (unionType.getType() != Type.NULL) {
if (PrimitiveType.isPrimitive(unionType)) {
if (primitiveType == null) {
primitiveType = PrimitiveType.getPrimitiveType(unionType);
} else {
primitiveType = null;
break;
}
}
}
}
return primitiveType;
}
示例4: getUsedTypeFromUnion
import org.apache.avro.Schema.Type; //導入依賴的package包/類
private static Type getUsedTypeFromUnion(Object avroObjField) {
if (GenericRecord.class.isAssignableFrom(avroObjField.getClass())) {
return Type.RECORD;
} else if (Number.class.isAssignableFrom(avroObjField.getClass())) {
return Type.LONG;
} else if (Boolean.class.isAssignableFrom(avroObjField.getClass())) {
return Type.BOOLEAN;
} else if (String.class.isAssignableFrom(avroObjField.getClass())) {
return Type.STRING;
} else if (byte[].class.isAssignableFrom(avroObjField.getClass()) || ByteBuffer.class.isAssignableFrom(avroObjField.getClass())) {
return Type.BYTES;
} else if (Map.class.isAssignableFrom(avroObjField.getClass())) {
return Type.MAP;
} else if (List.class.isAssignableFrom(avroObjField.getClass())) {
return Type.ARRAY;
} else if (Enum.class.isAssignableFrom(avroObjField.getClass())) {
return Type.ENUM;
} else {
LOGGER.error("Used type of union is unknown.");
return null;
}
}
示例5: lookupWriterField
import org.apache.avro.Schema.Type; //導入依賴的package包/類
/**
* Identifies the writer field that corresponds to the specified reader field.
*
* <p> Matching includes reader name aliases. </p>
*
* @param writerSchema Schema of the record where to look for the writer field.
* @param readerField Reader field to identify the corresponding writer field of.
* @return the writer field, if any does correspond, or None.
*/
public static Field lookupWriterField(final Schema writerSchema, final Field readerField) {
assert (writerSchema.getType() == Type.RECORD);
final List<Field> writerFields = new ArrayList<Field>();
final Field direct = writerSchema.getField(readerField.name());
if (direct != null) {
writerFields.add(direct);
}
for (final String readerFieldAliasName : readerField.aliases()) {
final Field writerField = writerSchema.getField(readerFieldAliasName);
if (writerField != null) {
writerFields.add(writerField);
}
}
switch (writerFields.size()) {
case 0: return null;
case 1: return writerFields.get(0);
default: {
throw new AvroRuntimeException(String.format(
"Reader record field %s matches multiple fields in writer record schema %s",
readerField, writerSchema));
}
}
}
示例6: toAvroType
import org.apache.avro.Schema.Type; //導入依賴的package包/類
private Type toAvroType(String columnName, int sqlType) {
Properties mapping = options.getMapColumnJava();
if (mapping.containsKey(columnName)) {
String type = mapping.getProperty(columnName);
if (LOG.isDebugEnabled()) {
LOG.info("Overriding type of column " + columnName + " to " + type);
}
if (type.equalsIgnoreCase("INTEGER")) { return Type.INT; }
if (type.equalsIgnoreCase("LONG")) { return Type.LONG; }
if (type.equalsIgnoreCase("BOOLEAN")) { return Type.BOOLEAN; }
if (type.equalsIgnoreCase("FLOAT")) { return Type.FLOAT; }
if (type.equalsIgnoreCase("DOUBLE")) { return Type.DOUBLE; }
if (type.equalsIgnoreCase("STRING")) { return Type.STRING; }
if (type.equalsIgnoreCase("BYTES")) { return Type.BYTES; }
// Mapping was not found
throw new IllegalArgumentException("Cannot convert to AVRO type " + type);
}
return connManager.toAvroType(tableName, columnName, sqlType);
}
示例7: testOverrideTypeMapping
import org.apache.avro.Schema.Type; //導入依賴的package包/類
public void testOverrideTypeMapping() throws IOException {
String [] types = { "INT" };
String [] vals = { "10" };
createTableWithColTypes(types, vals);
String [] extraArgs = { "--map-column-java", "DATA_COL0=String"};
runImport(getOutputArgv(true, extraArgs));
Schema schema = getSchema();
assertEquals(Type.RECORD, schema.getType());
List<Field> fields = schema.getFields();
assertEquals(types.length, fields.size());
checkField(fields.get(0), "DATA_COL0", Type.STRING);
DatasetReader<GenericRecord> reader = getReader();
try {
assertTrue(reader.hasNext());
GenericRecord record1 = reader.next();
assertEquals("DATA_COL0", "10", record1.get("DATA_COL0"));
assertFalse(reader.hasNext());
} finally {
reader.close();
}
}
示例8: testFirstUnderscoreInColumnName
import org.apache.avro.Schema.Type; //導入依賴的package包/類
public void testFirstUnderscoreInColumnName() throws IOException {
String [] names = { "_NAME" };
String [] types = { "INT" };
String [] vals = { "1987" };
createTableWithColTypesAndNames(names, types, vals);
runImport(getOutputArgv(true, null));
Schema schema = getSchema();
assertEquals(Type.RECORD, schema.getType());
List<Field> fields = schema.getFields();
assertEquals(types.length, fields.size());
checkField(fields.get(0), "__NAME", Type.INT);
DatasetReader<GenericRecord> reader = getReader();
try {
assertTrue(reader.hasNext());
GenericRecord record1 = reader.next();
assertEquals("__NAME", 1987, record1.get("__NAME"));
assertFalse(reader.hasNext());
} finally {
reader.close();
}
}
示例9: testNonIdentCharactersInColumnName
import org.apache.avro.Schema.Type; //導入依賴的package包/類
public void testNonIdentCharactersInColumnName() throws IOException {
String [] names = { "test_p-a+r/quet" };
String [] types = { "INT" };
String [] vals = { "2015" };
createTableWithColTypesAndNames(names, types, vals);
runImport(getOutputArgv(true, null));
Schema schema = getSchema();
assertEquals(Type.RECORD, schema.getType());
List<Field> fields = schema.getFields();
assertEquals(types.length, fields.size());
checkField(fields.get(0), "TEST_P_A_R_QUET", Type.INT);
DatasetReader<GenericRecord> reader = getReader();
try {
assertTrue(reader.hasNext());
GenericRecord record1 = reader.next();
assertEquals("TEST_P_A_R_QUET", 2015, record1.get("TEST_P_A_R_QUET"));
assertFalse(reader.hasNext());
} finally {
reader.close();
}
}
示例10: testFirstUnderscoreInColumnName
import org.apache.avro.Schema.Type; //導入依賴的package包/類
public void testFirstUnderscoreInColumnName() throws IOException {
String [] names = { "_NAME" };
String [] types = { "INT" };
String [] vals = { "1987" };
createTableWithColTypesAndNames(names, types, vals);
runImport(getOutputArgv(true, null));
Path outputFile = new Path(getTablePath(), "part-m-00000.avro");
DataFileReader<GenericRecord> reader = read(outputFile);
Schema schema = reader.getSchema();
assertEquals(Schema.Type.RECORD, schema.getType());
List<Field> fields = schema.getFields();
assertEquals(types.length, fields.size());
checkField(fields.get(0), "__NAME", Type.INT);
GenericRecord record1 = reader.next();
assertEquals("__NAME", 1987, record1.get("__NAME"));
}
示例11: testNonstandardCharactersInColumnName
import org.apache.avro.Schema.Type; //導入依賴的package包/類
public void testNonstandardCharactersInColumnName() throws IOException {
String [] names = { "avro\uC3A11" };
String [] types = { "INT" };
String [] vals = { "1987" };
createTableWithColTypesAndNames(names, types, vals);
runImport(getOutputArgv(true, null));
Path outputFile = new Path(getTablePath(), "part-m-00000.avro");
DataFileReader<GenericRecord> reader = read(outputFile);
Schema schema = reader.getSchema();
assertEquals(Schema.Type.RECORD, schema.getType());
List<Field> fields = schema.getFields();
assertEquals(types.length, fields.size());
checkField(fields.get(0), "AVRO1", Type.INT);
GenericRecord record1 = reader.next();
assertEquals("AVRO1", 1987, record1.get("AVRO1"));
}
示例12: testNonIdentCharactersInColumnName
import org.apache.avro.Schema.Type; //導入依賴的package包/類
public void testNonIdentCharactersInColumnName() throws IOException {
String [] names = { "test_a-v+r/o" };
String [] types = { "INT" };
String [] vals = { "2015" };
createTableWithColTypesAndNames(names, types, vals);
runImport(getOutputArgv(true, null));
Path outputFile = new Path(getTablePath(), "part-m-00000.avro");
DataFileReader<GenericRecord> reader = read(outputFile);
Schema schema = reader.getSchema();
assertEquals(Schema.Type.RECORD, schema.getType());
List<Field> fields = schema.getFields();
assertEquals(types.length, fields.size());
checkField(fields.get(0), "TEST_A_V_R_O", Type.INT);
GenericRecord record1 = reader.next();
assertEquals("TEST_A_V_R_O", 2015, record1.get("TEST_A_V_R_O"));
}
示例13: isEnabled
import org.apache.avro.Schema.Type; //導入依賴的package包/類
@Override
public boolean isEnabled(NodeType type, String attributeName) {
Schema fieldSchema = field.schema();
Type schemaType = fieldSchema.getType();
switch (attributeName) {
case AvroAttributes.PRIMITIVE_TYPE:
if (PrimitiveType.isPrimitive(fieldSchema)) {
return true;
} else {
switch (schemaType) {
case UNION:
// it is enabled if there is only one child with primitive type
return SchemaUtil.getSinglePrimitiveTypeOfUnion(fieldSchema) != null;
case ARRAY:
case MAP:
return PrimitiveType.getPrimitiveType(fieldSchema) != null;
default:
return false;
}
}
case AvroAttributes.OPTIONAL:
// optional is always enabled
return true;
}
return true;
}
示例14: valueIn
import org.apache.avro.Schema.Type; //導入依賴的package包/類
@Test
public void valueIn() {
NumberParameter np = new NumberParameter("n", "en");
Number result = np.valueIn("five", Type.INT);
assertNull(result);
result = np.valueIn("5", Type.INT);
assertEquals(Integer.class, result.getClass());
assertEquals(5, result.intValue());
result = np.valueIn("5", Type.LONG);
assertEquals(Long.class, result.getClass());
assertEquals(5, result.longValue());
result = np.valueIn("5", Type.FLOAT);
assertEquals(Float.class, result.getClass());
assertEquals("5.0", result.toString());
result = np.valueIn("5", Type.DOUBLE);
assertEquals(Double.class, result.getClass());
assertEquals("5.0", result.toString());
}
示例15: getJdbcType
import org.apache.avro.Schema.Type; //導入依賴的package包/類
public static JdbcType getJdbcType(Schema schema, int length, int scale) throws IOException {
Type type = schema.getType();
switch(type) {
case MAP : return JdbcType.BLOB;
case ARRAY : return JdbcType.BLOB;
case BOOLEAN: return JdbcType.BIT;
case BYTES : return JdbcType.BLOB;
case DOUBLE : return JdbcType.DOUBLE;
case ENUM : return JdbcType.VARCHAR;
case FIXED : return JdbcType.BINARY;
case FLOAT : return JdbcType.FLOAT;
case INT : return JdbcType.INTEGER;
case LONG : return JdbcType.BIGINT;
case NULL : break;
case RECORD : return JdbcType.BLOB;
case STRING : return JdbcType.VARCHAR;
case UNION : throw new IOException("Union is not supported yet");
}
return null;
}