gpt4 book ai didi

java - Liquibase 从 csv 加载数据

转载 作者:行者123 更新时间:2023-12-02 13:33:10 26 4
gpt4 key购买 nike

我想用 csv 文件中的数据加载 PostgreSql 表的整列,但是当我这样做时,我收到一个异常,说我的表的主键不应该为空。看起来 Liquibase 正在创建新行来插入数据。有没有办法加载现有行中的数据?

DatabaseChangeLog dbChangeLog = new DatabaseChangeLog();
Liquibase liquibase = new Liquibase(dbChangeLog, new FileSystemResourceAccessor(), database);
ChangeSet loadChangeSet = new ChangeSet(id + "", "nasri", false, false, "", "", "", liquibase.getDatabaseChangeLog());

LoadDataChange loadDataChange = new LoadDataChange();
loadDataChange.setTableName(key);
loadDataChange.setChangeSet(loadChangeSet);
loadDataChange.setResourceAccessor(new FileSystemResourceAccessor());

String path = context.getBundle().getVersion() + "." + key + "." + columnKey + "." + targetFieldKey + ".csv";
loadDataChange.setFile(path);

loadDataChange.setSchemaName("public");

LoadDataColumnConfig columnConfig = new LoadDataColumnConfig();
columnConfig.setName(targetFieldKey);
columnConfig.setType("String");
loadDataChange.addColumn(columnConfig);
loadChangeSet.addChange(loadDataChange);
liquibase.getDatabaseChangeLog().addChangeSet(loadChangeSet);
liquibase.update("");

最佳答案

有一个类叫做:LoadUpdateDataChange

描述说:

Loads or updates data from a CSV file into an existing table. Differs from loadData by issuing a SQL batch that checks for the existence of a record. If found, the record is UPDATEd, else the record is INSERTed. Also, generates DELETE statements for a rollback.

看起来它应该可以满足您的要求(尽管我自己没有使用过)。

关于java - Liquibase 从 csv 加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43112219/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com