我试图将我的维度从 SQL 服务器导入 MongoDB,我可以这样做,但是我必须为每个维度编写代码,这有点费时。部分代码:
Mongo mongo = new Mongo();
DB db = mongo.getDB("Nelly");
DBCollection facil = db.getCollection("Facilities");
while (r.next()) {
BasicDBObject facObj = new BasicDBObject();
String faccKey = (r.getString("dim_Facilities_dKey"));
if (faccKey != null && !faccKey.trim().isEmpty()
&& faccKey.length() != 0)
facObj.append("dKey", faccKey);
facil.insert(facObj);
但是我希望能够通过某种循环和列表更快地完成它,但我无法弄清楚如何解决它,我已经尝试了一切!
while(r.next()){
for (int i = 1; i < columnCount + 1; i++ ) {
HashMap<String, ArrayList<String>> hashmap = new HashMap<String, ArrayList<String>>();
ArrayList<String> arr=new ArrayList<String>();
arr.add(r.getString(i));
BasicDBObject facObj = new BasicDBObject();
String name = rsmd.getColumnName(i);
int varde=1;
String data =r.getString(varde);
varde++;
arr.add(data);
hashmap.put(name, arr);
facObj.append(name, hashmap.values().toString());
facil.insert(facObj);
有人可以给我关于如何继续前进的建议吗?
提前致谢
我解决了它:
ResultSet r = s.executeQuery();
ResultSetMetaData rsmd = s.getMetaData();
int columnCount = rsmd.getColumnCount();
while (r.next()) {
BasicDBObject facObj = new BasicDBObject();
for (int i = 1; i < columnCount + 1; i++) {
String name = rsmd.getColumnName(i);
String faccKey = (r.getString(i));
if (faccKey != null && !faccKey.trim().isEmpty() && faccKey.length()!= 0)
facObj.append(name, faccKey);
}
facil.insert(facObj);}