Refine search
public Record(Schema schema) { if (schema == null || !Type.RECORD.equals(schema.getType())) throw new AvroRuntimeException("Not a record schema: "+schema); this.schema = schema; this.values = new Object[schema.getFields().size()]; } public Record(Record other, boolean deepCopy) {
Throwable cause = e.getCause(); if (cause != null && cause.getClass() == ClassCastException.class) {
switch (schema.getType()) { case ENUM: newNamespace = namespaceOverride.containsKey(schema.getNamespace()) ? namespaceOverride.get(schema.getNamespace()) : schema.getNamespace(); newSchema = if (schema.getFields().size() > 0) { for (Schema.Field oldField : schema.getFields()) { Field newField = new Field(oldField.name(), switchNamespace(oldField.schema(), namespaceOverride), oldField.doc(), oldField.defaultValue(), oldField.order()); newFields.add(newField); LOG.error(exceptionMessage); throw new AvroRuntimeException(exceptionMessage);
private static int hashCode(HashData data, Schema schema) throws IOException { Decoder decoder = data.decoder; switch (schema.getType()) { case RECORD: { int hashCode = 1; for (Field field : schema.getFields()) { if (field.order() == Field.Order.IGNORE) { GenericDatumReader.skip(field.schema(), decoder); continue; hashCode = hashCode*31 + hashCode(data, field.schema()); Schema elementType = schema.getElementType(); int hashCode = 1; for (long l = decoder.readArrayStart(); l != 0; l = decoder.arrayNext()) throw new AvroRuntimeException("Can't hashCode maps!"); case UNION: return hashCode(data, schema.getTypes().get(decoder.readInt())); return 0; default: throw new AvroRuntimeException("Unexpected schema to hashCode!");
if (topLevel && !schema.getType().equals(Schema.Type.RECORD)) { throw new IllegalArgumentException( String.format("Schema for table must be of type RECORD. Received type: %s for dataset %s", schema.getType(), datasetName)); switch (schema.getType()) { case RECORD: isFirst = true; columns.append(", \n"); String type = generateAvroToHiveColumnMapping(field.schema(), hiveColumns, false, datasetName); if (hiveColumns.isPresent()) { hiveColumns.get().put(field.name(), type); String flattenSource = field.getProp("flatten_source"); if (StringUtils.isBlank(flattenSource)) { flattenSource = field.name(); isFirst = true; for (Schema unionMember : schema.getTypes()) { if (Schema.Type.NULL.equals(unionMember.getType())) { continue; String.format("DDL query generation failed for \"%s\" of dataset %s", schema, datasetName); log.error(exceptionMessage); throw new AvroRuntimeException(exceptionMessage);
public Array(int capacity, Schema schema) { if (schema == null || !Type.ARRAY.equals(schema.getType())) throw new AvroRuntimeException("Not an array schema: "+schema); this.schema = schema; if (capacity != 0) elements = new Object[capacity]; } public Array(Schema schema, Collection<T> c) {
protected int compare(Object o1, Object o2, Schema s, boolean equals) { if (o1 == o2) return 0; switch (s.getType()) { case RECORD: for (Field f : s.getFields()) { if (f.order() == Field.Order.IGNORE) continue; // ignore this field int pos = f.pos(); String name = f.name(); int compare = compare(getField(o1, name, pos), getField(o2, name, pos), return s.getEnumOrdinal(o1.toString()) - s.getEnumOrdinal(o2.toString()); case ARRAY: Collection a1 = (Collection)o1; if (equals) return ((Map)o1).equals(o2) ? 0 : 1; throw new AvroRuntimeException("Can't compare maps!"); case UNION: int i1 = resolveUnion(s, o1);
public static Object toObject(JsonNode jsonNode, Schema schema) { if (schema != null && schema.getType().equals(Schema.Type.UNION)) { return toObject(jsonNode, schema.getTypes().get(0)); return jsonNode.asBoolean(); } else if (jsonNode.isInt()) { if (schema == null || schema.getType().equals(Schema.Type.INT)) { return jsonNode.asInt(); } else if (schema.getType().equals(Schema.Type.LONG)) { return jsonNode.asLong(); return jsonNode.getTextValue().getBytes(BYTES_CHARSET); } catch (UnsupportedEncodingException e) { throw new AvroRuntimeException(e); s = schema.getValueType(); } else if (schema.getType().equals(Schema.Type.RECORD)) { s = schema.getField(key).schema();
JsonNode json = field.defaultValue(); if (json == null) throw new AvroRuntimeException("Field " + field + " not set and has no default value"); if (json.isNull() && (field.schema().getType() == Type.NULL || (field.schema().getType() == Type.UNION && field.schema().getTypes().get(0).getType() == Type.NULL))) { return null; throw new AvroRuntimeException(e);
public java.lang.Object get(int field$) { switch (field$) { case 0: return name; case 1: return favoriteNumber; case 2: return favoriteColor; case 3: return eventType; default: throw new org.apache.avro.AvroRuntimeException("Bad index"); } }
/** * Helper method that does the actual work for {@link #getField(Schema, String)} * @param schema passed from {@link #getFieldSchema(Schema, String)} * @param pathList passed from {@link #getFieldSchema(Schema, String)} * @param field keeps track of the index used to access the list pathList * @return the field */ private static Optional<Field> getFieldHelper(Schema schema, List<String> pathList, int field) { Field curField = schema.getField(pathList.get(field)); if (field + 1 == pathList.size()) { return Optional.fromNullable(curField); } Schema fieldSchema = curField.schema(); switch (fieldSchema.getType()) { case UNION: throw new AvroRuntimeException("Union of complex types cannot be handled : " + schema); case MAP: return AvroUtils.getFieldHelper(fieldSchema.getValueType(), pathList, ++field); case RECORD: return AvroUtils.getFieldHelper(fieldSchema, pathList, ++field); default: throw new AvroRuntimeException("Invalid type in schema : " + schema); } }
assert (writerSchema.getType() == Type.RECORD); final List<Field> writerFields = new ArrayList<>(); 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); 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));
public UnionSchema(LockableArrayList<Schema> types) { super(Type.UNION); this.types = types.lock(); int index = 0; for (Schema type : types) { if (type.getType() == Type.UNION) throw new AvroRuntimeException("Nested union: "+this); String name = type.getFullName(); if (name == null) throw new AvroRuntimeException("Nameless in union:"+this); if (indexByName.put(name, index++) != null) throw new AvroRuntimeException("Duplicate in union:" + name); } } public List<Schema> getTypes() { return types; }
@Override public Object respond(Message message, Object request) throws Exception { int numParams = message.getRequest().getFields().size(); Object[] params = new Object[numParams]; Class[] paramTypes = new Class[numParams]; int i = 0; try { for (Schema.Field param: message.getRequest().getFields()) { params[i] = ((GenericRecord)request).get(param.name()); paramTypes[i] = getSpecificData().getClass(param.schema()); i++; } Method method = impl.getClass().getMethod(message.getName(), paramTypes); method.setAccessible(true); return method.invoke(impl, params); } catch (InvocationTargetException e) { if (e.getTargetException() instanceof Exception) { throw (Exception) e.getTargetException(); } else { throw new Exception(e.getTargetException()); } } catch (NoSuchMethodException e) { throw new AvroRuntimeException(e); } catch (IllegalAccessException e) { throw new AvroRuntimeException(e); } }
@Override public void put(String key, Object value) { Schema.Field field = schema.getField(key); if (field == null) throw new AvroRuntimeException("Not a valid schema field: "+key); values[field.pos()] = value; } @Override public void put(int i, Object v) { values[i] = v; }
ResolvingDecoder in, Object state) throws IOException { if (state != null) { FieldAccessor accessor = ((FieldAccessor[]) state)[f.pos()]; if (accessor != null) { if (accessor.supportsIO() && (!Schema.Type.UNION.equals(f.schema().getType()) || accessor.isCustomEncoded())) { accessor.read(record, in); String asString = (String) read(null, f.schema(), in); accessor.set(record, asString == null ? null return; } catch (Exception e) { throw new AvroRuntimeException("Failed to read Stringable", e); LogicalType logicalType = f.schema().getLogicalType(); if (logicalType != null) { Conversion<?> conversion = getData().getConversionByClass( f.schema(), logicalType, conversion)); } catch (IllegalAccessException e) { throw new AvroRuntimeException("Failed to set " + f); return; } catch (IllegalAccessException e) { throw new AvroRuntimeException("Failed to set " + f);
/** Create a schema for a field. */ protected Schema createFieldSchema(Field field, Map<String, Schema> names) { AvroEncode enc = field.getAnnotation(AvroEncode.class); if (enc != null) try { return enc.using().newInstance().getSchema(); } catch (Exception e) { throw new AvroRuntimeException("Could not create schema from custom serializer for " + field.getName()); } AvroSchema explicit = field.getAnnotation(AvroSchema.class); if (explicit != null) // explicit schema return Schema.parse(explicit.value()); Schema schema = createSchema(field.getGenericType(), names); if (field.isAnnotationPresent(Stringable.class)) { // Stringable schema = Schema.create(Schema.Type.STRING); } if (field.isAnnotationPresent(Nullable.class)) // nullable schema = makeNullable(schema); return schema; }
static Class getClassProp(Schema schema, String prop) { String name = schema.getProp(prop); if (name == null) return null; Class c = CLASS_CACHE.get(name); if (c != null) return c; try { c = ClassUtils.forName(name); CLASS_CACHE.put(name, c); } catch (ClassNotFoundException e) { throw new AvroRuntimeException(e); } return c; }
@Test public void testDefaultRecordWithDuplicateFieldName() { String recordName = "name"; Schema schema = Schema.createRecord(recordName, "doc", "namespace", false); List<Field> fields = new ArrayList<>(); fields.add(new Field("field_name", Schema.create(Type.NULL), null, null)); fields.add(new Field("field_name", Schema.create(Type.INT), null, null)); try { schema.setFields(fields); fail("Should not be able to create a record with duplicate field name."); } catch (AvroRuntimeException are) { assertTrue(are.getMessage().contains("Duplicate field field_name in record " + recordName)); } }
return Type.NULL.getName(); if (isRecord(datum)) return getRecordSchema(datum).getFullName(); if (isEnum(datum)) return getEnumSchema(datum).getFullName(); if (isArray(datum)) return Type.ARRAY.getName(); if (isMap(datum)) return Type.MAP.getName(); if (isFixed(datum)) return getFixedSchema(datum).getFullName(); if (isString(datum)) return Type.STRING.getName(); if (isBoolean(datum)) return Type.BOOLEAN.getName(); throw new AvroRuntimeException (String.format("Unknown datum type %s: %s", datum.getClass().getName(), datum));