avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1511449 - in /avro/trunk: ./ lang/java/tools/src/main/java/org/apache/avro/tool/ lang/java/tools/src/test/idl/ lang/java/tools/src/test/java/org/apache/avro/tool/
Date Wed, 07 Aug 2013 19:22:57 GMT
Author: cutting
Date: Wed Aug  7 19:22:57 2013
New Revision: 1511449

URL: http://svn.apache.org/r1511449
Log:
AVRO-1337. Java: Add a command line tool to generate schema files from a protocol.  Contributed
by Bertrand Dechoux.

Added:
    avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/IdlToSchemataTool.java 
 (with props)
    avro/trunk/lang/java/tools/src/test/idl/
    avro/trunk/lang/java/tools/src/test/idl/protocol.avdl
    avro/trunk/lang/java/tools/src/test/java/org/apache/avro/tool/TestIdlToSchemataTool.java
  (with props)
Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/Main.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1511449&r1=1511448&r2=1511449&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Wed Aug  7 19:22:57 2013
@@ -16,6 +16,9 @@ Trunk (not yet released)
 
     AVRO-896. C: Snappy compression codec. (Grisha Trubetskoy via dcreager)
 
+    AVRO-1337. Java: Add a command line tool to generate schema files
+    from a protocol. (Bertrand Dechoux via cutting)
+
   IMPROVEMENTS
 
     AVRO-1260. Ruby: Improve read performance. (Martin Kleppmann via cutting)

Added: avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/IdlToSchemataTool.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/IdlToSchemataTool.java?rev=1511449&view=auto
==============================================================================
--- avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/IdlToSchemataTool.java (added)
+++ avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/IdlToSchemataTool.java Wed
Aug  7 19:22:57 2013
@@ -0,0 +1,90 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.avro.tool;
+
+import org.apache.avro.Schema;
+import org.apache.avro.compiler.idl.Idl;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.PrintStream;
+import java.util.List;
+
+/**
+ * Extract the Avro JSON schemata of the types of a protocol defined through an
+ * idl format file.
+ */
+public class IdlToSchemataTool implements Tool {
+  @Override
+  public int run(InputStream in, PrintStream out, PrintStream err,
+      List<String> args) throws Exception {
+    if (args.isEmpty() || args.size() > 2 || isRequestingHelp(args)) {
+      err.println("Usage: idl2schemata [idl] [outdir]");
+      err.println("");
+      err.println("If an output directory is not specified, "
+          + "outputs to current directory.");
+      return -1;
+    }
+
+    boolean pretty = true;
+    Idl parser = new Idl(new File(args.get(0)));
+    File outputDirectory = getOutputDirectory(args);
+
+    for (Schema schema : parser.CompilationUnit().getTypes()) {
+      print(schema, outputDirectory, pretty);
+    }
+    parser.close();
+
+    return 0;
+  }
+
+  private boolean isRequestingHelp(List<String> args) {
+    return args.size() == 1
+        && (args.get(0).equals("--help") || args.get(0).equals("-help"));
+  }
+
+  private File getOutputDirectory(List<String> args) {
+    String dirname = (args.size() == 2) ? args.get(1) : "";
+    File outputDirectory = new File(dirname);
+    outputDirectory.mkdirs();
+    return outputDirectory;
+  }
+
+  private void print(Schema schema, File outputDirectory, boolean pretty)
+      throws FileNotFoundException {
+    String dirpath = outputDirectory.getAbsolutePath();
+    String filename = dirpath + "/" + schema.getName() + ".avsc";
+    FileOutputStream fileOutputStream = new FileOutputStream(filename);
+    PrintStream printStream = new PrintStream(fileOutputStream);
+    printStream.println(schema.toString(pretty));
+    printStream.close();
+  }
+
+  @Override
+  public String getName() {
+    return "idl2schemata";
+  }
+
+  @Override
+  public String getShortDescription() {
+    return "Extract JSON schemata of the types from an Avro IDL file";
+  }
+}

Propchange: avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/IdlToSchemataTool.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/Main.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/Main.java?rev=1511449&r1=1511448&r2=1511449&view=diff
==============================================================================
--- avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/Main.java (original)
+++ avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/Main.java Wed Aug  7 19:22:57
2013
@@ -46,6 +46,7 @@ public class Main {
         new DataFileGetMetaTool(),
         new DataFileGetSchemaTool(),
         new IdlTool(),
+        new IdlToSchemataTool(),
         new RecodecTool(),
         new ConcatTool(),
         new RpcReceiveTool(),

Added: avro/trunk/lang/java/tools/src/test/idl/protocol.avdl
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/tools/src/test/idl/protocol.avdl?rev=1511449&view=auto
==============================================================================
--- avro/trunk/lang/java/tools/src/test/idl/protocol.avdl (added)
+++ avro/trunk/lang/java/tools/src/test/idl/protocol.avdl Wed Aug  7 19:22:57 2013
@@ -0,0 +1,40 @@
+/**
+ * An example protocol in Avro IDL
+ */
+@namespace("org.apache.avro.test")
+protocol Simple {
+
+  @aliases(["org.foo.KindOf"])
+  enum Kind {
+    FOO,
+    BAR, // the bar enum value
+    BAZ
+  }
+
+  fixed MD5(16);
+
+  record TestRecord {
+    @order("ignore")
+    string name;
+
+    @order("descending")
+    Kind kind;
+
+    MD5 hash;
+
+    union { MD5, null} @aliases(["hash"]) nullableHash;
+
+    array<long> arrayOfLongs;
+  }
+
+  error TestError {
+    string message;
+  }
+
+  string hello(string greeting);
+  TestRecord echo(TestRecord `record`);
+  int add(int arg1, int arg2);
+  bytes echoBytes(bytes data);
+  void `error`() throws TestError;
+  void ping() oneway;
+}
\ No newline at end of file

Added: avro/trunk/lang/java/tools/src/test/java/org/apache/avro/tool/TestIdlToSchemataTool.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/tools/src/test/java/org/apache/avro/tool/TestIdlToSchemataTool.java?rev=1511449&view=auto
==============================================================================
--- avro/trunk/lang/java/tools/src/test/java/org/apache/avro/tool/TestIdlToSchemataTool.java
(added)
+++ avro/trunk/lang/java/tools/src/test/java/org/apache/avro/tool/TestIdlToSchemataTool.java
Wed Aug  7 19:22:57 2013
@@ -0,0 +1,41 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro.tool;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Test;
+
+public class TestIdlToSchemataTool {
+
+  @Test
+  public void testSplitIdlIntoSchemata() throws Exception {
+    String idl = "src/test/idl/protocol.avdl";
+    String outdir = "target/test-split";
+
+    List<String> arglist = Arrays.asList(idl, outdir);
+    new IdlToSchemataTool().run(null, null, null, arglist);
+
+    String[] files = new File(outdir).list();
+    assertEquals(4, files.length);
+  }
+}

Propchange: avro/trunk/lang/java/tools/src/test/java/org/apache/avro/tool/TestIdlToSchemataTool.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message