[Flink开发总结]反射在flink中的使用.

flink代码中使用反射的目的:

1.获取pojo的所有属性字段,用于拼接sql语句.

反射常用三种方式:

 获取class对象的三种方法:  
 1.class.forName(...)   
 2.类名.class   
 3.对象.getClass

代码案例:

 Field[] fields = tClass.getDeclaredFields();
        //拼接属性名
        String cs = "";
        String wh = "";
        for (Field field : fields) {
            //如果这个field有NoSink这个注解,则不要拼接到sql中
            NoSink noSink = field.getAnnotation(NoSink.class);
            if (noSink == null) {
                cs += field.getName() + ",";
                wh += "?,";
            }
        }

        //insert into t(id,name,age)values(?,?,?)
        StringBuilder sql = new StringBuilder();
        sql
                .append("insert into ")
                .append(table)
                .append("(")
                //拼接字段
                .append(cs, 0, cs.length() - 1)
                .append(")values(")
                
log4j:WARN No appenders could be found for logger (org.apache.flink.api.java.ClosureCleaner). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make field private final byte[] java.lang.String.value accessible: module java.base does not "opens java.lang" to unnamed module @4883b407 at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354) at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297) at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178) at java.base/java.lang.reflect.Field.setAccessible(Field.java:172) at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:104) at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:126) at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:71) at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.clean(StreamExecutionEnvironment.java:1901) at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.addSource(StreamExecutionEnvironment.java:1614) at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.addSource(StreamExecutionEnvironment.java:1571) at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.socketTextStream(StreamExecutionEnvironment.java:1375) at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.socketTextStream(StreamExecutionEnvironment.java:1415) at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.socketTextStream(StreamExecutionEnvironment.java:1432) at org.example.FlinkSocketWindow.main(FlinkSocketWindow.java:30)
最新发布
06-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值