You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@asterixdb.apache.org by "Till Westmann (Code Review)" <do...@asterixdb.incubator.apache.org> on 2016/12/20 22:07:32 UTC

Change in asterixdb[master]: WIP - load from classpath

Till Westmann has uploaded a new change for review.

  https://asterix-gerrit.ics.uci.edu/1400

Change subject: WIP - load from classpath
......................................................................

WIP - load from classpath

- instantiate DelimitedDataParserFactory from config file found
  on classpath

Change-Id: I36a3fca2df9134423fe7590be260cfc3856c7f01
---
M asterixdb/asterix-external-data/pom.xml
M asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/provider/ParserFactoryProvider.java
A asterixdb/asterix-external-data/src/main/resources/META-INF/services/org.apache.asterix.external.api.IDataParserFactory
3 files changed, 64 insertions(+), 7 deletions(-)


  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb refs/changes/00/1400/1

diff --git a/asterixdb/asterix-external-data/pom.xml b/asterixdb/asterix-external-data/pom.xml
index 72c7997..3796a0d 100644
--- a/asterixdb/asterix-external-data/pom.xml
+++ b/asterixdb/asterix-external-data/pom.xml
@@ -106,6 +106,7 @@
             <include>**/NOTICE</include>
             <include>**/LICENSE</include>
             <include>**/DEPENDENCIES</include>
+            <include>**/services/**</include>
           </includes>
         </configuration>
         <executions>
diff --git a/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/provider/ParserFactoryProvider.java b/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/provider/ParserFactoryProvider.java
index ebe3276..c7b56ca 100644
--- a/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/provider/ParserFactoryProvider.java
+++ b/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/provider/ParserFactoryProvider.java
@@ -18,21 +18,34 @@
  */
 package org.apache.asterix.external.provider;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.asterix.common.exceptions.AsterixException;
 import org.apache.asterix.common.library.ILibraryManager;
 import org.apache.asterix.external.api.IDataParserFactory;
 import org.apache.asterix.external.parser.factory.ADMDataParserFactory;
-import org.apache.asterix.external.parser.factory.DelimitedDataParserFactory;
 import org.apache.asterix.external.parser.factory.HiveDataParserFactory;
 import org.apache.asterix.external.parser.factory.RSSParserFactory;
 import org.apache.asterix.external.parser.factory.RecordWithMetadataParserFactory;
 import org.apache.asterix.external.parser.factory.TweetParserFactory;
 import org.apache.asterix.external.util.ExternalDataConstants;
 import org.apache.asterix.external.util.ExternalDataUtils;
+import org.apache.commons.io.IOUtils;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
 
 public class ParserFactoryProvider {
+
+    private static final String RESOURCE = "META-INF/services/org.apache.asterix.external.api.IDataParserFactory";
+    private static Map<String, Class> FACTORIES = null;
 
     private ParserFactoryProvider() {
     }
@@ -56,14 +69,17 @@
 
     @SuppressWarnings("rawtypes")
     public static IDataParserFactory getDataParserFactory(String parser) throws AsterixException {
+        if (FACTORIES == null) {
+            FACTORIES = initFactories();
+        }
+        if (FACTORIES.containsKey(parser)) {
+            return getInstance(FACTORIES.get(parser));
+        }
         switch (parser) {
             case ExternalDataConstants.FORMAT_ADM:
             case ExternalDataConstants.FORMAT_JSON:
             case ExternalDataConstants.FORMAT_SEMISTRUCTURED:
                 return new ADMDataParserFactory();
-            case ExternalDataConstants.FORMAT_DELIMITED_TEXT:
-            case ExternalDataConstants.FORMAT_CSV:
-                return new DelimitedDataParserFactory();
             case ExternalDataConstants.FORMAT_HIVE:
             case ExternalDataConstants.PARSER_HIVE:
                 return new HiveDataParserFactory();
@@ -75,11 +91,47 @@
                 return new RecordWithMetadataParserFactory();
             default:
                 try {
-                    return (IDataParserFactory) Class.forName(parser).newInstance();
-                } catch (IllegalAccessException | ClassNotFoundException | InstantiationException
-                        | ClassCastException e) {
+                    return getInstance(Class.forName(parser));
+                } catch (ClassNotFoundException e) {
                     throw new AsterixException("Unknown format: " + parser, e);
                 }
         }
     }
+
+    protected static IDataParserFactory getInstance(Class clazz) throws AsterixException {
+        try {
+            return (IDataParserFactory) clazz.newInstance();
+        } catch (IllegalAccessException | InstantiationException | ClassCastException e) {
+            throw new AsterixException("Cannot create: " + clazz.getSimpleName(), e);
+        }
+    }
+
+    protected static Map<String, Class> initFactories() throws AsterixException {
+        Map<String, Class> factories = new HashMap<>();
+        ClassLoader cl = ParserFactoryProvider.class.getClassLoader();
+        final Charset encoding = Charset.forName("UTF-8");
+        try {
+            Enumeration<URL> urls = cl.getResources(RESOURCE);
+            for (URL url : Collections.list(urls)) {
+                InputStream is = url.openStream();
+                String config = IOUtils.toString(is, encoding);
+                is.close();
+                JSONObject confObj = new JSONObject(config);
+                String className = confObj.getString("class");
+                final Class<?> clazz = Class.forName(className);
+                JSONArray formatArray = confObj.getJSONArray("formats");
+                for (int i = 0; i < formatArray.length(); ++i) {
+                    String format = formatArray.getString(i);
+                    if (factories.containsKey(format)) {
+                        throw new AsterixException("Duplicate format " + format);
+                    }
+                    // System.err.println("registering " + factory + " for format \"" + format + "\"");
+                    factories.put(format, clazz);
+                }
+            }
+        } catch (IOException | JSONException | ClassNotFoundException e) {
+            throw new AsterixException(e);
+        }
+        return factories;
+    }
 }
diff --git a/asterixdb/asterix-external-data/src/main/resources/META-INF/services/org.apache.asterix.external.api.IDataParserFactory b/asterixdb/asterix-external-data/src/main/resources/META-INF/services/org.apache.asterix.external.api.IDataParserFactory
new file mode 100644
index 0000000..380025a
--- /dev/null
+++ b/asterixdb/asterix-external-data/src/main/resources/META-INF/services/org.apache.asterix.external.api.IDataParserFactory
@@ -0,0 +1,4 @@
+{
+  "formats": [ "delimited-text", "csv" ],
+  "class": "org.apache.asterix.external.parser.factory.DelimitedDataParserFactory"
+}

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/1400
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I36a3fca2df9134423fe7590be260cfc3856c7f01
Gerrit-PatchSet: 1
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Till Westmann <ti...@apache.org>

Change in asterixdb[master]: WIP - load from classpath

Posted by "Xikui Wang (Code Review)" <do...@asterixdb.incubator.apache.org>.
Xikui Wang has posted comments on this change.

Change subject: WIP - load from classpath
......................................................................


Patch Set 2:

(1 comment)

https://asterix-gerrit.ics.uci.edu/#/c/1400/2/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/provider/ParserFactoryProvider.java
File asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/provider/ParserFactoryProvider.java:

Line 47:     private static final String RESOURCE = "META-INF/services/org.apache.asterix.external.api.IDataParserFactory";
> There are different approaches. The only restriction we have is, that we ca
This is a viable solution. I will try this on my branch. Also I'm extending my knowledge on jar packing&loading... Thanks again for the example and explanation!


-- 
To view, visit https://asterix-gerrit.ics.uci.edu/1400
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I36a3fca2df9134423fe7590be260cfc3856c7f01
Gerrit-PatchSet: 2
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-Reviewer: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Xikui Wang <xk...@gmail.com>
Gerrit-HasComments: Yes

Change in asterixdb[master]: WIP - load from classpath

Posted by "Jenkins (Code Review)" <do...@asterixdb.incubator.apache.org>.
Jenkins has posted comments on this change.

Change subject: WIP - load from classpath
......................................................................


Patch Set 2: BAD+1

BAD Compatibility Tests Successful

https://asterix-jenkins.ics.uci.edu/job/asterixbad-compat/93/ : SUCCESS

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/1400
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I36a3fca2df9134423fe7590be260cfc3856c7f01
Gerrit-PatchSet: 2
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-HasComments: No

Change in asterixdb[master]: WIP - load from classpath

Posted by "Jenkins (Code Review)" <do...@asterixdb.incubator.apache.org>.
Jenkins has posted comments on this change.

Change subject: WIP - load from classpath
......................................................................


Patch Set 2:

Integration Tests Started https://asterix-jenkins.ics.uci.edu/job/asterix-gerrit-integration-tests/1369/

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/1400
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I36a3fca2df9134423fe7590be260cfc3856c7f01
Gerrit-PatchSet: 2
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-HasComments: No

Change in asterixdb[master]: WIP - load from classpath

Posted by "Till Westmann (Code Review)" <do...@asterixdb.incubator.apache.org>.
Hello Jenkins,

I'd like you to reexamine a change.  Please visit

    https://asterix-gerrit.ics.uci.edu/1400

to look at the new patch set (#2).

Change subject: WIP - load from classpath
......................................................................

WIP - load from classpath

- instantiate DelimitedDataParserFactory from config file found
  on classpath

Change-Id: I36a3fca2df9134423fe7590be260cfc3856c7f01
---
M asterixdb/asterix-external-data/pom.xml
M asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/provider/ParserFactoryProvider.java
A asterixdb/asterix-external-data/src/main/resources/META-INF/services/org.apache.asterix.external.api.IDataParserFactory
3 files changed, 64 insertions(+), 7 deletions(-)


  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb refs/changes/00/1400/2
-- 
To view, visit https://asterix-gerrit.ics.uci.edu/1400
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: I36a3fca2df9134423fe7590be260cfc3856c7f01
Gerrit-PatchSet: 2
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>

Change in asterixdb[master]: WIP - load from classpath

Posted by "Jenkins (Code Review)" <do...@asterixdb.incubator.apache.org>.
Jenkins has posted comments on this change.

Change subject: WIP - load from classpath
......................................................................


Patch Set 2: Integration-Tests+1

Integration Tests Successful

https://asterix-jenkins.ics.uci.edu/job/asterix-gerrit-integration-tests/1369/ : SUCCESS

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/1400
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I36a3fca2df9134423fe7590be260cfc3856c7f01
Gerrit-PatchSet: 2
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-HasComments: No

Change in asterixdb[master]: WIP - load from classpath

Posted by "Xikui Wang (Code Review)" <do...@asterixdb.incubator.apache.org>.
Xikui Wang has posted comments on this change.

Change subject: WIP - load from classpath
......................................................................


Patch Set 2:

(1 comment)

https://asterix-gerrit.ics.uci.edu/#/c/1400/2/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/provider/ParserFactoryProvider.java
File asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/provider/ParserFactoryProvider.java:

Line 47:     private static final String RESOURCE = "META-INF/services/org.apache.asterix.external.api.IDataParserFactory";
Should we make this RESOURCE as a directory? As I see from the later initFactories method, each resource file will be loaded with one class that maps to keyword. I would imagine there are multiple parser factories need to be loaded, then there may have multiple config files in this directory?


-- 
To view, visit https://asterix-gerrit.ics.uci.edu/1400
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I36a3fca2df9134423fe7590be260cfc3856c7f01
Gerrit-PatchSet: 2
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-Reviewer: Xikui Wang <xk...@gmail.com>
Gerrit-HasComments: Yes

Change in asterixdb[master]: WIP - load from classpath

Posted by "Till Westmann (Code Review)" <do...@asterixdb.incubator.apache.org>.
Till Westmann has abandoned this change.

Change subject: WIP - load from classpath
......................................................................


Abandoned

Superseded by https://asterix-gerrit.ics.uci.edu/#/c/1416/

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/1400
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: abandon
Gerrit-Change-Id: I36a3fca2df9134423fe7590be260cfc3856c7f01
Gerrit-PatchSet: 2
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-Reviewer: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Xikui Wang <xk...@gmail.com>

Change in asterixdb[master]: WIP - load from classpath

Posted by "Jenkins (Code Review)" <do...@asterixdb.incubator.apache.org>.
Jenkins has posted comments on this change.

Change subject: WIP - load from classpath
......................................................................


Patch Set 2:

Build Started https://asterix-jenkins.ics.uci.edu/job/asterix-gerrit-notopic/3629/

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/1400
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I36a3fca2df9134423fe7590be260cfc3856c7f01
Gerrit-PatchSet: 2
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-HasComments: No

Change in asterixdb[master]: WIP - load from classpath

Posted by "Jenkins (Code Review)" <do...@asterixdb.incubator.apache.org>.
Jenkins has posted comments on this change.

Change subject: WIP - load from classpath
......................................................................


Patch Set 2:

BAD Compatibility Tests Started https://asterix-jenkins.ics.uci.edu/job/asterixbad-compat/93/

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/1400
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I36a3fca2df9134423fe7590be260cfc3856c7f01
Gerrit-PatchSet: 2
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-HasComments: No

Change in asterixdb[master]: WIP - load from classpath

Posted by "Jenkins (Code Review)" <do...@asterixdb.incubator.apache.org>.
Jenkins has posted comments on this change.

Change subject: WIP - load from classpath
......................................................................


Patch Set 1:

Build Started https://asterix-jenkins.ics.uci.edu/job/asterix-gerrit-notopic/3627/

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/1400
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I36a3fca2df9134423fe7590be260cfc3856c7f01
Gerrit-PatchSet: 1
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-HasComments: No

Change in asterixdb[master]: WIP - load from classpath

Posted by "Till Westmann (Code Review)" <do...@asterixdb.incubator.apache.org>.
Till Westmann has posted comments on this change.

Change subject: WIP - load from classpath
......................................................................


Patch Set 2:

(1 comment)

https://asterix-gerrit.ics.uci.edu/#/c/1400/2/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/provider/ParserFactoryProvider.java
File asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/provider/ParserFactoryProvider.java:

Line 47:     private static final String RESOURCE = "META-INF/services/org.apache.asterix.external.api.IDataParserFactory";
> Should we make this RESOURCE as a directory? As I see from the later initFa
There are different approaches. The only restriction we have is, that we can only have of configuration file with a specific name per jar. 

Now we can either create a lot of jars for the individual classes or create a mechanism to cover several classes in one jar. I'm not sure if directories would work well as I don't know what URL.openStream() does for directories, but this is clearly a solvable problem. 

One option to handle multiple implementations for one interface in a single jar would be to 

a) have a single file per interface in the jar (.e.g o.a.a.e.a.IDataParserFactory) that contains a class name of an implementation on each line (e.g. o.a.a.e.p.f.DelimitedDataParserFactory) and to 

b) extend the interface to provide more information about the implementation (e.g. add a supportedFormats() method to IDataParserFactory).

That way we could keep the configuration files simple and allow for an arbitrary number of implementations of a arbitrary number of interfaces in a single jar.


-- 
To view, visit https://asterix-gerrit.ics.uci.edu/1400
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I36a3fca2df9134423fe7590be260cfc3856c7f01
Gerrit-PatchSet: 2
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-Reviewer: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Xikui Wang <xk...@gmail.com>
Gerrit-HasComments: Yes