遠端從Mongodb 資料庫中匯出為CSV檔案
阿新 • • 發佈:2019-02-09
Java 連線Mongodb 資料庫並且從資料庫匯出資料為CSV檔案。
需要匯入三個包:
bson-3.4.3.jarmongo-java-driver-3.4.3.jar
mongodb-driver-core-3.4.3.jar
當然版本可以改變。可以從http://mongodb.github.io/mongo-java-driver/ 這裡下載
mongodb.properties:
url=localhost
port=27017
連線資料庫的工具類:
package com.qitech.util; import java.io.IOException; import java.io.InputStream; import java.net.UnknownHostException; import java.util.Properties; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.Mongo; import com.mongodb.MongoException; public class MongoDbConn { private static String url; private static int port; // 初始化載入 static{ //載入 Properties p = new Properties(); InputStream input = MongoDbConn.class.getClassLoader().getResourceAsStream("mongodb.properties"); try { p.load(input); url = p.getProperty("url"); port = Integer.valueOf(p.getProperty("port")); } catch (IOException e) { e.printStackTrace(); }finally{ if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 獲取連線mongodb * @Description: * @param @return * @return DBCollection 返回型別 */ public static DBCollection getConn(String db,String table) throws UnknownHostException{ DBCollection conn = null; try { @SuppressWarnings("deprecation") Mongo m = new Mongo(url,port); DB d = m.getDB(db); conn = d.getCollection(table); } catch (MongoException e) { e.printStackTrace(); } return conn; } /** * 關閉連線 * @Description: * @param @param m * @return void 返回型別 */ public static void getClose(Mongo m){ m.close(); } }
匯出CSV 檔案的工具類:
package com.qitech.util; import java.io.IOException; import java.util.ArrayList; import com.csvreader.CsvWriter; import com.mongodb.DBCursor; import com.mongodb.DBObject; public class WriteCsvUtil { public static void Csv(DBCursor cursor,String fileName) throws IOException{ CsvWriter csvWriter = new CsvWriter(("F:/"+fileName)); //獲取標題 DBObject ob = cursor.toArray().get(0); ArrayList<String> title = new ArrayList<String>(); for(String key:ob.keySet()){ title.add(key); } String[] buffer = new String[100]; String[] buffer1 = new String[10000]; for(int i = 0;i< (cursor.count() + 1);i++){ int k=0; //建立標題行 if(i == 0){ for(int j=0;j<title.size();j++){ buffer[k] = title.get(j); k++; } continue; } if(i == 1){ csvWriter.writeRecord(buffer); } //寫資料 DBObject obj = null; for(int j = 0;j < title.size();j++){ int k1=0; obj = cursor.toArray().get(i-1); for(String key : obj.keySet()){ if (key.equals(title.get(j))) { buffer1[k1] = obj.get(key).toString(); } k1++; } } csvWriter.writeRecord(buffer1); } try { System.out.println("aaa"); } catch (RuntimeException r) { // TODO Auto-generated catch block new RuntimeException("匯出CSV 檔案出錯"); }finally { System.out.println("匯出檔案成功"); //csvWriter.flush(); csvWriter.close(); } } }
測試類:
public static void main(String[] args) throws IOException { DBCollection conn = null; try { conn = MongoDbConn.getConn("local", "student"); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } DBCursor cursor = conn.find(); WriteCsvUtil.Csv(cursor, "1.csv"); }
匯出截圖: