diff --git a/lang/java/tools/src/main/java/org/apache/avro/tool/DataFileReadTool.java b/lang/java/tools/src/main/java/org/apache/avro/tool/DataFileReadTool.java index fb5ef7227f4..87ff94aa592 100644 --- a/lang/java/tools/src/main/java/org/apache/avro/tool/DataFileReadTool.java +++ b/lang/java/tools/src/main/java/org/apache/avro/tool/DataFileReadTool.java @@ -18,6 +18,7 @@ package org.apache.avro.tool; import java.io.BufferedInputStream; +import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.util.ArrayList; @@ -35,9 +36,12 @@ import org.apache.avro.generic.GenericDatumWriter; import org.apache.avro.io.EncoderFactory; import org.apache.avro.io.JsonEncoder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** Reads a data file and dumps to JSON */ public class DataFileReadTool implements Tool { + private static final Logger LOG = LoggerFactory.getLogger(DataFileReadTool.class); private static final long DEFAULT_HEAD_COUNT = 10; @Override @@ -62,18 +66,13 @@ public int run(InputStream stdin, PrintStream out, PrintStream err, List .ofType(String.class); OptionSet optionSet = optionParser.parse(args.toArray(new String[0])); - Boolean pretty = optionSet.has(prettyOption); + boolean pretty = optionSet.has(prettyOption); List nargs = new ArrayList<>((List) optionSet.nonOptionArguments()); String readerSchemaStr = readerSchemaOption.value(optionSet); String readerSchemaFile = readerSchemaFileOption.value(optionSet); - Schema readerSchema = null; - if (readerSchemaFile != null) { - readerSchema = Util.parseSchemaFromFS(readerSchemaFile); - } else if (readerSchemaStr != null) { - readerSchema = new Schema.Parser().parse(readerSchemaStr); - } + Schema readerSchema = getSchema(readerSchemaStr, readerSchemaFile); long headCount = getHeadCount(optionSet, headOption, nargs); @@ -92,7 +91,7 @@ public int run(InputStream stdin, PrintStream out, PrintStream err, List } try (DataFileStream streamReader = new DataFileStream<>(inStream, reader)) { Schema schema = readerSchema != null ? readerSchema : streamReader.getSchema(); - DatumWriter writer = new GenericDatumWriter<>(schema); + DatumWriter writer = new GenericDatumWriter<>(schema); JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, out, pretty); for (long recordCount = 0; streamReader.hasNext() && recordCount < headCount; recordCount++) { Object datum = streamReader.next(); @@ -105,6 +104,18 @@ public int run(InputStream stdin, PrintStream out, PrintStream err, List return 0; } + static Schema getSchema(String schemaStr, String schemaFile) throws IOException { + Schema readerSchema = null; + if (schemaFile != null) { + LOG.info("Reading schema from file '{}'", schemaFile); + readerSchema = Util.parseSchemaFromFS(schemaFile); + } else if (schemaStr != null) { + LOG.info("Reading schema from string '{}'", schemaStr); + readerSchema = new Schema.Parser().parse(schemaStr); + } + return readerSchema; + } + private static long getHeadCount(OptionSet optionSet, OptionSpec headOption, List nargs) { long headCount = Long.MAX_VALUE; if (optionSet.has(headOption)) { diff --git a/lang/java/tools/src/main/java/org/apache/avro/tool/DataFileWriteTool.java b/lang/java/tools/src/main/java/org/apache/avro/tool/DataFileWriteTool.java index b763a00bee4..4bf2f79a0b2 100644 --- a/lang/java/tools/src/main/java/org/apache/avro/tool/DataFileWriteTool.java +++ b/lang/java/tools/src/main/java/org/apache/avro/tool/DataFileWriteTool.java @@ -72,7 +72,7 @@ public int run(InputStream stdin, PrintStream out, PrintStream err, List p.printHelpOn(err); return 1; } - Schema schema = (schemafile != null) ? Util.parseSchemaFromFS(schemafile) : new Schema.Parser().parse(schemastr); + Schema schema = DataFileReadTool.getSchema(schemastr, schemafile); DatumReader reader = new GenericDatumReader<>(schema); diff --git a/lang/java/tools/src/test/java/org/apache/avro/tool/TestDataFileRepairTool.java b/lang/java/tools/src/test/java/org/apache/avro/tool/TestDataFileRepairTool.java index b73fc964b2f..0e24b8915f0 100644 --- a/lang/java/tools/src/test/java/org/apache/avro/tool/TestDataFileRepairTool.java +++ b/lang/java/tools/src/test/java/org/apache/avro/tool/TestDataFileRepairTool.java @@ -180,7 +180,7 @@ void repairAfterCorruptRecord() throws Exception { } private void checkFileContains(File repairedFile, String... lines) throws IOException { - DataFileReader r = new DataFileReader<>(repairedFile, new GenericDatumReader<>(SCHEMA)); + DataFileReader r = new DataFileReader<>(repairedFile, new GenericDatumReader<>(SCHEMA)); for (String line : lines) { assertEquals(line, r.next().toString()); }