Polish bd53168ddb
This commit is contained in:
@@ -39,6 +39,7 @@ import org.springframework.util.Assert;
|
||||
* An {@link ItemReader} that deserializes data from a {@link Resource} containing serialized Avro objects.
|
||||
*
|
||||
* @author David Turanski
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @since 4.2
|
||||
*/
|
||||
public class AvroItemReader<T> extends AbstractItemCountingItemStreamItemReader<T> {
|
||||
@@ -102,10 +103,10 @@ public class AvroItemReader<T> extends AbstractItemCountingItemStreamItemReader<
|
||||
|
||||
@Override
|
||||
protected T doRead() throws Exception {
|
||||
if (inputStreamReader != null) {
|
||||
return inputStreamReader.read();
|
||||
if (this.inputStreamReader != null) {
|
||||
return this.inputStreamReader.read();
|
||||
}
|
||||
return dataFileReader.hasNext()? dataFileReader.next(): null;
|
||||
return this.dataFileReader.hasNext()? this.dataFileReader.next(): null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -116,10 +117,10 @@ public class AvroItemReader<T> extends AbstractItemCountingItemStreamItemReader<
|
||||
@Override
|
||||
protected void doClose() throws Exception {
|
||||
if (this.inputStreamReader != null) {
|
||||
inputStreamReader.close();
|
||||
this.inputStreamReader.close();
|
||||
return;
|
||||
}
|
||||
dataFileReader.close();
|
||||
this.dataFileReader.close();
|
||||
}
|
||||
|
||||
private void initializeReader() throws IOException {
|
||||
@@ -160,8 +161,8 @@ public class AvroItemReader<T> extends AbstractItemCountingItemStreamItemReader<
|
||||
}
|
||||
|
||||
private T read() throws Exception {
|
||||
if (!binaryDecoder.isEnd()) {
|
||||
return datumReader.read(null, binaryDecoder);
|
||||
if (!this.binaryDecoder.isEnd()) {
|
||||
return this.datumReader.read(null, this.binaryDecoder);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @since 4.2
|
||||
* @author David Turanski
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
public class AvroItemWriter<T> extends AbstractItemStreamItemWriter<T> {
|
||||
|
||||
@@ -55,8 +56,6 @@ public class AvroItemWriter<T> extends AbstractItemStreamItemWriter<T> {
|
||||
|
||||
private WritableResource resource;
|
||||
|
||||
private Schema schema;
|
||||
|
||||
private Resource schemaResource;
|
||||
|
||||
private Class<T> clazz;
|
||||
@@ -109,6 +108,7 @@ public class AvroItemWriter<T> extends AbstractItemStreamItemWriter<T> {
|
||||
*/
|
||||
@Override
|
||||
public void open(ExecutionContext executionContext) {
|
||||
super.open(executionContext);
|
||||
try {
|
||||
initializeWriter();
|
||||
} catch (IOException e) {
|
||||
@@ -120,7 +120,7 @@ public class AvroItemWriter<T> extends AbstractItemStreamItemWriter<T> {
|
||||
public void close() {
|
||||
try {
|
||||
if (this.dataFileWriter != null) {
|
||||
dataFileWriter.close();
|
||||
this.dataFileWriter.close();
|
||||
}
|
||||
else {
|
||||
this.outputStreamWriter.close();
|
||||
@@ -132,22 +132,23 @@ public class AvroItemWriter<T> extends AbstractItemStreamItemWriter<T> {
|
||||
}
|
||||
|
||||
private void initializeWriter() throws IOException {
|
||||
Assert.notNull(resource, "'resource' is required.");
|
||||
Assert.notNull(clazz, "'class' is required.");
|
||||
Assert.notNull(this.resource, "'resource' is required.");
|
||||
Assert.notNull(this.clazz, "'class' is required.");
|
||||
|
||||
if (this.embedSchema) {
|
||||
Assert.notNull(this.schemaResource, "'schema' is required.");
|
||||
Assert.state(this.schemaResource.exists(),
|
||||
"'schema' " + this.schemaResource.getFilename() + " does not exist.");
|
||||
Schema schema;
|
||||
try {
|
||||
this.schema = new Schema.Parser().parse(this.schemaResource.getInputStream());
|
||||
schema = new Schema.Parser().parse(this.schemaResource.getInputStream());
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException(e.getMessage(), e);
|
||||
}
|
||||
this.dataFileWriter = new DataFileWriter<>(datumWriterForClass(this.clazz));
|
||||
this.dataFileWriter.create(this.schema, this.resource.getOutputStream());
|
||||
this.dataFileWriter.create(schema, this.resource.getOutputStream());
|
||||
} else {
|
||||
this.outputStreamWriter = createOutputStreamWriter(resource.getOutputStream(),
|
||||
this.outputStreamWriter = createOutputStreamWriter(this.resource.getOutputStream(),
|
||||
datumWriterForClass(this.clazz));
|
||||
}
|
||||
|
||||
@@ -184,8 +185,8 @@ public class AvroItemWriter<T> extends AbstractItemStreamItemWriter<T> {
|
||||
}
|
||||
|
||||
private void write(T datum) throws Exception {
|
||||
datumWriter.write(datum, binaryEncoder);
|
||||
binaryEncoder.flush();
|
||||
this.datumWriter.write(datum, this.binaryEncoder);
|
||||
this.binaryEncoder.flush();
|
||||
}
|
||||
|
||||
private void close() {
|
||||
|
||||
@@ -25,9 +25,10 @@ import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* A builder implementation for the {@link AvroItemReader}
|
||||
* A builder implementation for the {@link AvroItemReader}.
|
||||
*
|
||||
* @author David Turanski
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @since 4.2
|
||||
*/
|
||||
public class AvroItemReaderBuilder<T> {
|
||||
@@ -196,7 +197,7 @@ public class AvroItemReaderBuilder<T> {
|
||||
|
||||
private AvroItemReader<T> buildForSchema() {
|
||||
Assert.notNull(this.schema, "'schema' is required.");
|
||||
return new AvroItemReader<>(resource, schema);
|
||||
return new AvroItemReader<>(this.resource, this.schema);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -23,7 +23,10 @@ import org.springframework.core.io.WritableResource;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A builder implementation for the {@link AvroItemWriter}.
|
||||
*
|
||||
* @author David Turanski
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @since 4.2
|
||||
*/
|
||||
public class AvroItemWriterBuilder<T> {
|
||||
@@ -107,11 +110,10 @@ public class AvroItemWriterBuilder<T> {
|
||||
|
||||
Assert.notNull(this.type, "A 'type' is required.");
|
||||
|
||||
|
||||
AvroItemWriter<T> avroItemWriter = this.schema != null ?
|
||||
new AvroItemWriter(this.resource, this.schema, this.type):
|
||||
new AvroItemWriter<>(this.resource, this.schema, this.type):
|
||||
new AvroItemWriter<>(this.resource, this.type);
|
||||
avroItemWriter.setName(name);
|
||||
avroItemWriter.setName(this.name);
|
||||
return avroItemWriter;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user