本文整理匯總了Java中org.apache.avro.Schema.getType方法的典型用法代碼示例。如果您正苦於以下問題:Java Schema.getType方法的具體用法?Java Schema.getType怎麽用?Java Schema.getType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.avro.Schema
的用法示例。
在下文中一共展示了Schema.getType方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getSinglePrimitiveTypeOfUnion
import org.apache.avro.Schema; //導入方法依賴的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;
}
示例2: isEnabled
import org.apache.avro.Schema; //導入方法依賴的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;
}
示例3: matches
import org.apache.avro.Schema; //導入方法依賴的package包/類
private MatchType matches(JsonNode datum, Schema schema) {
switch (schema.getType()) {
case RECORD:
if (datum.isObject()) {
return matchRecord(datum, schema);
}
break;
case UNION:
return resolveUnion(datum, schema.getTypes()).matchType;
case MAP:
if (datum.isObject()) {
return matchMapValue(datum, schema);
}
break;
case ARRAY:
if (datum.isArray()) {
return matchArrayElement(datum, schema);
}
break;
case BOOLEAN:
if (datum.isBoolean()) {
return MatchType.FULL;
}
break;
case FLOAT:
if (datum.isDouble() && datum.doubleValue() >= -Float.MAX_VALUE && datum.doubleValue() <= Float.MAX_VALUE
|| datum.isLong()
&& datum.longValue() >= (long) -Float.MAX_VALUE
&& datum.longValue() <= (long) Float.MAX_VALUE
|| datum.isFloat()
|| datum.isInt()) {
return MatchType.FULL;
}
break;
case DOUBLE:
if (datum.isDouble() || datum.isFloat() || datum.isLong() || datum.isInt()) {
return MatchType.FULL;
}
break;
case INT:
if (datum.isInt()) {
return MatchType.FULL;
}
break;
case LONG:
if (datum.isLong() || datum.isInt()) {
return MatchType.FULL;
}
break;
case STRING:
if (datum.isTextual()) {
return MatchType.FULL;
}
break;
case ENUM:
if (datum.isTextual() && schema.hasEnumSymbol(datum.textValue())) {
return MatchType.FULL;
}
break;
case BYTES:
case FIXED:
if (datum.isTextual()) {
return MatchType.FULL;
}
break;
case NULL:
if (datum.isNull()) {
return MatchType.FULL;
}
break;
default: // unknown
throw new IllegalArgumentException("Unsupported schema: " + schema);
}
return MatchType.NONE;
}
示例4: nullOk
import org.apache.avro.Schema; //導入方法依賴的package包/類
/**
* Returns whether null is allowed by the schema.
* <p>
* </p>
* Copied from {@code org.kitesdk.data.spi.SchemaUtil.nullOk(Schema)}.
*
* @param schema a Schema
* @return true if schema allows the value to be null
*/
private static boolean nullOk(Schema schema) {
if (Schema.Type.NULL == schema.getType()) {
return true;
} else if (Schema.Type.UNION == schema.getType()) {
for (Schema possible : schema.getTypes()) {
if (nullOk(possible)) {
return true;
}
}
}
return false;
}
示例5: internalValidate
import org.apache.avro.Schema; //導入方法依賴的package包/類
private void internalValidate(Schema schema) throws SchemaValidationException {
if (schema.getType() == RECORD) {
String name = schema.getFullName();
if (recordTypeNames.contains(name)) {
return;
}
recordTypeNames.add(name);
for (Field field : schema.getFields()) {
internalValidate(field.schema());
}
} else if (schema.getType() == MAP) {
internalValidate(schema.getValueType());
} else if (schema.getType() == ARRAY) {
internalValidate(schema.getElementType());
} else if (schema.getType() == UNION) {
boolean containsBytes = false;
boolean containsString = false;
for (Schema unionSchema : schema.getTypes()) {
if (unionSchema.getType() == Schema.Type.BYTES) {
containsBytes = true;
} else if (unionSchema.getType() == Schema.Type.STRING) {
containsString = true;
}
if (containsBytes && containsString) {
String message = "Schema contains a variant of union[bytes, string]: " + unionSchema.toString();
throw new SchemaValidationException(message);
}
if (COMPOSITE_TYPES.contains(unionSchema)) {
internalValidate(unionSchema);
}
}
}
}
示例6: processRecord
import org.apache.avro.Schema; //導入方法依賴的package包/類
private void processRecord(final GenericContainer container, final Schema schema) {
final Schema.Type type = schema.getType();
switch (type) {
case RECORD:
process(container, schema, null, new MapOrListWriter(writer.rootAsMap()));
break;
default:
throw new DrillRuntimeException("Root object must be record type. Found: " + type);
}
}
示例7: processRecord
import org.apache.avro.Schema; //導入方法依賴的package包/類
private void processRecord(final GenericContainer container, final Schema schema) {
final Schema.Type type = schema.getType();
switch (type) {
case RECORD:
process(container, schema, null, new MapOrListWriterImpl(writer.rootAsMap()), fieldSelection);
break;
default:
throw new RuntimeException("Root object must be record type. Found: " + type);
}
}
示例8: unionHasTypedChild
import org.apache.avro.Schema; //導入方法依賴的package包/類
public static boolean unionHasTypedChild(Schema unionSchema, Type type) {
List<Schema> unionTypes = unionSchema.getTypes();
for (Schema unionType : unionTypes) {
if (unionType.getType() == type) {
return true;
}
}
return false;
}
示例9: isMultiChoiceUnion
import org.apache.avro.Schema; //導入方法依賴的package包/類
public static boolean isMultiChoiceUnion(Schema unionSchema) {
List<Schema> unionTypes = unionSchema.getTypes();
int nbrOfNoNullChild = 0;
for (Schema unionType : unionTypes) {
if (unionType.getType() != Type.NULL) {
nbrOfNoNullChild++;
}
}
return nbrOfNoNullChild > 1;
}
示例10: get
import org.apache.avro.Schema; //導入方法依賴的package包/類
public static IAvroFormatter get( final Schema avroSchema ){
Schema.Type schemaType = avroSchema.getType();
if( schemaType == Schema.Type.ARRAY ){
return new AvroArrayFormatter( avroSchema );
}
else if( schemaType == Schema.Type.MAP ){
return new AvroMapFormatter( avroSchema );
}
else if( schemaType == Schema.Type.RECORD ){
return new AvroRecordFormatter( avroSchema );
}
else if( schemaType == Schema.Type.UNION ){
return new AvroUnionFormatter( avroSchema );
}
else if( schemaType == Schema.Type.BOOLEAN ){
return new AvroBooleanFormatter();
}
else if( schemaType == Schema.Type.BYTES ){
return new AvroBytesFormatter();
}
else if( schemaType == Schema.Type.DOUBLE ){
return new AvroDoubleFormatter();
}
else if( schemaType == Schema.Type.FLOAT ){
return new AvroFloatFormatter();
}
else if( schemaType == Schema.Type.INT ){
return new AvroIntegerFormatter();
}
else if( schemaType == Schema.Type.LONG ){
return new AvroLongFormatter();
}
else if( schemaType == Schema.Type.STRING ){
return new AvroStringFormatter();
}
else if( schemaType == Schema.Type.NULL ){
return new AvroNullFormatter();
}
else{
return new AvroNullFormatter();
}
}
示例11: getGeneralSchema
import org.apache.avro.Schema; //導入方法依賴的package包/類
public static IField getGeneralSchema( final String fieldName , final Schema avroSchema ) throws IOException{
Schema.Type schemaType = avroSchema.getType();
if( schemaType == Schema.Type.ARRAY ){
return new AvroArraySchema( fieldName, avroSchema ).getGeneralSchema();
}
else if( schemaType == Schema.Type.MAP ){
return new AvroMapSchema( fieldName, avroSchema ).getGeneralSchema();
}
else if( schemaType == Schema.Type.RECORD ){
return new AvroRecordSchema( avroSchema ).getGeneralSchema();
}
else if( schemaType == Schema.Type.UNION ){
List<Schema> childSchemas = avroSchema.getTypes();
if( childSchemas.size() == 2 ){
Schema childSchema0 = childSchemas.get(0);
Schema childSchema1 = childSchemas.get(1);
if( childSchema0.getType() == Schema.Type.NULL && childSchema1.getType() != Schema.Type.NULL ){
return getGeneralSchema( fieldName , childSchema1 );
}
else if( childSchema0.getType() != Schema.Type.NULL && childSchema1.getType() == Schema.Type.NULL ){
return getGeneralSchema( fieldName , childSchema0 );
}
}
return new AvroUnionSchema( avroSchema ).getGeneralSchema();
}
else if( schemaType == Schema.Type.BOOLEAN ){
return new BooleanField( fieldName );
}
else if( schemaType == Schema.Type.BYTES ){
return new BytesField( fieldName );
}
else if( schemaType == Schema.Type.DOUBLE ){
return new DoubleField( fieldName );
}
else if( schemaType == Schema.Type.FLOAT ){
return new FloatField( fieldName );
}
else if( schemaType == Schema.Type.INT ){
return new IntegerField( fieldName );
}
else if( schemaType == Schema.Type.LONG ){
return new LongField( fieldName );
}
else if( schemaType == Schema.Type.STRING ){
return new StringField( fieldName );
}
else if( schemaType == Schema.Type.NULL ){
return new NullField( fieldName );
}
else{
return new NullField( fieldName );
}
}
示例12: fromAvro
import org.apache.avro.Schema; //導入方法依賴的package包/類
/**
* Convert from Avro type to Sqoop's java representation of the SQL type
* see SqlManager#toJavaType
*/
public static Object fromAvro(Object avroObject, Schema schema, String type) {
if (avroObject == null) {
return null;
}
switch (schema.getType()) {
case NULL:
return null;
case BOOLEAN:
case INT:
case FLOAT:
case DOUBLE:
return avroObject;
case LONG:
if (type.equals(DATE_TYPE)) {
return new Date((Long) avroObject);
} else if (type.equals(TIME_TYPE)) {
return new Time((Long) avroObject);
} else if (type.equals(TIMESTAMP_TYPE)) {
return new Timestamp((Long) avroObject);
}
return avroObject;
case BYTES:
ByteBuffer bb = (ByteBuffer) avroObject;
BytesWritable bw = new BytesWritable();
bw.set(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining());
if (type.equals(BLOB_REF_TYPE)) {
// TODO: Should convert BytesWritable to BlobRef properly. (SQOOP-991)
throw new UnsupportedOperationException("BlobRef not supported");
}
return bw;
case STRING:
if (type.equals(BIG_DECIMAL_TYPE)) {
return new BigDecimal(avroObject.toString());
} else if (type.equals(DATE_TYPE)) {
return Date.valueOf(avroObject.toString());
} else if (type.equals(TIME_TYPE)) {
return Time.valueOf(avroObject.toString());
} else if (type.equals(TIMESTAMP_TYPE)) {
return Timestamp.valueOf(avroObject.toString());
}
return avroObject.toString();
case ENUM:
return avroObject.toString();
case UNION:
List<Schema> types = schema.getTypes();
if (types.size() != 2) {
throw new IllegalArgumentException("Only support union with null");
}
Schema s1 = types.get(0);
Schema s2 = types.get(1);
if (s1.getType() == Schema.Type.NULL) {
return fromAvro(avroObject, s2, type);
} else if (s2.getType() == Schema.Type.NULL) {
return fromAvro(avroObject, s1, type);
} else {
throw new IllegalArgumentException("Only support union with null");
}
case FIXED:
return new BytesWritable(((GenericFixed) avroObject).bytes());
case RECORD:
case ARRAY:
case MAP:
default:
throw new IllegalArgumentException("Cannot convert Avro type "
+ schema.getType());
}
}
示例13: detectConverter
import org.apache.avro.Schema; //導入方法依賴的package包/類
static private AbstractAvroValueConverter detectConverter(Schema schema) {
Schema.Type avroType = schema.getType();
switch (avroType) {
case NULL:
return new AvroNullConverter(schema);
case BOOLEAN:
return new AvroBooleanConverter(schema);
case STRING:
return new AvroStringConverter(schema);
case INT:
return new AvroIntConverter(schema);
case LONG:
return new AvroLongConverter(schema);
case FLOAT:
return new AvroFloatConverter(schema);
case DOUBLE:
return new AvroDoubleConverter(schema);
case ENUM:
return new AvroEnumConverter(schema, schema.getEnumSymbols());
case FIXED:
return new AvroFixedConverter(schema);
case UNION:
for (Schema s : schema.getTypes()) {
if (s.getType() != Schema.Type.NULL)
return detectConverter(s);
}
return new AvroNullConverter(schema);
case ARRAY:
return new AvroArrayConverter(schema, detectConverter(schema.getElementType()));
case MAP:
return new AvroMapConverter(schema, detectConverter(schema.getValueType()));
case RECORD:
ImmutableMap.Builder<String, AbstractAvroValueConverter> builder = ImmutableMap.builder();
for (Schema.Field f : schema.getFields()) {
builder.put(f.name(), detectConverter(f.schema()));
}
return new AvroRecordConverter(schema, builder.build());
default:
throw new RuntimeException(String.format("%s of %s is unsupported avro type", schema.getType(), schema.getName()));
}
}
示例14: parseSchema
import org.apache.avro.Schema; //導入方法依賴的package包/類
protected AvroNode parseSchema(Schema schema, AvroNode parentNode) {
AvroNode schemaNode = null;
Type schemaType = schema.getType();
switch (schemaType) {
case RECORD:
schemaNode = parseRecordSchema(schema, parentNode);
break;
case UNION:
schemaNode = parseUnionSchema(schema, parentNode);
break;
case ENUM:
schemaNode = parseEnumNode(schema, parentNode);
break;
case ARRAY:
schemaNode = parseArrayNode(schema, parentNode);
break;
case FIXED:
schemaNode = parseFixedNode(schema, parentNode);
break;
case MAP:
schemaNode = parseMapNode(schema, parentNode);
break;
// Primitive types
case NULL:
case BOOLEAN:
case INT:
case LONG:
case FLOAT:
case DOUBLE:
case BYTES:
case STRING:
schemaNode = parsePrimitiveTypeNode(schema, parentNode);
break;
default:
break;
}
return schemaNode;
}