前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊maxwell的Scripting

聊聊maxwell的Scripting

作者头像
code4it
发布2020-05-16 14:30:11
4560
发布2020-05-16 14:30:11
举报
文章被收录于专栏:码匠的流水账

本文主要研究一下maxwell的Scripting

Scripting

maxwell-1.25.1/src/main/java/com/zendesk/maxwell/scripting/Scripting.java

代码语言:javascript
复制
public class Scripting {
    static final Logger LOGGER = LoggerFactory.getLogger(Scripting.class);

    private final ScriptObjectMirror processRowFunc, processHeartbeatFunc, processDDLFunc;

    private ScriptObjectMirror getFunc(ScriptEngine engine, String fName, String filename) {
        ScriptObjectMirror f = (ScriptObjectMirror) engine.get(fName);
        if ( f == null )
            return null;
        else if ( !f.isFunction() ) {
            throw new RuntimeException("Expected " + fName + " to be a function!");
        } else {
            LOGGER.info("using function " + fName + " from " + filename);
        }
        return f;
    }

    public Scripting(String filename) throws IOException, ScriptException, NoSuchMethodException {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("nashorn");

        String externJS = new String(Files.readAllBytes(Paths.get(filename)));
        engine.put("logger", LOGGER);
        engine.eval(externJS);

        processRowFunc = getFunc(engine, "process_row", filename);
        processHeartbeatFunc = getFunc(engine, "process_heartbeat", filename);
        processDDLFunc = getFunc(engine, "process_ddl", filename);

        if ( processRowFunc == null && processHeartbeatFunc == null && processDDLFunc == null )
            LOGGER.warn("expected " + filename + " to define at least one of: process_row,process_heartbeat,process_ddl");
    }

    public void invoke(RowMap row) {
        if ( row instanceof HeartbeatRowMap && processHeartbeatFunc != null )
            processHeartbeatFunc.call(null, new WrappedHeartbeatMap((HeartbeatRowMap) row));
        else if ( row instanceof DDLMap && processDDLFunc != null )
            processDDLFunc.call(null, new WrappedDDLMap((DDLMap) row));
        else if ( row instanceof RowMap && processRowFunc != null )
            processRowFunc.call(null, new WrappedRowMap(row));
    }

    private static ThreadLocal<ScriptEngine> stringifyEngineThreadLocal = ThreadLocal.withInitial(() -> {
        ScriptEngineManager manager = new ScriptEngineManager();
        return manager.getEngineByName("nashorn");
    });

    public static String stringify(ScriptObjectMirror mirror) throws ScriptException {
        ScriptObjectMirror json = (ScriptObjectMirror) stringifyEngineThreadLocal.get().eval("JSON");
        return (String) json.callMember("stringify", mirror);
    }
}
  • Scripting定义了processRowFunc、processHeartbeatFunc、processDDLFunc属性;其构造器接收filename参数,它创建ScriptEngineManager,然后获取名为nashorn的ScriptEngine,之后通过filename获取externJS,并执行engine.eval(externJS),之后通过getFunc方法初始化化processRowFunc、processHeartbeatFunc、processDDLFunc;其getFunc方法通过engine.get(fName)获取ScriptObjectMirror;其invoke方法针对HeartbeatRowMap执行processHeartbeatFunc.call,针对DDLMap执行processDDLFunc.call,针对RowMap执行processRowFunc.call

MaxwellConfig

maxwell-1.25.1/src/main/java/com/zendesk/maxwell/MaxwellConfig.java

代码语言:javascript
复制
public class MaxwellConfig extends AbstractConfig {
    static final Logger LOGGER = LoggerFactory.getLogger(MaxwellConfig.class);

    //......

    public String javascriptFile;
    public Scripting scripting;

    //......

    public void validate() {
        validatePartitionBy();
        validateFilter();

        //......

        if ( this.javascriptFile != null ) {
            try {
                this.scripting = new Scripting(this.javascriptFile);
            } catch ( Exception e ) {
                LOGGER.error("Error setting up javascript: ", e);
                System.exit(1);
            }
        }
    }

    //......

}
  • MaxwellConfig的validate方法在javascriptFile不为null的时候会创建Scripting,如果出现异常则执行System.exit(1)

小结

Scripting定义了processRowFunc、processHeartbeatFunc、processDDLFunc属性;其构造器接收filename参数,它创建ScriptEngineManager,然后获取名为nashorn的ScriptEngine,之后通过filename获取externJS,并执行engine.eval(externJS),之后通过getFunc方法初始化化processRowFunc、processHeartbeatFunc、processDDLFunc;其getFunc方法通过engine.get(fName)获取ScriptObjectMirror;其invoke方法针对HeartbeatRowMap执行processHeartbeatFunc.call,针对DDLMap执行processDDLFunc.call,针对RowMap执行processRowFunc.call

doc

  • Scripting
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-05-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Scripting
  • MaxwellConfig
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档