Search in sources :

Example 1 with IDataParserFactory

use of org.apache.asterix.external.api.IDataParserFactory in project asterixdb by apache.

the class ExternalDataUtils method createExternalParserFactory.

public static IDataParserFactory createExternalParserFactory(ILibraryManager libraryManager, String dataverse, String parserFactoryName) throws AsterixException {
    try {
        String library = parserFactoryName.substring(0, parserFactoryName.indexOf(ExternalDataConstants.EXTERNAL_LIBRARY_SEPARATOR));
        ClassLoader classLoader = libraryManager.getLibraryClassLoader(dataverse, library);
        return (IDataParserFactory) classLoader.loadClass(parserFactoryName.substring(parserFactoryName.indexOf(ExternalDataConstants.EXTERNAL_LIBRARY_SEPARATOR) + 1)).newInstance();
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        throw new AsterixException("Failed to create an external parser factory", e);
    }
}
Also used : AsterixException(org.apache.asterix.common.exceptions.AsterixException) IDataParserFactory(org.apache.asterix.external.api.IDataParserFactory)

Example 2 with IDataParserFactory

use of org.apache.asterix.external.api.IDataParserFactory in project asterixdb by apache.

the class ParserFactoryProvider method getDataParserFactory.

public static IDataParserFactory getDataParserFactory(ILibraryManager libraryManager, Map<String, String> configuration) throws AsterixException {
    IDataParserFactory parserFactory;
    String parserFactoryName = configuration.get(ExternalDataConstants.KEY_DATA_PARSER);
    if ((parserFactoryName != null) && ExternalDataUtils.isExternal(parserFactoryName)) {
        return ExternalDataUtils.createExternalParserFactory(libraryManager, ExternalDataUtils.getDataverse(configuration), parserFactoryName);
    } else {
        String parserFactoryKey = ExternalDataUtils.getRecordFormat(configuration);
        if (parserFactoryKey == null) {
            parserFactoryKey = configuration.get(ExternalDataConstants.KEY_PARSER_FACTORY);
        }
        parserFactory = ParserFactoryProvider.getDataParserFactory(parserFactoryKey);
    }
    return parserFactory;
}
Also used : IDataParserFactory(org.apache.asterix.external.api.IDataParserFactory)

Example 3 with IDataParserFactory

use of org.apache.asterix.external.api.IDataParserFactory in project asterixdb by apache.

the class ParserFactoryProvider method initFactories.

protected static Map<String, Class> initFactories() throws AsterixException {
    Map<String, Class> factories = new HashMap<>();
    ClassLoader cl = ParserFactoryProvider.class.getClassLoader();
    try {
        Enumeration<URL> urls = cl.getResources(RESOURCE);
        for (URL url : Collections.list(urls)) {
            List<String> classNames;
            try (InputStream is = url.openStream()) {
                classNames = IOUtils.readLines(is, StandardCharsets.UTF_8);
            }
            for (String className : classNames) {
                if (className.startsWith("#")) {
                    continue;
                }
                final Class<?> clazz = Class.forName(className);
                List<String> formats = ((IDataParserFactory) clazz.newInstance()).getParserFormats();
                for (String format : formats) {
                    if (factories.containsKey(format)) {
                        throw new AsterixException("Duplicate format " + format);
                    }
                    factories.put(format, clazz);
                }
            }
        }
    } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        throw new AsterixException(e);
    }
    return factories;
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) IDataParserFactory(org.apache.asterix.external.api.IDataParserFactory) IOException(java.io.IOException) URL(java.net.URL) AsterixException(org.apache.asterix.common.exceptions.AsterixException)

Aggregations

IDataParserFactory (org.apache.asterix.external.api.IDataParserFactory)3 AsterixException (org.apache.asterix.common.exceptions.AsterixException)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1