简体   繁体   English

从CSV加载Liquibase数据

[英]Liquibase loading data from csv

I want to load an entire column of my PostgreSql table with data from csv file but when I do that I get an exception saying that the primary key of my table should not be null. 我想用csv文件中的数据加载PostgreSql表的整个列,但是当我这样做时,出现一个异常,说表的主键不应为null。 It looks like Liquibase is creating new rows to insert the data. 看来Liquibase正在创建新行以插入数据。 Is there a way to load the data in existing rows ? 有没有一种方法可以将数据加载到现有行中?

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("");

There is a class thats called: LoadUpdateDataChange 有一个叫做的类: LoadUpdateDataChange

The description says: 描述说:

Loads or updates data from a CSV file into an existing table. 将数据从CSV文件加载或更新到现有表中。 Differs from loadData by issuing a SQL batch that checks for the existence of a record. 通过发出检查记录是否存在的SQL批处理,不同于loadData。 If found, the record is UPDATEd, else the record is INSERTed. 如果找到,则记录为UPDATE,否则记录为INSERTed。 Also, generates DELETE statements for a rollback. 另外,生成DELETE语句以进行回滚。

Looks like it should do what you are looking for (I have not used this myself though). 看起来它应该可以满足您的要求(尽管我自己没有使用过)。

To update records, LoadUpdateDataChange is not working for me. 要更新记录,LoadUpdateDataChange对我不起作用。 Its always trying to insert new records.It won't update existing records. 它总是尝试插入新记录,不会更新现有记录。

If want to update existing records, you should use like below. 如果要更新现有记录,则应使用如下所示。

<update tableName="someTable">

   <column name="update_column_name" value="updated value" />

   <where> primaryKey = condition</where>

</update>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM