How to insert into a snowflake variant field using a DAO?

▼魔方 西西 提交于 2019-12-13 03:30:14

问题


I have the following code:

@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 );
}

As you can see, I am bulk inserting a list of entities into my Snowflake table using this DAO.

The issue is that I am unable to insert into the flags columns, which is a variant. I have tried to_variant(:flags) and currently parse_json(:flags), but the JDBI keeps throwing the following error:

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:[]}]

How should I pass the value in the flags column ? Has anyone attempted this before? The flags field in MyEntity is in my control, I can keep it as a POJO or a String, whichever helps me resolve this issue.


回答1:


See the comment by Jiansheng Huang for answer:

INSERT INTO T SELECT parse_json(:flag);


来源:https://stackoverflow.com/questions/56784193/how-to-insert-into-a-snowflake-variant-field-using-a-dao

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!