use of com.linkedin.databus2.core.schema.tools.AvroConverter.AvroFormat in project databus by linkedin.
the class AvroConvertCli method main.
/**
* @param args
*/
public static void main(String[] args) throws Exception {
ConsoleAppender app = new ConsoleAppender(new SimpleLayout());
Logger.getRootLogger().removeAllAppenders();
Logger.getRootLogger().addAppender(app);
AvroConvertCli cli = new AvroConvertCli();
try {
cli.parseCommandLine(args);
} catch (ParseException pe) {
System.err.println(pe.getMessage());
cli.printUsage();
System.exit(1);
}
if (!cli.hasOptions() || cli.hasHelpOption()) {
cli.printUsage();
System.exit(0);
}
int verbosity = cli.getVerbosity();
switch(verbosity) {
case 0:
Logger.getRootLogger().setLevel(Level.ERROR);
break;
case 1:
Logger.getRootLogger().setLevel(Level.INFO);
break;
case 2:
Logger.getRootLogger().setLevel(Level.DEBUG);
break;
default:
Logger.getRootLogger().setLevel(Level.ALL);
break;
}
AvroFormat inputFormat = cli.getInputFormat(AvroFormat.JSON);
LOG.info("Using input format: " + inputFormat);
AvroFormat outputFormat = cli.getOutputFormat(AvroFormat.JSON);
LOG.info("Using output format: " + outputFormat);
String inputSchemaName = cli.getInputSchema(null);
if (null == inputSchemaName) {
System.err.println("Input schema expected");
cli.printUsage();
System.exit(4);
}
Schema inputSchema = null;
try {
inputSchema = openSchema(inputSchemaName);
} catch (IOException ioe) {
System.err.println("Unable to open input schema: " + ioe);
System.exit(2);
}
LOG.info("Using input schema:" + inputSchemaName);
String outputSchemaName = cli.getOutputSchema(inputSchemaName);
Schema outputSchema = null;
try {
outputSchema = outputSchemaName.equals(inputSchemaName) ? inputSchema : openSchema(outputSchemaName);
} catch (IOException ioe) {
System.err.println("Unable to open output schema: " + ioe);
System.exit(3);
}
LOG.info("Using output schema:" + outputSchemaName);
String inputFileName = cli.getInputFileName("-");
InputStream input = inputFileName.equals("-") ? System.in : new FileInputStream(inputFileName);
LOG.info("Using input: " + inputFileName);
String outputFileName = cli.getOutputFileName("-");
OutputStream output = outputFileName.equals("-") ? System.out : new FileOutputStream(outputFileName);
LOG.info("Using output: " + outputFileName);
AvroConverter avroConverter = new AvroConverter(inputFormat, outputFormat, inputSchema, outputSchema);
avroConverter.convert(input, output);
if (!inputFileName.equals("-"))
input.close();
if (!outputFileName.equals("-"))
output.close();
}
Aggregations