@Override public String toString() { List<String> fieldStrings = new ArrayList<String>(); for (PojoField field : fields) { fieldStrings.add(field.getField().getName() + ": " + field.getTypeInformation().toString()); } return "PojoType<" + getTypeClass().getName() + ", fields = [" + StringUtils.join(fieldStrings, ", ") + "]" + ">"; }
@SuppressWarnings("unchecked") @Internal private static <T extends SpecificRecordBase> List<PojoField> generateFieldsFromAvroSchema(Class<T> typeClass) { PojoTypeExtractor pte = new PojoTypeExtractor(); ArrayList<Type> typeHierarchy = new ArrayList<>(); typeHierarchy.add(typeClass); TypeInformation ti = pte.analyzePojo(typeClass, typeHierarchy, null, null, null); if (!(ti instanceof PojoTypeInfo)) { throw new IllegalStateException("Expecting type to be a PojoTypeInfo"); } PojoTypeInfo pti = (PojoTypeInfo) ti; List<PojoField> newFields = new ArrayList<>(pti.getTotalFields()); for (int i = 0; i < pti.getArity(); i++) { PojoField f = pti.getPojoFieldAt(i); TypeInformation newType = f.getTypeInformation(); // check if type is a CharSequence if (newType instanceof GenericTypeInfo) { if ((newType).getTypeClass().equals(CharSequence.class)) { // replace the type by a org.apache.avro.util.Utf8 newType = new GenericTypeInfo(org.apache.avro.util.Utf8.class); } } PojoField newField = new PojoField(f.getField(), newType); newFields.add(newField); } return newFields; }
@Override public boolean equals(Object obj) { if (obj instanceof PojoField) { PojoField other = (PojoField) obj; return other.canEqual(this) && type.equals(other.type) && Objects.equals(field, other.field); } else { return false; } }
@Override @PublicEvolving public <X> TypeInformation<X> getTypeAt(int pos) { if (pos < 0 || pos >= this.fields.length) { throw new IndexOutOfBoundsException(); } @SuppressWarnings("unchecked") TypeInformation<X> typed = (TypeInformation<X>) fields[pos].getTypeInformation(); return typed; }
throw new InvalidTypesException("Field '" + field.getKey() + "'could not be accessed."); pojoFields.add(new PojoField(f, field.getValue()));
@PublicEvolving public String[] getFieldNames() { String[] result = new String[fields.length]; for (int i = 0; i < fields.length; i++) { result[i] = fields[i].getField().getName(); } return result; }
@PublicEvolving public PojoTypeInfo(Class<T> typeClass, List<PojoField> fields) { super(typeClass); checkArgument(Modifier.isPublic(typeClass.getModifiers()), "POJO %s is not public", typeClass); this.fields = fields.toArray(new PojoField[fields.size()]); Arrays.sort(this.fields, new Comparator<PojoField>() { @Override public int compare(PojoField o1, PojoField o2) { return o1.getField().getName().compareTo(o2.getField().getName()); } }); int counterFields = 0; for(PojoField field : fields) { counterFields += field.getTypeInformation().getTotalFields(); } totalFields = counterFields; }
fieldTypeHierarchy.add(fieldType); TypeInformation<?> ti = createTypeInfoWithTypeHierarchy(fieldTypeHierarchy, fieldType, in1Type, in2Type); pojoFields.add(new PojoField(field, ti)); } catch (InvalidTypesException e) { Class<?> genericClass = Object.class; genericClass = typeToClass(fieldType); pojoFields.add(new PojoField(field, new GenericTypeInfo<OUT>((Class<OUT>) genericClass)));
private static TypeInformation<?> getTypeOfPojoField(TypeInformation<?> pojoInfo, Field field) { for (int j = 0; j < pojoInfo.getArity(); j++) { PojoField pf = ((PojoTypeInfo<?>) pojoInfo).getPojoFieldAt(j); if (pf.getField().getName().equals(field.getName())) { return pf.getTypeInformation(); } } return null; }
@Override @PublicEvolving public int getFieldIndex(String fieldName) { for (int i = 0; i < fields.length; i++) { if (fields[i].getField().getName().equals(fieldName)) { return i; } } return -1; }
@SuppressWarnings("unchecked") @Internal private static <T extends SpecificRecordBase> List<PojoField> generateFieldsFromAvroSchema(Class<T> typeClass) { PojoTypeExtractor pte = new PojoTypeExtractor(); ArrayList<Type> typeHierarchy = new ArrayList<>(); typeHierarchy.add(typeClass); TypeInformation ti = pte.analyzePojo(typeClass, typeHierarchy, null, null, null); if (!(ti instanceof PojoTypeInfo)) { throw new IllegalStateException("Expecting type to be a PojoTypeInfo"); } PojoTypeInfo pti = (PojoTypeInfo) ti; List<PojoField> newFields = new ArrayList<>(pti.getTotalFields()); for (int i = 0; i < pti.getArity(); i++) { PojoField f = pti.getPojoFieldAt(i); TypeInformation newType = f.getTypeInformation(); // check if type is a CharSequence if (newType instanceof GenericTypeInfo) { if ((newType).getTypeClass().equals(CharSequence.class)) { // replace the type by a org.apache.avro.util.Utf8 newType = new GenericTypeInfo(org.apache.avro.util.Utf8.class); } } PojoField newField = new PojoField(f.getField(), newType); newFields.add(newField); } return newFields; }
@Test public void testRecursivePojo3() { TypeInformation<?> ti = TypeExtractor.createTypeInfo(RecursivePojo3.class); Assert.assertTrue(ti instanceof PojoTypeInfo); PojoField pf = ((PojoTypeInfo) ti).getPojoFieldAt(0); Assert.assertTrue(pf.getTypeInformation() instanceof PojoTypeInfo); Assert.assertEquals(GenericTypeInfo.class, ((PojoTypeInfo) pf.getTypeInformation()).getPojoFieldAt(0).getTypeInformation().getClass()); }
public PojoBuilder field(String name, DataType type) throws NoSuchFieldException { fields.add(new PojoField(typeClass.getDeclaredField(name), TypeConverters.createExternalTypeInfoFromDataType(type))); return this; }
@Override public boolean equals(Object obj) { if (obj instanceof PojoField) { PojoField other = (PojoField) obj; return other.canEqual(this) && type.equals(other.type) && Objects.equals(field, other.field); } else { return false; } }
public PojoSerializer<T> createPojoSerializer(ExecutionConfig config) { TypeSerializer<?>[] fieldSerializers = new TypeSerializer<?>[fields.length]; Field[] reflectiveFields = new Field[fields.length]; for (int i = 0; i < fields.length; i++) { fieldSerializers[i] = fields[i].getTypeInformation().createSerializer(config); reflectiveFields[i] = fields[i].getField(); } return new PojoSerializer<T>(getTypeClass(), fieldSerializers, reflectiveFields, config); }
HashMap<String, TaggedValue> containerMapping = new HashMap<String, TaggedValue>(); for (int i = 0; i < pojoTypeInfo.getArity(); i++) { final String fieldName = pojoTypeInfo.getPojoFieldAt(i).getField().getName(); containerMapping.put(fieldName, convertTypeInfoToTaggedValue(input,
TypeInformation newType = f.getTypeInformation(); PojoField newField = new PojoField(f.getField(), newType); newFields.add(newField);
@Test public void testPojosWithMutualRecursion() { TypeInformation<?> ti = TypeExtractor.createTypeInfo(MutualPojoB.class); Assert.assertTrue(ti instanceof PojoTypeInfo); TypeInformation<?> pti = ((PojoTypeInfo) ti).getPojoFieldAt(0).getTypeInformation(); Assert.assertTrue(pti instanceof PojoTypeInfo); Assert.assertEquals(GenericTypeInfo.class, ((PojoTypeInfo) pti).getPojoFieldAt(0).getTypeInformation().getClass()); }
throw new InvalidTypesException("Field '" + field.getKey() + "'could not be accessed."); pojoFields.add(new PojoField(f, field.getValue()));