
com.huaweicloud.dws.client.op.Write Maven / Gradle / Ivy
package com.huaweicloud.dws.client.op;
import com.huaweicloud.dws.client.collector.ActionCollector;
import com.huaweicloud.dws.client.exception.DwsClientException;
import com.huaweicloud.dws.client.exception.InvalidException;
import com.huaweicloud.dws.client.model.ConflictStrategy;
import com.huaweicloud.dws.client.model.OperationType;
import com.huaweicloud.dws.client.model.TableSchema;
import com.huaweicloud.dws.client.model.WriteMode;
import com.huaweicloud.dws.client.util.AssertUtil;
/**
* @ProjectName: dws-connector
* @ClassName: Upsert
* @Description:
* @Date: 2023/1/14 20:03
* @Version: 1.0
*/
public class Write extends Operate {
public Write(TableSchema schema, ActionCollector collector) {
super(schema, collector, OperationType.WRITE);
}
/**
* 有主键 && 替换模式下,设置用户未设置的字段为null, 便于后续流程执行
*/
protected void fillRecord() {
TableSchema tableSchema = record.getTableSchema();
if (collector.getConfig().getTableConfig(tableSchema.getTableName()).getConflictStrategy() == ConflictStrategy.INSERT_OR_REPLACE
&& !record.getTableSchema().getPrimaryKeys().isEmpty()) {
for (String column : record.getTableSchema().getColumnNames()) {
if (record.isSet(column)) {
continue;
}
record.setValue(column, null, false);
}
}
}
protected void checkRecord() {
// update模式下必须主键全填充
TableSchema tableSchema = record.getTableSchema();
WriteMode writeMode = collector.getConfig().getTableConfig(tableSchema.getTableName()).getWriteMode();
if (WriteMode.updateWrites().contains(writeMode)) {
// update 需要检查主键是否全覆盖
for (String keyName : record.getTableSchema().getPrimaryKeyNames()) {
AssertUtil.isTrue(record.isSet(keyName), new InvalidException("update must set primary key " + keyName));
}
}
}
@Override
public void commit() throws DwsClientException {
checkRecord();
fillRecord();
super.commit();
}
/**
* 同步提交
*/
@Override
public void syncCommit() throws DwsClientException {
checkRecord();
fillRecord();
super.syncCommit();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy