Java连接示例
<p>本文主要介绍Java连接示例。</p>
<p> <img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20202805142819-1c336f7e9e0b.png" style="height:26px; margin:0px; width:65px" />:</p>
<p> • 官方资料可参考<a href="http://mongodb.github.io/mongo-java-driver/3.0/driver/getting-started/" target="_blank">Quick Start</a>。</p>
<p> • Jar包可单击<a href="https://repo1.maven.org/maven2/org/mongodb/mongo-java-driver/3.0.4/" target="_blank">下载</a>。</p>
<p><span style="font-size:18px"><strong>操作步骤</strong></span></p>
<p>1. 获取 DDS 实例的连接信息。</p>
<p>2. Java连接实例:Maven配置。</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas"><dependencies></span></p>
<p><span style="font-family:Consolas"> <dependency></span></p>
<p><span style="font-family:Consolas"> <groupId>org.mongodb</groupId></span></p>
<p><span style="font-family:Consolas"> <artifactId>mongo-java-driver</artifactId></span></p>
<p><span style="font-family:Consolas"> <version>3.0.4</version></span></p>
<p><span style="font-family:Consolas"> </dependency></span></p>
<p><span style="font-family:Consolas"></dependencies></span></p>
<p><span style="font-family:Consolas">Java Code。</span></p>
<p><span style="font-family:Consolas"><strong>import</strong> java.util.ArrayList;</span></p>
<p><span style="font-family:Consolas"><strong>import</strong> java.util.List;</span></p>
<p><span style="font-family:Consolas"><strong>import</strong> java.util.UUID;</span></p>
<p><span style="font-family:Consolas"><strong>import</strong> org.bson.BsonDocument;</span></p>
<p><span style="font-family:Consolas"><strong>import</strong> org.bson.BsonString;</span></p>
<p><span style="font-family:Consolas"><strong>import</strong> org.bson.Document;</span></p>
<p><span style="font-family:Consolas"><strong>import</strong> com.mongodb.MongoClient;</span></p>
<p><span style="font-family:Consolas"><strong>import</strong> com.mongodb.MongoClientOptions;</span></p>
<p><span style="font-family:Consolas"><strong>import</strong> com.mongodb.MongoClientURI;</span></p>
<p><span style="font-family:Consolas"><strong>import</strong> com.mongodb.MongoCredential;</span></p>
<p><span style="font-family:Consolas"><strong>import</strong> com.mongodb.ServerAddress;</span></p>
<p><span style="font-family:Consolas"><strong>import</strong> com.mongodb.client.MongoCollection;</span></p>
<p><span style="font-family:Consolas"><strong>import</strong> com.mongodb.client.MongoCursor;</span></p>
<p><span style="font-family:Consolas"><strong>import</strong> com.mongodb.client.MongoDatabase;</span></p>
<p><span style="font-family:Consolas"> <strong>public</strong> <strong>class</strong> <strong>Main</strong> {</span></p>
<p><span style="font-family:Consolas"> <strong>public</strong> <strong>static</strong> ServerAddress seed1 = <strong>new</strong> ServerAddress("demotest.mongodb.db.cloud.papub",</span></p>
<p><span style="font-family:Consolas"> 15923);</span></p>
<p><span style="font-family:Consolas"> <strong>public</strong> <strong>static</strong> String username = "demouser";</span></p>
<p><span style="font-family:Consolas"> <strong>public</strong> <strong>static</strong> String password = "123456";</span></p>
<p><span style="font-family:Consolas"> <strong>public</strong> <strong>static</strong> String ReplSetName = "demotest";</span></p>
<p><span style="font-family:Consolas"> <strong>public</strong> <strong>static</strong> String DEFAULT_DB = "test";</span></p>
<p><span style="font-family:Consolas"> <strong>public</strong> <strong>static</strong> String DEMO_DB = "test";</span></p>
<p><span style="font-family:Consolas"> <strong>public</strong> <strong>static</strong> String DEMO_COLL = "testColl";</span></p>
<p><span style="font-family:Consolas"> <strong>public</strong> <strong>static</strong> MongoClient <strong>createMongoDBClient</strong>() {</span></p>
<p><span style="font-family:Consolas"> // 构建Seed列表</span></p>
<p><span style="font-family:Consolas"> List<ServerAddress> seedList = <strong>new</strong> ArrayList<ServerAddress>();</span></p>
<p><span style="font-family:Consolas"> seedList.add(seed1);</span></p>
<p><span style="font-family:Consolas"> // 构建鉴权信息</span></p>
<p><span style="font-family:Consolas"> List<MongoCredential> credentials = <strong>new</strong> ArrayList<MongoCredential>();</span></p>
<p><span style="font-family:Consolas"> credentials.add(MongoCredential.createScramSha1Credential(username, DEFAULT_DB,</span></p>
<p><span style="font-family:Consolas"> password.toCharArray()));</span></p>
<p><span style="font-family:Consolas"> // 构建操作选项,requiredReplicaSetName属性外的选项根据实际需求配置,默认参数满足大多数场景</span></p>
<p><span style="font-family:Consolas"> MongoClientOptions options = MongoClientOptions.builder().requiredReplicaSetName(ReplSetName)</span></p>
<p><span style="font-family:Consolas"> .socketTimeout(2000).connectionsPerHost(1).build();</span></p>
<p><span style="font-family:Consolas"> <strong>return</strong> <strong>new</strong> MongoClient(seedList, credentials, options);</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> <strong>public</strong> <strong>static</strong> MongoClient <strong>createMongoDBClientWithURI</strong>() {</span></p>
<p><span style="font-family:Consolas"> // 另一种通过URI初始化</span></p>
<p><span style="font-family:Consolas"> //mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]</span></p>
<p><span style="font-family:Consolas"> MongoClientURI connectionString = <strong>new</strong> MongoClientURI("mongodb://" + username + ":" + password + "@"</span></p>
<p><span style="font-family:Consolas"> + seed1 + "/" + DEFAULT_DB + "?replicaSet=" + ReplSetName);</span></p>
<p><span style="font-family:Consolas"> <strong>return</strong> <strong>new</strong> MongoClient(connectionString);</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> <strong>public</strong> <strong>static</strong> <strong>void</strong> <strong>main</strong>(String args[]) {</span></p>
<p><span style="font-family:Consolas"> MongoClient client = createMongoDBClient();</span></p>
<p><span style="font-family:Consolas"> // or</span></p>
<p><span style="font-family:Consolas"> // MongoClient client = createMongoDBClientWithURI();</span></p>
<p><span style="font-family:Consolas"> <strong>try</strong> {</span></p>
<p><span style="font-family:Consolas"> // 取得Collecton句柄</span></p>
<p><span style="font-family:Consolas"> MongoDatabase database = client.getDatabase(DEMO_DB);</span></p>
<p><span style="font-family:Consolas"> MongoCollection<Document> collection = database.getCollection(DEMO_COLL);</span></p>
<p><span style="font-family:Consolas"> // 插入数据</span></p>
<p><span style="font-family:Consolas"> Document doc = <strong>new</strong> Document();</span></p>
<p><span style="font-family:Consolas"> String demoname = "JAVA:" + UUID.randomUUID();</span></p>
<p><span style="font-family:Consolas"> doc.append("DEMO", demoname);</span></p>
<p><span style="font-family:Consolas"> doc.append("MESG", "Hello AliCoudDB For MongoDB");</span></p>
<p><span style="font-family:Consolas"> collection.insertOne(doc);</span></p>
<p><span style="font-family:Consolas"> System.out.println("insert document: " + doc);</span></p>
<p><span style="font-family:Consolas"> // 读取数据</span></p>
<p><span style="font-family:Consolas"> BsonDocument filter = <strong>new</strong> BsonDocument();</span></p>
<p><span style="font-family:Consolas"> filter.append("DEMO", <strong>new</strong> BsonString(demoname));</span></p>
<p><span style="font-family:Consolas"> MongoCursor<Document> cursor = collection.find(filter).iterator();</span></p>
<p><span style="font-family:Consolas"> <strong>while</strong> (cursor.hasNext()) {</span></p>
<p><span style="font-family:Consolas"> System.out.println("find document: " + cursor.next());</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> } <strong>finally</strong> {</span></p>
<p><span style="font-family:Consolas"> // 关闭Client,释放资源</span></p>
<p><span style="font-family:Consolas"> client.close();</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> <strong>return</strong>;</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas">}</span></p>
<p> </p>
</td>
</tr>
</tbody>
</table>
提交成功!非常感谢您的反馈,我们会继续努力做到更好!