2014-04-01 96 views
0

我想從postgresql數據庫導出數據到MongoDB.I已經成功創建JSON格式的字符串,當我將這個json存儲在mongoDB集合中時,只有第一個條目被存儲。 這裏是我的代碼: 公共類jsonTobson {如何使用java代碼將數據從Postgresql導出到mongoDB?

public static void main(String[] args) { 

    Connection con = null; 
    Statement st = null; 

    try{ 
     Class.forName("org.postgresql.Driver"); 
     con = DriverManager.getConnection("jdbc:postgresql://localhost:5544/ddc", "postgres", "aman"); 
     st = con.createStatement(); 

     String sql = "select row_to_json(judge_info) FROM dp.judge_info order by judge_idno"; 

     ResultSet rs = st.executeQuery(sql); 
     StringBuilder builder = new StringBuilder(); 
     int columnCount = rs.getMetaData().getColumnCount(); 

     while (rs.next()) { 
      for (int i = 0; i < columnCount;) { 
       builder.append(rs.getString(i + 1)); 
       if (++i < columnCount) builder.append(","); 
      } 
      builder.append("\r\n"); 
     } 
     String resultSetAsString = builder.toString(); 



     MongoClient mongoClient = new MongoClient("localhost" , 27017); 
     DB db = mongoClient.getDB("mongoTest"); 
     DBCollection coll = db.getCollection("newTable"); 
     Set<String> colls = db.getCollectionNames(); 

     for (String s : colls) { 
      System.out.println(s); 
     } 


    DBObject dbObject = (DBObject)JSON.parse(resultSetAsString); 

    coll.insert(dbObject, WriteConcern.NORMAL);      
     DBCursor cursorDocJSON = coll.find(); 
     while (cursorDocJSON.hasNext()) { 
      System.out.println(cursorDocJSON.next()); 
     }  

      rs.close(); 
      st.close(); 
      con.close(); 
     } catch (Exception e) 
     { 
      e.printStackTrace(); 
      System.err.println(e.getClass().getName()+": "+e.getMessage()); 
      System.exit(0); 
     } finally { 
     } 


    } 


} 
+0

它看起來並不像你正在構建有效的對象。沒有構建數組來插入多行,並且/或者您沒有循環遍歷結果以將每行插入爲文檔。 – WiredPrairie

+0

你可以發佈String resultSetAsString = builder.toString()的輸出嗎? – hellboy

回答

0
public static void main(String[] args) { 

Connection con = null; 
Statement st = null; 

try{ 
    Class.forName("org.postgresql.Driver"); 
    con = DriverManager.getConnection("jdbc:postgresql://localhost:5544/ddc", "postgres", "aman"); 
    st = con.createStatement(); 

    String sql = "select row_to_json(judge_info) FROM dp.judge_info order by judge_idno"; 

    ResultSet rs = st.executeQuery(sql); 

    MongoClient mongoClient=null; 
     try { 
      mongoClient = new MongoClient("localhost", 27017); 
     } catch (UnknownHostException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

    DB db = mongoClient.getDB("mongoTest"); 
    System.out.println("Connect to database successfully"); 

    while (rs.next()) { 
      DBCollection coll = db.getCollection("newTable"); 
      System.out.println("Collection selected successfully"); 

      BasicDBObject doc = new BasicDBObject("data", rs.getString(1)); 

      coll.insert(doc); 
      } 
    } catch (Exception e) 
    { 
     e.printStackTrace(); 
     System.err.println(e.getClass().getName()+": "+e.getMessage()); 
     System.exit(0); 
    } finally { 
    } 
} 

}

+0

此代碼正在添加所有文檔。 –

相關問題