set null value to `NULL` in SqlWriter

This commit is contained in:
LuckyPickleZZ 2023-12-07 12:02:54 +08:00
parent 86b7935bb4
commit 1a48afdac7
2 changed files with 14 additions and 5 deletions

View File

@ -15,13 +15,15 @@ public class SqlWriter implements UnstructuredWriter {
private String quoteChar;
private String lineSeparator;
private String tableName;
private String nullFormat;
private StringBuilder insertPrefix;
public SqlWriter(Writer writer, String quoteChar, String tableName, String lineSeparator, List<String> columnNames) {
public SqlWriter(Writer writer, String quoteChar, String tableName, String lineSeparator, List<String> columnNames, String nullFormat) {
this.sqlWriter = writer;
this.quoteChar = quoteChar;
this.lineSeparator = lineSeparator;
this.tableName = tableName;
this.nullFormat = nullFormat;
buildInsertPrefix(columnNames);
}
@ -33,7 +35,12 @@ public class SqlWriter implements UnstructuredWriter {
}
StringBuilder sqlPatten = new StringBuilder(4096).append(insertPrefix);
sqlPatten.append(splitedRows.stream().map(e -> "'" + DataXCsvWriter.replace(e, "'", "''") + "'").collect(Collectors.joining(",")));
sqlPatten.append(splitedRows.stream().map(e -> {
if (nullFormat.equals(e)) {
return "NULL";
}
return "'" + DataXCsvWriter.replace(e, "'", "''") + "'";
}).collect(Collectors.joining(",")));
sqlPatten.append(");").append(lineSeparator);
this.sqlWriter.write(sqlPatten.toString());
}
@ -48,9 +55,10 @@ public class SqlWriter implements UnstructuredWriter {
sb.append(quoteChar).append(columnName).append(quoteChar);
}
int capacity = 16 + tableName.length() + sb.length();
int capacity = 18 + tableName.length() + sb.length();
this.insertPrefix = new StringBuilder(capacity);
this.insertPrefix.append("INSERT INTO ").append(tableName).append(" (").append(sb).append(")").append(" VALUES(");
this.insertPrefix
.append("INSERT INTO ").append("`").append(tableName).append("`").append(" (").append(sb).append(")").append(" VALUES(");
}
public void appendCommit() throws IOException {

View File

@ -283,7 +283,8 @@ public class UnstructuredStorageWriterUtil {
String lineSeparator = config.getString(Key.LINE_DELIMITER, IOUtils.LINE_SEPARATOR);
List<String> headers = config.getList(Key.HEADER, String.class);
Preconditions.checkArgument(CollectionUtils.isNotEmpty(headers), "column names are empty");
unstructuredWriter = new SqlWriter(writer, quoteChar, tableName, lineSeparator, headers);
String nullFormat = config.getString(Key.NULL_FORMAT, Constant.DEFAULT_NULL_FORMAT);
unstructuredWriter = new SqlWriter(writer, quoteChar, tableName, lineSeparator, headers, nullFormat);
}
return unstructuredWriter;