如何使用DAO插入雪花变体字段?

时间:2019-06-27 05:02:14

标签: dao jdbi snowflake

我有以下代码:

@RegisterMapper(MyEntity.ResultMapper.class)
@UseStringTemplate3StatementLocator
public interface MyDao {

    @Transaction(TransactionIsolationLevel.SERIALIZABLE)
    @SqlBatch("INSERT INTO mySchema.myTable (" +
        " id, entity_type, entity_id, flags " +
        " ) VALUES " +
        "(" +
        " :stepId , :entityType , :entityId,parse_json(:flags) " +
        ")")
    @BatchChunkSize(500)
    Object create( @BindBean List<MyEntity> entities );
}

如您所见,我正在使用此DAO将实体列表批量插入到Snowflake表中。

问题是我无法插入标志列,这是一个变体。我尝试过to_variant(:flags),目前已经尝试过parse_json(:flags),但JDBI不断抛出以下错误:

net.snowflake.client.jdbc.SnowflakeSQLException: SQL 
compilation error:
Invalid expression [PARSE_JSON(?)] in VALUES clause 
[statement:"INSERT INTO mySchema.myTable ( id, entity_type, 
entity_id, flags  ) VALUES ( :stepId , :entityType , :entityId,
parse_json(:flags) )", located:"null", rewritten:"null",
arguments:{ positional:{}, named:{timeStamp:'null', 
entityType:MYENTITY,
flags:'{"client":"myClient","flow":"myFlow"}',stepId:null, 
entityId:'189643357241513', class:class myOrg.MyEntity}, finder:[]}]

我应该如何在flags列中传递值?有人尝试过吗? MyEntity中的flags字段处于我的控制范围内,我可以将其保留为POJO或String,以帮助解决此问题的方式为准。

1 个答案:

答案 0 :(得分:0)

请参见黄建生的the comment以获得答案:

INSERT INTO T SELECT parse_json(:flag);