You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by cu...@apache.org on 2009/11/20 05:39:29 UTC

svn commit: r882410 - in /hadoop/avro/trunk: CHANGES.txt build.xml src/java/org/apache/avro/reflect/InduceSchemaTool.java src/java/org/apache/avro/tool/Main.java src/test/bin/test_avroj.sh

Author: cutting
Date: Fri Nov 20 04:39:28 2009
New Revision: 882410

URL: http://svn.apache.org/viewvc?rev=882410&view=rev
Log:
AVRO-154. Add 'induce' sub-command to avroj command line tool.  Contributed by Philip Zeyliger.

Added:
    hadoop/avro/trunk/src/java/org/apache/avro/reflect/InduceSchemaTool.java
Modified:
    hadoop/avro/trunk/CHANGES.txt
    hadoop/avro/trunk/build.xml
    hadoop/avro/trunk/src/java/org/apache/avro/tool/Main.java
    hadoop/avro/trunk/src/test/bin/test_avroj.sh

Modified: hadoop/avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=882410&r1=882409&r2=882410&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Fri Nov 20 04:39:28 2009
@@ -17,6 +17,9 @@
 
     AVRO-158. Permit appending to a data file from Java.  (cutting)
 
+    AVRO-154. Add 'induce' sub-command to avroj command line tool.
+    (Philip Zeyliger via cutting)
+
   IMPROVEMENTS
 
     AVRO-157. Changes from code review comments for C++. (sbanacho)

Modified: hadoop/avro/trunk/build.xml
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/build.xml?rev=882410&r1=882409&r2=882410&view=diff
==============================================================================
--- hadoop/avro/trunk/build.xml (original)
+++ hadoop/avro/trunk/build.xml Fri Nov 20 04:39:28 2009
@@ -461,7 +461,7 @@
     <chmod file="${build.dir}/avroj-${version}.jar" perm="ugo+x"/>
   </target>
 
-  <target name="test-avroj" depends="avroj"
+  <target name="test-avroj" depends="avroj,compile-test-java"
    description="Tests avroj commands">
     <exec executable="${basedir}/src/test/bin/test_avroj.sh"
           failonerror="true">

Added: hadoop/avro/trunk/src/java/org/apache/avro/reflect/InduceSchemaTool.java
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/reflect/InduceSchemaTool.java?rev=882410&view=auto
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/reflect/InduceSchemaTool.java (added)
+++ hadoop/avro/trunk/src/java/org/apache/avro/reflect/InduceSchemaTool.java Fri Nov 20 04:39:28 2009
@@ -0,0 +1,59 @@
+/**
+ * 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.reflect;
+
+import java.io.File;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.List;
+
+import org.apache.avro.tool.Tool;
+
+/**
+ * Utility to induce the schema of a given class using
+ * reflection.
+ */
+public class InduceSchemaTool implements Tool {
+
+  @Override
+  public void run(List<String> args) throws Exception {
+    if (args.size() == 0 || args.size() > 2) {
+      System.err.println("Usage: [colon-delimited-classpath] classname");
+      return;
+    }
+    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+    String className;
+    if (args.size() == 2) {
+      String classpaths = args.get(0);
+      className = args.get(1);
+      if (!classpaths.isEmpty()) {
+        String[] paths = args.get(0).split(":");
+        URL[] urls = new URL[paths.length];
+        for (int i = 0; i < paths.length; ++i) {
+          urls[i] = new File(paths[i]).toURI().toURL();
+        }
+        classLoader = URLClassLoader.newInstance(urls, classLoader);
+      }
+    } else {
+      className = args.get(0);
+    }
+
+    Class<?> klass = classLoader.loadClass(className);
+    System.out.println(ReflectData.get().getSchema(klass).toString());
+  }
+}

Modified: hadoop/avro/trunk/src/java/org/apache/avro/tool/Main.java
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/tool/Main.java?rev=882410&r1=882409&r2=882410&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/tool/Main.java (original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/tool/Main.java Fri Nov 20 04:39:28 2009
@@ -21,6 +21,7 @@
 import java.util.Map;
 import java.util.TreeMap;
 
+import org.apache.avro.reflect.InduceSchemaTool;
 import org.apache.avro.specific.SpecificCompiler.SpecificCompilerTool;
 
 /** Command-line driver.*/
@@ -33,6 +34,7 @@
   private Main() {
     tools = new TreeMap<String, Tool>();
     tools.put("compile", new SpecificCompilerTool());
+    tools.put("induce", new InduceSchemaTool());
   }
 
   public static void main(String[] args) throws Exception {

Modified: hadoop/avro/trunk/src/test/bin/test_avroj.sh
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/test/bin/test_avroj.sh?rev=882410&r1=882409&r2=882410&view=diff
==============================================================================
--- hadoop/avro/trunk/src/test/bin/test_avroj.sh (original)
+++ hadoop/avro/trunk/src/test/bin/test_avroj.sh Fri Nov 20 04:39:28 2009
@@ -53,7 +53,10 @@
 [ "Foo.java Interop.java Kind.java MD5.java Node.java " = \
   "$(find $TMPDIR/schema -name "*.java" \
     | awk -F "/" '{ print $NF }' | sort | tr '\n' ' ')" ]
-
+######################################################################
+echo "Testing induce schema..."
+$AVROJ induce build/test/classes org.apache.avro.BarRecord | grep -q -F \
+  '{"type":"record","name":"BarRecord","namespace":"org.apache.avro","fields":[{"name":"beerMsg","type":"string"}]}'
 ######################################################################
 $CMD 2>&1 | grep -q "Expected one of the following"
 $CMD doesnotexist 2>&1 | grep -q "Expected one of the following"