Refine search
@SuppressWarnings({ "unchecked", "rawtypes" }) public void checkStringable(Class c, String value) throws Exception { ReflectData data = new ReflectData(); Schema schema = data.getSchema(c); assertEquals ("{\"type\":\"string\",\"java-class\":\""+c.getName()+"\"}", schema.toString()); checkBinary(schema, c.getConstructor(String.class).newInstance(value)); }
@Override public Class getClass(Schema schema) { Conversion<?> conversion = getConversionFor(schema.getLogicalType()); if (conversion != null) { return conversion.getConvertedType(); switch (schema.getType()) { case ARRAY: Class collectionClass = getClassProp(schema, CLASS_PROP); if (collectionClass != null) return collectionClass; Class elementClass = getClass(schema.getElementType()); if(elementClass.isPrimitive()) { Class stringClass = getClassProp(schema, CLASS_PROP); if (stringClass != null) return stringClass;
/** * Creates a ParquetWriterFactory for the given type. The Parquet writers will use Avro * to reflectively create a schema for the type and use that schema to write the columnar data. * * @param type The class of the type to write. */ public static <T> ParquetWriterFactory<T> forReflectRecord(Class<T> type) { final String schemaString = ReflectData.get().getSchema(type).toString(); final ParquetBuilder<T> builder = (out) -> createAvroParquetWriter(schemaString, ReflectData.get(), out); return new ParquetWriterFactory<>(builder); }
private static Schema getSchema(Object o) { try { return ReflectData.get().getSchema(o.getClass()); } catch (AvroRuntimeException e) { throw new AvroRuntimeException ("Cannot infer schema for : " + o.getClass() + ". Must create Pair with explicit key and value schemas.", e); } }
@Override public boolean validate(Schema schema, Object datum) { switch (schema.getType()) { case RECORD: if (datum == null) return false; Class c = datum.getClass(); for (Schema.Field f : schema.getFields()) { try { if (!validate(f.schema(), getField(c, f.name()).get(datum))) return false; } catch (IllegalAccessException e) { throw new AvroRuntimeException(e); if (datum instanceof Collection) { // collection for (Object element : (Collection)datum) if (!validate(schema.getElementType(), element)) return false; return true; int length = java.lang.reflect.Array.getLength(datum); for (int i = 0; i < length; i++) if (!validate(schema.getElementType(), java.lang.reflect.Array.get(datum, i))) return false;
@Test public void testP0() throws Exception { Protocol p0 = ReflectData.get().getProtocol(P0.class); Protocol.Message message = p0.getMessages().get("foo"); // check response schema is union Schema response = message.getResponse(); assertEquals(Schema.Type.UNION, response.getType()); assertEquals(Schema.Type.NULL, response.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, response.getTypes().get(1).getType()); // check request schema is union Schema request = message.getRequest(); Field field = request.getField("s"); assertNotNull("field 's' should not be null", field); Schema param = field.schema(); assertEquals(Schema.Type.UNION, param.getType()); assertEquals(Schema.Type.NULL, param.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, param.getTypes().get(1).getType()); // check union erasure assertEquals(String.class, ReflectData.get().getClass(response)); assertEquals(String.class, ReflectData.get().getClass(param)); }
private static int reflectAwareHashCode(Object o, Schema s) { if (o == null) return 0; // incomplete datum int hashCode = 1; switch (s.getType()) { case RECORD: for (Schema.Field f : s.getFields()) { if (f.order() == Schema.Field.Order.IGNORE) continue; hashCode = hashCodeAdd(hashCode, ReflectData.get().getField(o, f.name(), f.pos()), f.schema()); } return hashCode; case ARRAY: Collection<?> a = (Collection<?>) o; Schema elementType = s.getElementType(); for (Object e : a) hashCode = hashCodeAdd(hashCode, e, elementType); return hashCode; case UNION: return reflectAwareHashCode(o, s.getTypes().get(ReflectData.get().resolveUnion(s, o))); case ENUM: return s.getEnumOrdinal(o.toString()); case NULL: return 0; case STRING: return (o instanceof Utf8 ? o : new Utf8(o.toString())).hashCode(); default: return o.hashCode(); } }
@Test public void testP2() throws Exception { Schema e1 = ReflectData.get().getSchema(E1.class); assertEquals(Schema.Type.RECORD, e1.getType()); assertTrue(e1.isError()); Field message = e1.getField("detailMessage"); assertNotNull("field 'detailMessage' should not be null", message); Schema messageSchema = message.schema(); assertEquals(Schema.Type.UNION, messageSchema.getType()); assertEquals(Schema.Type.NULL, messageSchema.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, messageSchema.getTypes().get(1).getType()); Protocol p2 = ReflectData.get().getProtocol(P2.class); Protocol.Message m = p2.getMessages().get("error"); // check error schema is union Schema response = m.getErrors(); assertEquals(Schema.Type.UNION, response.getType()); assertEquals(Schema.Type.STRING, response.getTypes().get(0).getType()); assertEquals(e1, response.getTypes().get(1)); }
@Test public void testR11() throws Exception { Schema r11Record = ReflectData.get().getSchema(R11.class); assertEquals(Schema.Type.RECORD, r11Record.getType()); Field r11Field = r11Record.getField("text"); assertEquals(JsonProperties.NULL_VALUE, r11Field.defaultVal()); Schema r11FieldSchema = r11Field.schema(); assertEquals(Schema.Type.UNION, r11FieldSchema.getType()); assertEquals(Schema.Type.NULL, r11FieldSchema.getTypes().get(0).getType()); Schema r11String = r11FieldSchema.getTypes().get(1); assertEquals(Schema.Type.STRING, r11String.getType()); R11 r11 = new R11(); checkReadWrite(r11, r11Record); r11.text = "foo"; checkReadWrite(r11, r11Record); }
@Override public Class getClass(Schema schema) { switch (schema.getType()) { case ARRAY: Class collectionClass = getClassProp(schema, CLASS_PROP); if (collectionClass != null) return collectionClass; return java.lang.reflect.Array.newInstance(getClass(schema.getElementType()),0).getClass(); case STRING: return String.class; case BYTES: return BYTES_CLASS; case INT: if (Short.class.getName().equals(schema.getProp(CLASS_PROP))) return Short.TYPE; default: return super.getClass(schema); } }
void checkReadWrite(Object object, Schema s) throws Exception { ReflectDatumWriter<Object> writer = new ReflectDatumWriter<>(s); ByteArrayOutputStream out = new ByteArrayOutputStream(); writer.write(object, factory.directBinaryEncoder(out, null)); ReflectDatumReader<Object> reader = new ReflectDatumReader<>(s); Object after = reader.read(null, DecoderFactory.get().binaryDecoder( out.toByteArray(), null)); assertEquals(object, after); // check reflective setField works for records if (s.getType().equals(Schema.Type.RECORD)) { Object copy = object.getClass().newInstance(); for (Field f : s.getFields()) { Object val = ReflectData.get().getField(object, f.name(), f.pos()); ReflectData.get().setField(copy, f.name(), f.pos(), val); } assertEquals("setField", object, copy); } }
private Message getMessage(Method method, Protocol protocol, Map<String,Schema> names) { List<Schema.Field> fields = new ArrayList<>(); String[] paramNames = getParameterNames(method); Type[] paramTypes = method.getGenericParameterTypes(); Annotation[][] annotations = method.getParameterAnnotations(); for (int i = 0; i < paramTypes.length; i++) { Schema paramSchema = getSchema(paramTypes[i], names); for (int j = 0; j < annotations[i].length; j++) { Annotation annotation = annotations[i][j]; if (annotation instanceof AvroSchema) // explicit schema paramSchema = Schema.parse(((AvroSchema)annotation).value()); else if (annotation instanceof Union) // union paramSchema = getAnnotatedUnion(((Union)annotation), names); else if (annotation instanceof Nullable) // nullable paramSchema = makeNullable(paramSchema); Schema request = Schema.createRecord(fields); ? getSchema(method.getGenericReturnType(), names) : getAnnotatedUnion(union, names); if (method.isAnnotationPresent(Nullable.class)) // nullable response = makeNullable(response); errs.add(getSchema(err, names)); Schema errors = Schema.createUnion(errs); return protocol.createMessage(method.getName(), null /* doc */, request, response, errors);
@Test public void testDecimalBytes() throws IOException { Schema schema = REFLECT.getSchema(DecimalRecordBytes.class); Assert.assertEquals("Should have the correct record name", "org.apache.avro.reflect.TestReflectLogicalTypes", schema.getNamespace()); Assert.assertEquals("Should have the correct record name", "DecimalRecordBytes", schema.getName()); Assert.assertEquals("Should have the correct logical type", LogicalTypes.decimal(9, 2), LogicalTypes.fromSchema(schema.getField("decimal").schema())); DecimalRecordBytes record = new DecimalRecordBytes(); record.decimal = new BigDecimal("3.14"); File test = write(REFLECT, schema, record); Assert.assertEquals("Should match the decimal after round trip", Arrays.asList(record), read(REFLECT.createDatumReader(schema), test)); }
@Test(expected = IllegalArgumentException.class) public void testReadUUIDMissingLogicalTypeReflect() throws IOException { String unsafeValue = System.getProperty("avro.disable.unsafe"); try { // only one FieldAccess can be set per JVM System.setProperty("avro.disable.unsafe", "true"); Assume.assumeTrue( ReflectionUtil.getFieldAccess() instanceof FieldAccessReflect); Schema uuidSchema = SchemaBuilder.record(RecordWithUUID.class.getName()) .fields().requiredString("uuid").endRecord(); LogicalTypes.uuid().addToSchema(uuidSchema.getField("uuid").schema()); UUID u1 = UUID.randomUUID(); RecordWithStringUUID r1 = new RecordWithStringUUID(); r1.uuid = u1.toString(); File test = write( ReflectData.get().getSchema(RecordWithStringUUID.class), r1); read(ReflectData.get().createDatumReader(uuidSchema), test).get(0); } finally { if (unsafeValue != null) { System.setProperty("avro.disable.unsafe", unsafeValue); } } }
@Override protected void setField(Object record, String name, int position, Object o) { try { ReflectData.getField(record.getClass(), name).set(record, o); } catch (IllegalAccessException e) { throw new AvroRuntimeException(e); } }
@Test public void testP4() throws Exception { Protocol p = ReflectData.get().getProtocol(P4.class); Protocol.Message message = p.getMessages().get("foo"); assertEquals(Schema.Type.INT, message.getResponse().getType()); Field field = message.getRequest().getField("x"); assertEquals(Schema.Type.INT, field.schema().getType()); }
@Test @SuppressWarnings("unchecked") public void testPairRecord() throws IOException { ReflectData model = new ReflectData(); model.addLogicalTypeConversion(new Conversion<Pair>() { @Override public Class<Pair> getConvertedType() { Schema schema = model.getSchema(PairRecord.class); Assert.assertEquals("Should have the correct record name", "org.apache.avro.reflect.TestReflectLogicalTypes", schema.getNamespace()); Assert.assertEquals("Should have the correct record name", "PairRecord", schema.getName()); Assert.assertEquals("Should have the correct logical type", "pair", LogicalTypes.fromSchema(schema.getField("pair").schema()).getName()); <PairRecord>read(model.createDatumReader(schema), test).get(0)).pair; Assert.assertEquals("Data should match after serialization round-trip", 34L, (long) actual.first);
@Test public void testReflectedSchema() { Schema expected = SchemaBuilder.record(RecordWithUUIDList.class.getName()) .fields() .name("uuids").type().array().items().stringType().noDefault() .endRecord(); expected.getField("uuids").schema().addProp( SpecificData.CLASS_PROP, List.class.getName()); LogicalTypes.uuid().addToSchema( expected.getField("uuids").schema().getElementType()); Schema actual = REFLECT.getSchema(RecordWithUUIDList.class); Assert.assertEquals("Should use the UUID logical type", expected, actual); }