我正在編寫一個應用程序,其中輸入的數據源是CSV格式。我需要在數據庫上執行CRUD操作。因爲我覺得它適合我的需要,所以我打算去MongoDB。我的問題是 1.如何將CSV數據存儲在MongoDB中? 2.我是否需要將CSV解析爲json? 3.我想在UI上顯示數據,那麼MongoDB能以json格式檢索數據嗎?帶CSV數據的MongoDB
請幫助我,因爲我是MongoDB的新手。
問候, 普拉迪普
我正在編寫一個應用程序,其中輸入的數據源是CSV格式。我需要在數據庫上執行CRUD操作。因爲我覺得它適合我的需要,所以我打算去MongoDB。我的問題是 1.如何將CSV數據存儲在MongoDB中? 2.我是否需要將CSV解析爲json? 3.我想在UI上顯示數據,那麼MongoDB能以json格式檢索數據嗎?帶CSV數據的MongoDB
請幫助我,因爲我是MongoDB的新手。
問候, 普拉迪普
直接在MongoDB中存儲的CSV作爲字符串將是一個非常糟糕的主意,因爲這將是不切實際的操作,並在該格式查詢數據,所以你會希望將其轉換到JSON(實際上是BSON)文檔,因爲您將它添加到MongoDB中。
如果是一次性將CSV數據導入到MongoDB中,那麼您可以使用mongoimport
實用程序來執行此操作。否則,您需要在處理CSV輸入數據的代碼中進行轉換。
使用Java
Grid FS將文件存儲在兩個集合:
要檢索存儲的文件,GridFS會查找並返回其所有組件塊。
商店csv文件到的MongoDB通過使用GridFS
public static void main(String[] args) throws IOException {
mongoDB_GRIDFS("D:\\Yash\\JavaCSV.csv");
}
public static void mongoDB_GRIDFS(String csvlocation) throws IOException{
Mongo Mongo = new Mongo("localhost" , 27017); // Connect to MongoDB
DB db = Mongo.getDB("DBName"); // Get database
String bucketName = "BucketName";
GridFS gridFs = new GridFS(db,bucketName); //Create instance of GridFS implementation
String imageName = "image1";
//upload(gridFs, inputcsvlocation);
//download(gridFs, imageName);
//delete(gridFs, imageName);
Mongo.close();
}
public static void upload(GridFS gridFs, String csvlocation, String imageName) throws IOException{
GridFSInputFile gridFsInputFile = gridFs.createFile(new File(csvlocation));
gridFsInputFile.setId("777");
gridFsInputFile.setFilename(imageName); //Set a name on GridFS entry
gridFsInputFile.save(); //Save the file to MongoDB
}
public static void download(GridFS gridFs, String imageName) throws IOException{
GridFSDBFile outputImageFile = gridFs.findOne(imageName);
String outcsvLocation = "D:\\Yash\\mongoCSV.csv";//Location of the file read from MongoDB to be written
outputImageFile.writeTo(new File(outcsvLocation));
}
public static void delete(GridFS gridFs, String imageName){
gridFs.remove(gridFs.findOne(imageName));
}
轉換CSV數據到JSON(或)JSON到csv
public static void main(String myHelpers[]) throws ParseException, IOException{
String jsonString = "{'csvdata': [{'field1': 11,'field2': 12,'field3': 13},{'field1': 21,'field2': 22,'field3': 23},{'field1': 31,'field2': 32,'field3': 33}]}";
Json2Csv("D:\\Yash\\JavaCSV.csv", jsonString);
csv2Josn("D:\\Yash\\JavaCSV.csv");
}
@SuppressWarnings("unchecked")
public static void csv2Josn(String fileName) throws IOException{
BufferedReader csv = new BufferedReader(new FileReader(new File(fileName)));
JSONObject obj = new JSONObject();
JSONArray csvFields = new JSONArray();
String csvRow = "", keys = "";
int rowscount = 0;
while((csvRow = csv.readLine()) != null){
String[] data = csvRow.split(",");
if (rowscount == 0) keys = csvRow;
else if(data.length == keys.split(",").length){
JSONObject fieldobj = new JSONObject();
for (int i = 0; i < data.length; i++) fieldobj.put(keys.split(",")[i], data[i]);
csvFields.add(fieldobj);
}
rowscount++;
}
obj.put("scvdata", csvFields);
System.out.println("Final Object : "+obj);
}
public static void Json2Csv(String fileName, String jsonString) throws IOException, ParseException{
JSONParser jparser = new JSONParser();
jsonString = jsonString.replace("'", "\"");
java.io.FileWriter writer = new FileWriter(fileName);
JSONObject output = (org.json.simple.JSONObject) jparser.parse(jsonString);
JSONArray csvdata = (org.json.simple.JSONArray) output.get("csvdata");
String[] orderdkeys = {"field1", "field2", "field3"}; // JSONObject = HashMap (unorered)
writer.append(orderdkeys[0]+","+orderdkeys[1]+","+orderdkeys[2]+"\n");
for (int i = 0; i < csvdata.size(); i++) {
JSONObject row = (JSONObject) csvdata.get(i);
StringBuffer rowdata= new StringBuffer();
if (orderdkeys.length == row.size())
for (int j = 0; j < row.size(); j++) {
rowdata.append(row.get(orderdkeys[j]));
if (j+1 < row.size()) rowdata.append(",");
}
writer.append(rowdata.append("\n").toString());
}
writer.flush(); writer.close();
}