[英]How to convert rdbms datatypes to nosql datatypes using java
我有来自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驱动程序为您提供了可用的功能(尤其是如果您使用斑点类型(例如斑点)等)。 但总的来说,这应该适用于最常见的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.