以毫秒为单位将数据存储到MongoDB Collection

时间:2014-11-05 10:55:56

标签: java mongodb nosql

我在java中编写应用程序来存储来自UDP广播的数据并将它们存储到MongoDB。 UDP以毫秒为单位发送,它包含由昏迷(约30个字段)分隔的文本文件。

但是我遇到了以下问题(问题仅在4-5分钟内显示我运行程序)。

Connect success
Doc inserted
Connect success
Doc inserted
Connect success
com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches AnyServerSelector{}. Client view of cluster state is {type=Unknown, servers=[{address=127.0.0.1:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.SocketException: Too many open files}}]
    at com.mongodb.BaseCluster.getServer(BaseCluster.java:82)
    at com.mongodb.DBTCPConnector.getServer(DBTCPConnector.java:654)
    at com.mongodb.DBTCPConnector.access$300(DBTCPConnector.java:39)
    at com.mongodb.DBTCPConnector$MyPort.getConnection(DBTCPConnector.java:503)
    at com.mongodb.DBTCPConnector$MyPort.get(DBTCPConnector.java:451)
    at com.mongodb.DBTCPConnector.authenticate(DBTCPConnector.java:624)
    at com.mongodb.DBApiLayer.doAuthenticate(DBApiLayer.java:195)
    at com.mongodb.DB.authenticateCommandHelper(DB.java:765)
    at com.mongodb.DB.authenticate(DB.java:721)
    at UdpGPSListener.main(UdpGPSListener.java:108)

以下是我在Java中的代码:

            DatagramSocket serverSocket = new DatagramSocket(2020);
        byte[] receiveData = new byte[2048];
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        serverSocket.receive(receivePacket);
        String dtCollection = new String(receivePacket.getData(), "UTF8");
        String[] arrCollection = dtCollection.split(",");
        String dtField = "UnitTime,Ab1,ab2,ab3,ab4,ab5,ab6,ab7,cc1,cc2,cc3,cc4,cc5,cc6,cc7,cc8,cc9,cc9,cc10,m01,m02,m03,m04,m05,m06,m07";
        String[] arrField = dtField.split(",");
        String gbCollection="";
        for (int m=0; m<arrCollection.length; m++) {
            gbCollection+=arrField[m] + "=" +arrCollection[m] +",";
        }
            try {
                MongoClient mClient = new MongoClient();
                //MongoClient mClient = new MongoClient("127.0.0.1", 27017);
                DB db = mClient.getDB("mms");
                        System.out.println("Connect success");
                boolean auth = db.authenticate("user1", "passw0rd".toCharArray());
                DBCollection coll = db.getCollection("tgps");
                BasicDBObject doc = new BasicDBObject(arrField[0], arrCollection[0]).
                    append(arrField[1], arrCollection[1]).
                    append(arrField[2], arrCollection[2]).
                    append(arrField[3], arrCollection[3]).
                    append(arrField[4], arrCollection[4]).
                    append(arrField[5], arrCollection[5]).
                    append(arrField[6], arrCollection[6]).
                    append(arrField[7], arrCollection[7]).
                    append(arrField[8], arrCollection[8]).
                    append(arrField[9], arrCollection[9]).
                    append(arrField[10], arrCollection[10]).
                    append(arrField[11], arrCollection[11]).
                    append(arrField[12], arrCollection[12]).
                    append(arrField[13], arrCollection[13]).
                    append(arrField[14], arrCollection[14]).
                    append(arrField[15], arrCollection[15]).
                    append(arrField[16], arrCollection[16]).
                    append(arrField[17], arrCollection[17]).
                    append(arrField[18], arrCollection[18]).
                    append(arrField[19], arrCollection[19]).
                    append(arrField[20], arrCollection[20]).
                    append(arrField[21], arrCollection[21]).
                    append(arrField[22], arrCollection[22]).
                    append(arrField[23], arrCollection[23]).
                    append(arrField[24], arrCollection[24]).
                    append(arrField[25], new Date());
                coll.insert(doc);
                System.out.println("Doc inserted");
            } catch(UnknownHostException  e) {
                    System.err.println(e.getClass().getName() + ": " + e.getMessage() );
            } catch(MongoException e) {
                    e.printStackTrace();
            }

1 个答案:

答案 0 :(得分:0)

我认为是

  
    

以毫秒发送的UDP

  

你的意思是每毫秒都可能发生一次更新。

无论如何,你遇到这个问题:

  
    

java.net.SocketException:打开的文件太多

  

我还假设您在某种循环中收到了您的活动。

在这种情况下,这可能是您的问题:

MongoClient mClient = new MongoClient();

在开始收听事件之前,您应该只启动MongoClient一次,否则会打开一个连接,并且永远不会为您使用的每个事件关闭。

这也应该用于获取db,验证和获取集合。即

 MongoClient mClient = new MongoClient();
 //MongoClient mClient = new MongoClient("127.0.0.1", 27017);
 DB db = mClient.getDB("mms");
 System.out.println("Connect success");
 boolean auth = db.authenticate("user1", "passw0rd".toCharArray());
 DBCollection coll = db.getCollection("tgps");

在您开始接收活动之前,所有这些只应该一次