AvroUtil.java org::apache::iceberg::connect::events::AvroUtil org::apache::iceberg::connect::events /* *LicensedtotheApacheSoftwareFoundation(ASF)underone *ormorecontributorlicenseagreements.SeetheNOTICEfile *distributedwiththisworkforadditionalinformation *regardingcopyrightownership.TheASFlicensesthisfile *toyouundertheApacheLicense,Version2.0(the *"License");youmaynotusethisfileexceptincompliance *withtheLicense.YoumayobtainacopyoftheLicenseat * *http://www.apache.org/licenses/LICENSE-2.0 * *Unlessrequiredbyapplicablelaworagreedtoinwriting, *softwaredistributedundertheLicenseisdistributedonan *"ASIS"BASIS,WITHOUTWARRANTIESORCONDITIONSOFANY *KIND,eitherexpressorimplied.SeetheLicenseforthe *specificlanguagegoverningpermissionsandlimitations *undertheLicense. */ packageorg.apache.iceberg.connect.events; importjava.io.IOException; importjava.io.UncheckedIOException; importjava.util.List; importjava.util.Map; importorg.apache.avro.Schema; importorg.apache.avro.generic.IndexedRecord; importorg.apache.iceberg.DataFile; importorg.apache.iceberg.PartitionData; importorg.apache.iceberg.avro.AvroEncoderUtil; importorg.apache.iceberg.avro.AvroSchemaUtil; importorg.apache.iceberg.data.avro.DecoderResolver; importorg.apache.iceberg.relocated.com.google.common.base.Preconditions; importorg.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; importorg.apache.iceberg.types.Types; publicclassAvroUtil{ staticfinalMap<Integer,String>FIELD_ID_TO_CLASS= ImmutableMap.of( DataComplete.ASSIGNMENTS_ELEMENT, TopicPartitionOffset.class.getName(), DataFile.PARTITION_ID, PartitionData.class.getName(), DataWritten.TABLE_REFERENCE, TableReference.class.getName(), DataWritten.DATA_FILES_ELEMENT, "org.apache.iceberg.GenericDataFile", DataWritten.DELETE_FILES_ELEMENT, "org.apache.iceberg.GenericDeleteFile", CommitToTable.TABLE_REFERENCE, TableReference.class.getName()); publicstaticbyte[]encode(Eventevent){ try{ returnAvroEncoderUtil.encode(event,event.getSchema()); }catch(IOExceptione){ thrownewUncheckedIOException(e); } } publicstaticEventdecode(byte[]bytes){ try{ Eventevent=AvroEncoderUtil.decode(bytes); //clearthecachetoavoidmemoryleak DecoderResolver.clearCache(); returnevent; }catch(IOExceptione){ thrownewUncheckedIOException(e); } } staticSchemaconvert(Types.StructTypeicebergSchema,Class<?extendsIndexedRecord>javaClass){ returnconvert(icebergSchema,javaClass,FIELD_ID_TO_CLASS); } staticSchemaconvert( Types.StructTypeicebergSchema, Class<?extendsIndexedRecord>javaClass, Map<Integer,String>typeMap){ returnAvroSchemaUtil.convert( icebergSchema, (fieldId,struct)-> struct.equals(icebergSchema)?javaClass.getName():typeMap.get(fieldId)); } staticintpositionToId(intposition,SchemaavroSchema){ List<Schema.Field>fields=avroSchema.getFields(); Preconditions.checkArgument( position>=0&&position<fields.size(),"Invalidfieldposition:"+position); Objectval=fields.get(position).getObjectProp(AvroSchemaUtil.FIELD_ID_PROP); returnval==null?-1:(int)val; } privateAvroUtil(){} }