Search in sources :

Example 1 with Transformer

use of com.alibaba.datax.transformer.Transformer in project DataX by alibaba.

the class TransformerRegistry method loadTransformer.

public static void loadTransformer(String each) {
    String transformerPath = CoreConstant.DATAX_STORAGE_TRANSFORMER_HOME + File.separator + each;
    Configuration transformerConfiguration;
    try {
        transformerConfiguration = loadTransFormerConfig(transformerPath);
    } catch (Exception e) {
        LOG.error(String.format("skip transformer(%s),load transformer.json error, path = %s, ", each, transformerPath), e);
        return;
    }
    String className = transformerConfiguration.getString("class");
    if (StringUtils.isEmpty(className)) {
        LOG.error(String.format("skip transformer(%s),class not config, path = %s, config = %s", each, transformerPath, transformerConfiguration.beautify()));
        return;
    }
    String funName = transformerConfiguration.getString("name");
    if (!each.equals(funName)) {
        LOG.warn(String.format("transformer(%s) name not match transformer.json config name[%s], will ignore json's name, path = %s, config = %s", each, funName, transformerPath, transformerConfiguration.beautify()));
    }
    JarLoader jarLoader = new JarLoader(new String[] { transformerPath });
    try {
        Class<?> transformerClass = jarLoader.loadClass(className);
        Object transformer = transformerClass.newInstance();
        if (ComplexTransformer.class.isAssignableFrom(transformer.getClass())) {
            ((ComplexTransformer) transformer).setTransformerName(each);
            registComplexTransformer((ComplexTransformer) transformer, jarLoader, false);
        } else if (Transformer.class.isAssignableFrom(transformer.getClass())) {
            ((Transformer) transformer).setTransformerName(each);
            registTransformer((Transformer) transformer, jarLoader, false);
        } else {
            LOG.error(String.format("load Transformer class(%s) error, path = %s", className, transformerPath));
        }
    } catch (Exception e) {
        //错误funciton跳过
        LOG.error(String.format("skip transformer(%s),load Transformer class error, path = %s ", each, transformerPath), e);
    }
}
Also used : ComplexTransformer(com.alibaba.datax.transformer.ComplexTransformer) Transformer(com.alibaba.datax.transformer.Transformer) Configuration(com.alibaba.datax.common.util.Configuration) JarLoader(com.alibaba.datax.core.util.container.JarLoader) ComplexTransformer(com.alibaba.datax.transformer.ComplexTransformer) DataXException(com.alibaba.datax.common.exception.DataXException)

Example 2 with Transformer

use of com.alibaba.datax.transformer.Transformer in project DataX by alibaba.

the class GroovyTransformer method initGroovyTransformer.

private void initGroovyTransformer(String code, List<String> extraPackage) {
    GroovyClassLoader loader = new GroovyClassLoader(GroovyTransformer.class.getClassLoader());
    String groovyRule = getGroovyRule(code, extraPackage);
    Class groovyClass;
    try {
        groovyClass = loader.parseClass(groovyRule);
    } catch (CompilationFailedException cfe) {
        throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_GROOVY_INIT_EXCEPTION, cfe);
    }
    try {
        Object t = groovyClass.newInstance();
        if (!(t instanceof Transformer)) {
            throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_GROOVY_INIT_EXCEPTION, "datax bug! contact askdatax");
        }
        this.groovyTransformer = (Transformer) t;
    } catch (Throwable ex) {
        throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_GROOVY_INIT_EXCEPTION, ex);
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) Transformer(com.alibaba.datax.transformer.Transformer) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException)

Aggregations

Transformer (com.alibaba.datax.transformer.Transformer)2 DataXException (com.alibaba.datax.common.exception.DataXException)1 Configuration (com.alibaba.datax.common.util.Configuration)1 JarLoader (com.alibaba.datax.core.util.container.JarLoader)1 ComplexTransformer (com.alibaba.datax.transformer.ComplexTransformer)1 GroovyClassLoader (groovy.lang.GroovyClassLoader)1 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)1