有一个数据文件,从oralce导出的文件,table1.csv,文件开头如下,注意要保留列名在文件开头:
" ","USERID","NAME","PASSWORD","EMAIL","TYPE","CORPNAME","COMMADDRESS","COMMCALL","ZIPCODE","ORGCODE","REGISTCODE","REGISTADDRESS","LEGALNAME","FAX","CONTACT","USERNAME","IDENTITY","MOBILE","INDUSTRY","INTEGRITY","CREDIT","ORGID","ZDYHNAME","DEPARTMENT","CREATEDATE","REMARK","ISVERIFY","VERIFYMESSAGE","ISSTATE","ORGLICENSE","ORGORGNIZATION","SETTLESPAN","CREATETIME","YHNAME","ZJLX","ORG_ID"
"1","1163","zzbgmail","123456","[email protected]","3","jkdf","asdf","12312321","100000","","","","","","","zzb","110110197711110021","13811099999","销售","","","","","","2012/1/6 11:03:00","","","","1","","","","","","","""2","1204","zhangyi","111111","[email protected]","3","","","","","","","","","","","","","","","","","","","","2012/1/6 17:27:38","","","","0","","","","","","",""
首选,要把数据文件转成utf-8格式,否则导入mongdb会报错误。
然后,使用mongoimport 命令导入文件到mydb数据库,集合名(表名),仍然叫table1:
mongoimport -d mydb -c table1 --type csv --headerline --file D:\Table1.csv
看到如下输出:
connected to: 127.0.0.1
2014-05-06T11:04:18.293+0800 check 9 35
2014-05-06T11:04:18.323+0800 imported 34 objects
说明导入成功,而且添加了34行记录。
再用如下java程序查询导入的数据:
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import java.net.UnknownHostException;
public class getTable1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Mongo mongoClient;
try {
mongoClient = new Mongo("127.0.0.1", 27017);
DB db = mongoClient.getDB("mydb");
DBCollection coll = db.getCollection("table1"); // 按名字取文档
// 游标查询
DBCursor cursor = coll.find();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
mongoClient.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MongoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出如下结果(部分结果):
{ "_id" : { "$oid" : "536850fa03740c23ae783716"} , " " : 2 , "USERID" : 1204 , "NAME" : "zhangyi" , "PASSWORD" : 111111 , "EMAIL" : "[email protected]" , "TYPE" : 3 , "CORPNAME" : "" , "COMMADDRESS" : "" , "COMMCALL" : "" , "ZIPCODE" : "" , "ORGCODE" : "" , "REGISTCODE" : "" , "REGISTADDRESS" : "" , "LEGALNAME" : "" , "FAX" : "" , "CONTACT" : "" , "USERNAME" : "" , "IDENTITY" : "" , "MOBILE" : "" , "INDUSTRY" : "" , "INTEGRITY" : "" , "CREDIT" : "" , "ORGID" : "" , "ZDYHNAME" : "" , "DEPARTMENT" : "" , "CREATEDATE" : "2012/1/6 17:27:38" , "REMARK" : "" , "ISVERIFY" : "" , "VERIFYMESSAGE" : "" , "ISSTATE" : 0 , "ORGLICENSE" : "" , "ORGORGNIZATION" : "" , "SETTLESPAN" : "" , "CREATETIME" : "" , "YHNAME" : "" , "ZJLX" : "" , "ORG_ID" : ""}
{ "_id" : { "$oid" : "536850fa03740c23ae783717"} , " " : 5 , "USERID" : 281 , "NAME" : "ssss" , "PASSWORD" : 111111 , "EMAIL" : "[email protected]" , "TYPE" : 1 , "CORPNAME" : "" , "COMMADDRESS" : "" , "COMMCALL" : "" , "ZIPCODE" : "" , "ORGCODE" : "" , "REGISTCODE" : "" , "REGISTADDRESS" : "" , "LEGALNAME" : "" , "FAX" : "" , "CONTACT" : "" , "USERNAME" : "" , "IDENTITY" : "" , "MOBILE" : "" , "INDUSTRY" : "" , "INTEGRITY" : "" , "CREDIT" : "" , "ORGID" : "" , "ZDYHNAME" : "" , "DEPARTMENT" : "" , "CREATEDATE" : "" , "REMARK" : "" , "ISVERIFY" : 0 , "VERIFYMESSAGE" : "" , "ISSTATE" : 0 , "ORGLICENSE" : "" , "ORGORGNIZATION" : "" , "SETTLESPAN" : "" , "CREATETIME" : "" , "YHNAME" : "" , "ZJLX" : "" , "ORG_ID" : ""}
{ "_id" : { "$oid" : "536850fa03740c23ae783718"} , " " : 8 , "USERID" : 229 , "NAME" : "llz5" , "PASSWORD" : 111111 , "EMAIL" : "" , "TYPE" : 4 , "CORPNAME" : "" , "COMMADDRESS" : "" , "COMMCALL" : "" , "ZIPCODE" : "" , "ORGCODE" : "" , "REGISTCODE" : "" , "REGISTADDRESS" : "" , "LEGALNAME" : "" , "FAX" : "" , "CONTACT" : "" , "USERNAME" : "" , "IDENTITY" : "" , "MOBILE" : "" , "INDUSTRY" : "" , "INTEGRITY" : "" , "CREDIT" : "" , "ORGID" : 45 , "ZDYHNAME" : "qq" , "DEPARTMENT" : 11 , "CREATEDATE" : "2011/3/29 17:47:00" , "REMARK" : "" , "ISVERIFY" : "" , "VERIFYMESSAGE" : "" , "ISSTATE" : "" , "ORGLICENSE" : "" , "ORGORGNIZATION" : "" , "SETTLESPAN" : "" , "CREATETIME" : "" , "YHNAME" : "" , "ZJLX" : "" , "ORG_ID" : ""}
说明导入成功。