gpt4 book ai didi

java - 如何使用java将rdbms数据类型转换为nosql数据类型

转载 作者:行者123 更新时间:2023-11-29 21:16:07 25 4
gpt4 key购买 nike

我有来自 mysql 的源数据类型和来自 mongodb 的目标数据类型。

如果源数据类型是varchar,我如何转换为字符串并使用convertToTargetDataType方法将该值设置为文档。任何人都可以帮我解决这个问题...

private Document prepareDoc(ResultSet rs, List<Columns> columns) throws SQLException {
Document document = new Document();
for (Columns col : columns) {
String sourceDataType = col.getSourceDataType();
String targetDataType = col.getTargetDataType();

String key = col.getColumnName();
String value = null;

switch (sourceDataType) {
case "varchar": {
//String value = rs.getString(key);
document.put(key, convertToTargetDataType(targetDataType, value));
break;
}
case "int": {
//double value = rs.getDouble(key);
document.put(key, convertToTargetDataType(targetDataType, value));
break;
}

case "date": {
//Date value = rs.getDate(key);
document.put(key, convertToTargetDataType(targetDataType, value));
break;
}

}
}
return document;
}

public Object convertToTargetDataType(String targetDataType, String value) {
//if target datatype = value datatype ,return that datatype

return null;
}

最佳答案

假设您打算使用 MongoDB,值得注意的是您不需要在插入之前使用某种预定义类型。

由于您的对象是动态的并且您事先不知道架构,因此您可以简单地将结果集中的对象附加到文档中:

private Document prepareDoc(ResultSet rs, List<Columns> columns) throws SQLException {
Document document = new Document();
for (Columns col : columns) {
String sourceDataType = col.getSourceDataType();
String targetDataType = col.getTargetDataType();

String key = col.getColumnName();

//The correct JSON type will be used based on the ACTUAL JAVA TYPE RETURNED BY THE DRIVER
document.put(key, rs.getObject(key));

}
return document;
}

注意:检查驱动程序为您在 RS 中各自的 SQL 类型提供的具体类非常重要。行为可能有所不同,因此请确保您的特定 RDBMS 的 JDBC 驱动程序为您提供了可用的东西(特别是如果您使用诸如 blob 等危险类型)。但一般来说,这应该适用于最常见的类型。

关于java - 如何使用java将rdbms数据类型转换为nosql数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35938166/

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