Search in sources :

Example 1 with AvroFormat

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();
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) AvroFormat(com.linkedin.databus2.core.schema.tools.AvroConverter.AvroFormat) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Schema(org.apache.avro.Schema) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SimpleLayout(org.apache.log4j.SimpleLayout) FileOutputStream(java.io.FileOutputStream) ParseException(org.apache.commons.cli.ParseException)

Aggregations

AvroFormat (com.linkedin.databus2.core.schema.tools.AvroConverter.AvroFormat)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Schema (org.apache.avro.Schema)1 ParseException (org.apache.commons.cli.ParseException)1 ConsoleAppender (org.apache.log4j.ConsoleAppender)1 SimpleLayout (org.apache.log4j.SimpleLayout)1