AvroFileAppender.java org::apache::iceberg::avro::AvroFileAppender org::apache::iceberg::avro /* *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.avro; importjava.io.IOException; importjava.util.Map; importjava.util.function.Function; importorg.apache.avro.Schema; importorg.apache.avro.file.CodecFactory; importorg.apache.avro.file.DataFileWriter; importorg.apache.avro.io.DatumWriter; importorg.apache.iceberg.Metrics; importorg.apache.iceberg.MetricsConfig; importorg.apache.iceberg.exceptions.RuntimeIOException; importorg.apache.iceberg.io.FileAppender; importorg.apache.iceberg.io.OutputFile; importorg.apache.iceberg.io.PositionOutputStream; importorg.apache.iceberg.relocated.com.google.common.base.Preconditions; classAvroFileAppender<D>implementsFileAppender<D>{ privatePositionOutputStreamstream; privateDataFileWriter<D>writer; privateDatumWriter<?>datumWriter; privateorg.apache.iceberg.SchemaicebergSchema; privateMetricsConfigmetricsConfig; privatelongnumRecords=0L; privatebooleanisClosed=false; AvroFileAppender( org.apache.iceberg.SchemaicebergSchema, Schemaschema, OutputFilefile, Function<Schema,DatumWriter<?>>createWriterFunc, CodecFactorycodec, Map<String,String>metadata, MetricsConfigmetricsConfig, booleanoverwrite) throwsIOException{ this.icebergSchema=icebergSchema; this.stream=overwrite?file.createOrOverwrite():file.create(); this.datumWriter=createWriterFunc.apply(schema); this.writer=newAvroWriter(schema,stream,datumWriter,codec,metadata); this.metricsConfig=metricsConfig; } @Override publicvoidadd(Ddatum){ try{ numRecords+=1L; writer.append(datum); }catch(IOExceptione){ thrownewRuntimeIOException(e); } } @Override publicMetricsmetrics(){ Preconditions.checkState(isClosed,"Cannotreturnmetricswhileappendingtoanopenfile."); returnAvroMetrics.fromWriter(datumWriter,icebergSchema,numRecords,metricsConfig); } @Override publiclonglength(){ if(stream!=null){ try{ returnstream.storedLength(); }catch(IOExceptione){ thrownewRuntimeIOException(e,"Failedtogetstreamlength"); } } thrownewRuntimeIOException("Failedtogetstreamlength:noopenstream"); } @Override publicvoidclose()throwsIOException{ if(writer!=null){ writer.close(); this.writer=null; isClosed=true; } } @SuppressWarnings("unchecked") privatestatic<D>DataFileWriter<D>newAvroWriter( Schemaschema, PositionOutputStreamstream, DatumWriter<?>metricsAwareDatumWriter, CodecFactorycodec, Map<String,String>metadata) throwsIOException{ DataFileWriter<D>writer=newDataFileWriter<>((DatumWriter<D>)metricsAwareDatumWriter); writer.setCodec(codec); for(Map.Entry<String,String>entry:metadata.entrySet()){ writer.setMeta(entry.getKey(),entry.getValue()); } returnwriter.create(schema,stream); } }