如何使用java将rdbms数据类型转换为nosql数据类型
我有来自mysql的源数据类型和来自mongodb的目标数据类型 如果源数据类型是varchar,我如何转换成字符串并使用convertToTargetDataType方法将该值设置为文档。有人能帮我解决这个问题吗如何使用java将rdbms数据类型转换为nosql数据类型,java,mysql,mongodb,Java,Mysql,Mongodb,我有来自mysql的源数据类型和来自mongodb的目标数据类型 如果源数据类型是varchar,我如何转换成字符串并使用convertToTargetDataType方法将该值设置为文档。有人能帮我解决这个问题吗 private Document prepareDoc(ResultSet rs, List<Columns> columns) throws SQLException { Document document = new Document();
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;
}
private Document prepareDoc(结果集、列表列)抛出SQLException{
文档=新文档();
对于(列:列){
字符串sourceDataType=col.getSourceDataType();
字符串targetDataType=col.getTargetDataType();
字符串键=col.getColumnName();
字符串值=null;
开关(sourceDataType){
案例“varchar”:{
//字符串值=rs.getString(键);
put(key,convertToTargetDataType(targetDataType,value));
打破
}
案例“int”:{
//double value=rs.getDouble(键);
put(key,convertToTargetDataType(targetDataType,value));
打破
}
案件“日期”:{
//日期值=rs.getDate(键);
put(key,convertToTargetDataType(targetDataType,value));
打破
}
}
}
归还文件;
}
公共对象convertToTargetDataType(字符串targetDataType,字符串值){
//如果目标数据类型=值数据类型,则返回该数据类型
返回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;
}
private Document prepareDoc(结果集、列表列)抛出SQLException{
文档=新文档();
对于(列:列){
字符串sourceDataType=col.getSourceDataType();
字符串targetDataType=col.getTargetDataType();
字符串键=col.getColumnName();
//将根据驱动程序返回的实际JAVA类型使用正确的JSON类型
document.put(key,rs.getObject(key));
}
归还文件;
}
注意:检查驱动程序为RS中各自的SQL类型提供的具体类很重要。行为可能会有所不同,因此请确保特定RDBMS的JDBC驱动程序为您提供了一些可用的内容(特别是如果您使用诸如Blob等不可靠的类型)。
但一般来说,这适用于最常见的类型。不,我有一个json文件形式的预定义模式。我有源数据类型和目标数据类型。我从该文件读取源数据类型和目标数据类型,并检查这两种数据类型。