Java对MongoDB中的数据查询处理

Java语言标准的数据库时MySQL,但是有些时候也会用到MongoDB,这次Boss交代处理MongoDB,所以讲代码以及思路记录下了

摸索的过程,才发现软件的适用还是很重要的啊!!!

我连接的MongoDB的数据是远程数据库,连接本地数据库的方法网上有很多:

//连接到MongoDB服务 如果是远程连接可以替换“localhost”为服务器所在IP地址
        //ServerAddress()两个参数分别为 服务器地址 和 端口
        ServerAddress serverAddress = new ServerAddress("106.12.34.175",27017);
        List<ServerAddress> addrs = new ArrayList<ServerAddress>();
        addrs.add(serverAddress);

        //MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
        MongoCredential credential = MongoCredential.createScramSha1Credential("***", "***", "***".toCharArray());
        List<MongoCredential> credentials = new ArrayList<MongoCredential>();
        credentials.add(credential);

        //通过连接认证获取MongoDB连接
        MongoClient mongoClient = new MongoClient(addrs,credentials);

        //连接到数据库
        MongoDatabase mongoDatabase = mongoClient.getDatabase("***");
        MongoCollection<Document> collection  = mongoDatabase.getCollection("dianping_city");

        //查询过程
        BasicDBObject query = new BasicDBObject();
        query.put("city_num","xxx");

        //查询结果
        //MongoCursor<Document> cursor = collection.find(query).skip(0).limit(10).iterator();
        MongoCursor<Document> cursor = collection.find(query).skip(0).iterator();

这样查询结果就有了,下面要将查询结果存储为CSV文件,我这里实现的是对查询的结果进行存储(对于多条的查询数据,也一并放入CSV文件中);存储的过程需要注意:从MongoDB返回的数据类型,多条数据类型在CSV文件中的对齐。

List<String> resultList = new LinkedList<>();
        List<String> tableList = new ArrayList<>();
        while (cursor.hasNext()) {
            String  jsonString = new String();
            jsonString = cursor.next().toJson();
            int length = jsonString.length();
            jsonString = "[{" + jsonString.substring(jsonString.indexOf(",") + 1, length) + "]";
            System.out.println(jsonString);

            JSONArray jsonArray = new JSONArray(jsonString);
            JSONObject jsonObject = jsonArray.getJSONObject(0);
            try {
                if(tableList.size() == 0) {
                    StringBuilder stringKey = new StringBuilder();
                    Iterator iterator = jsonObject.keys();
                    while (iterator.hasNext()) {
                        String key = (String) iterator.next();
                        if(key.compareTo("shophours") == 0){continue;}
                        tableList.add(key);
                    stringKey.append(key).append(',');
                    }
                    resultList.add(stringKey.deleteCharAt(stringKey.length()-1).toString());
                }
                StringBuilder stringValue = new StringBuilder();
                for(String entry: tableList){
                    String value = new String();
                    if(!jsonObject.has(entry)){
                        value = "null";
                    }
                    else {
                        value = jsonObject.get(entry).toString();
                    }
                    stringValue.append(value).append(',');
                }
                resultList.add(stringValue.deleteCharAt(stringValue.length()-1).toString());
            }
            catch (JSONException e){
                e.printStackTrace();
            }
        }

总结一下:之前没有处理过MongoDB,所以在这个small task上花了点时间,不过最后也有收获,至少MongoDB与Java相关的坑踩了一部分,为以后积累经验嘛。

整体代码:

  1 package MongoDB;
  2 
  3 import com.mongodb.*;
  4 import com.mongodb.client.MongoCollection;
  5 import com.mongodb.client.MongoCursor;
  6 import com.mongodb.client.MongoDatabase;
  7 import org.bson.Document;
  8 import org.json.*;
  9 import java.io.*;
 10 import java.util.*;
 11 
 12 public class outputData {
 13     public static void main(String[] args){
 14         //连接到MongoDB服务 如果是远程连接可以替换“localhost”为服务器所在IP地址
 15         //ServerAddress()两个参数分别为 服务器地址 和 端口
 16         ServerAddress serverAddress = new ServerAddress("IP",port);
 17         List<ServerAddress> addrs = new ArrayList<ServerAddress>();
 18         addrs.add(serverAddress);
 19 
 20         //MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
 21         MongoCredential credential = MongoCredential.createScramSha1Credential("***", "***", "***".toCharArray());
 22         List<MongoCredential> credentials = new ArrayList<MongoCredential>();
 23         credentials.add(credential);
 24 
 25         //通过连接认证获取MongoDB连接
 26         MongoClient mongoClient = new MongoClient(addrs,credentials);
 27 
 28         //连接到数据库
 29         MongoDatabase mongoDatabase = mongoClient.getDatabase("****");
 30         MongoCollection<Document> collection  = mongoDatabase.getCollection("dianping_city");
 31 
 32         //查询过程
 33         BasicDBObject query = new BasicDBObject();
 34         query.put("city_num","xxx");
 35 
 36         //查询结果
 37         //MongoCursor<Document> cursor = collection.find(query).skip(0).limit(10).iterator();
 38         MongoCursor<Document> cursor = collection.find(query).skip(0).iterator();
 39 
 40 
 41         List<String> resultList = new LinkedList<>();
 42         List<String> tableList = new ArrayList<>();
 43         while (cursor.hasNext()) {
 44             String  jsonString = new String();
 45             jsonString = cursor.next().toJson();
 46             int length = jsonString.length();
 47             jsonString = "[{" + jsonString.substring(jsonString.indexOf(",") + 1, length) + "]";
 48             System.out.println(jsonString);
 49 
 50             JSONArray jsonArray = new JSONArray(jsonString);
 51             JSONObject jsonObject = jsonArray.getJSONObject(0);
 52             try {
 53                 if(tableList.size() == 0) {
 54                     StringBuilder stringKey = new StringBuilder();
 55                     Iterator iterator = jsonObject.keys();
 56                     while (iterator.hasNext()) {
 57                         String key = (String) iterator.next();
 58                         if(key.compareTo("shophours") == 0){continue;}
 59                         tableList.add(key);
 60                     stringKey.append(key).append(',');
 61                     }
 62                     resultList.add(stringKey.deleteCharAt(stringKey.length()-1).toString());
 63                 }
 64                 StringBuilder stringValue = new StringBuilder();
 65                 for(String entry: tableList){
 66                     String value = new String();
 67                     if(!jsonObject.has(entry)){
 68                         value = "null";
 69                     }
 70                     else {
 71                         value = jsonObject.get(entry).toString();
 72                     }
 73                     stringValue.append(value).append(',');
 74                 }
 75                 resultList.add(stringValue.deleteCharAt(stringValue.length()-1).toString());
 76             }
 77             catch (JSONException e){
 78                 e.printStackTrace();
 79             }
 80         }
 81         cursor.close();
 82 
 83         try {
 84             File csv = new File("C:\\Users\\Administrator\\Desktop\\tmp2.csv");
 85             OutputStreamWriter outStream = null;
 86             outStream = new OutputStreamWriter(new FileOutputStream(csv), "GBK");
 87             BufferedWriter bw = new BufferedWriter(outStream);
 88            for(String entry : resultList){
 89             // 添加新的数据行
 90                 bw.write(entry.toCharArray());
 91                 bw.newLine();
 92            }
 93             bw.close();
 94         }
 95         catch (FileNotFoundException e) {
 96             // File对象的创建过程中的异常捕获
 97             e.printStackTrace();
 98         } catch (IOException e) {
 99             // BufferedWriter在关闭对象捕捉异常
100             e.printStackTrace();
101         }
102         System.out.println("MongoDB connect successfully: "+"mongoDatabase = " + mongoDatabase.getName());
103     }
104 }
View Code

最后贴上几个为以后做准备的链接:

MongoDB安装:http://www.cnblogs.com/lzrabbit/p/3682510.html

Java下MongoDB查询:https://www.cnblogs.com/luoaz/p/4691639.html

猜你喜欢

转载自www.cnblogs.com/jayinnn/p/9593512.html