我在通过 TLS/SSL 使用纯 Java 连接我的 DocumentDB 集群时遇到问题
我按照AWS 文档遵循的程序是这样的:
我.pem
从 AWS下载了文件并复制到我的 java 项目的根目录中,从那里我执行:
keytool -importcert -trustcacerts -file rds-combined-ca-bundle.pem -alias certAlias -keystore rds-ca-certs -storepass keyStorePassword
Run Code Online (Sandbox Code Playgroud)
这已经在rds-ca-certs
我的项目的根目录创建了,现在看起来像这样:
并且,Main.java 中的 java 代码是:
package com.example.documentdb;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.ServerAddress;
import com.mongodb.MongoException;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
public final class Main {
private Main() {
}
public static void main(String[] args) {
String template = "mongodb://%s:%s@%s/test?ssl=true&replicaSet=rs0&readpreference=%s";
String username = "myUsernameInCluster";
String password = "myPasswordInCluster"; …
Run Code Online (Sandbox Code Playgroud) 我有以下的bulk_write来将数据集中的每个文档更新插入到集合中。
data = [] # list of dicts/documents
mongo = MongoClient('some_host')
db = mongo['some_db']
collection = db['some_collection']
operations = [UpdateOne({'_id': d['_id']}, {'$set': d}, upsert=True) for d in data]
result = collection.bulk_write(operations)
Run Code Online (Sandbox Code Playgroud)
它在本地 MongoDB 服务器上运行良好,但在 AWS DocumentDB 上运行时收到以下错误消息。有一种解决方法是我删除并插入每条记录,但想了解为什么会发生这种情况并使用更新而不是删除+插入
pymongo.errors.OperationFailure: Retryable writes are not supported, full error: {'ok': 0.0, 'code': 301, 'errmsg': 'Retryable writes are not supported', 'operationTime': Timestamp(1638883052, 1)}
我希望通过侦听 DocumentDB 更改流,通过 lambda 函数填充 SQS 队列。我知道这可以通过DynamoDB来完成。有没有办法用 DocumentDB 来实现这一点?
我目前正在尝试从 AWS Document DB 编写元数据库问题,并且遇到了需要将字符串转换为整数的问题。不幸的是,aws documentdb 似乎不支持 $toInt,我不知道如何解决它。这是查询:
[
{"$match": {
"metaData.fileSize" : {"$exists": true}
}},
{"$project": {
"file_size" : "$metaData.fileSize",
"timestamp": 1,
"past7Days":
{ "$subtract":
[ ISODate(), 604800000]
}
}},
{"$project": {
"file_size" : 1,
"timestamp": 1,
"dayofweek": {"$dayOfWeek":["$timestamp"]},
"past7DaysComp":
{ "$subtract":
[ "$timestamp", "$past7Days"]
}
}},
{"$group" :
{
"_id" : {"dayofweek" : "$dayofweek"},
"size": {"$avg" : "$file_size"}
}
}
]
Run Code Online (Sandbox Code Playgroud)
该组不返回任何大小,因为它不是数字类型。有什么想法如何将 file_size 转换为整数、双精度或浮点数吗?
我无法从我的 Amazon EC2 ubuntu 计算机连接到我的 Amazon DocumentDB?我已经检查了 Amazon DocumentDB 的安全性,它当前被分配给具有“所有流量”的默认值。
我已尝试直接从 AWS 实例页面执行以下命令,尽管我收到了下面包含的错误消息。
我已遵循此 aws 指南https://docs.aws.amazon.com/documentdb/latest/developerguide/getting-started.connect.html
EC2 ubuntu 机器上的 Mongo shell 命令
mongo --ssl --host mydatabasename.23scnncsd3.eu-west-1.docdb.amazonaws.com:27017 --sslCAFile rds-combined-ca-bundle.pem --username webuser --password mypassword
Run Code Online (Sandbox Code Playgroud)
我从 ubuntu 命令提示符中收到的错误消息如下
错误信息
MongoDB shell version v3.6.11
connecting to: mongodb://mydatabasename.23scnncsd3.eu-west-1.docdb.amazonaws.com:27017/?gssapiServiceName=mongodb
2019-03-11T21:39:37.587+0000 W NETWOK [thread1] Failed to connect to 172.31.45.184:27017 after 5000ms milliseconds, giving up.
2019-03-11T21:39:37.595+0000 E QUERY [thread1] Error: couldn't connect to server mydatabasename.23scnncsd3.eu-west-1.docdb.amazonaws.com:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:263:13
@(connect):1:6
exception: connect failed
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?任何帮助表示赞赏!
非常感谢,
更新
当我调用函数时出现以下错误mongoTemplate.collectionExists
com.mongodb.MongoCommandException: Command failed with error 2: 'Cannot open a new cursor since too many cursors are already opened' on server documentdb-cluster-dev-2.cipll54l4b4g.eu-central-1.docdb.amazonaws.com:27017. The full response is
{
"ok": 0,
"errmsg": "Cannot open a new cursor since too many cursors are already opened",
"code": 2
}
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:131)
at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2589)
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:499)
at org.springframework.data.mongodb.core.MongoTemplate.collectionExists(MongoTemplate.java:600)
at sun.reflect.GeneratedMethodAccessor217.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:223)
at org.springframework.cloud.context.scope.GenericScope$LockedScopedProxyFactoryBean.invoke(GenericScope.java:494)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
at org.springframework.data.mongodb.core.MongoTemplate$$EnhancerBySpringCGLIB$$ba89d337.collectionExists(<generated>)
Run Code Online (Sandbox Code Playgroud)
我使用spring框架的MongoTemplate和AWS的DocumentDB作为mongodb(spring-boot-starter-data-mongodb:2.0.10.RELEASE)
有什么办法可以增加光标的限制吗?我看到 ORACLE 有一种方法可以增加打开游标的限制,但没有找到 mongo/documentDb 的任何内容。
我正在使用 Spring 中的 DocumentDb,我想使用另一个字段的值对一个字段进行 updateMany。
以下查询在 MongoDb 控制台中运行:
db.mtpProject.update({}, [{$set: {"fieldToBeUpdated": "$myCurrentField"}}]).
Run Code Online (Sandbox Code Playgroud)
如果我删除 $set 之前的 [],则该字段不会使用 myCurrentField 的值更新,而是使用字符串“myCurrentField”更新。
在Java中我尝试像这样执行它:
MongoDatabase db = mongoTemplate.getDb();
MongoCollection collection = db.getCollection("myProject");
collection.updateMany(new Document(), new Document("$set", new
Document("fieldToBeUpdated", "$myCurrentField")));
Run Code Online (Sandbox Code Playgroud)
但因为是在没有数组的情况下创建它,所以只需将 fieldToBeUpdated 的值设置为“$myCurrentField”。
我提到我也尝试过将 $addFields 与 $out 一起使用,但 DocumentDb 不支持 $out。
Aggregation agg =
Aggregation.newAggregation(
aoc ->
new Document(
"$addFields",
new Document("fieldToBeUpdated", "$myCurrentField")),
aoc -> new Document("$out", "myProject"));
mongoTemplate.aggregate(agg, "myProject", MyProject.class);
Run Code Online (Sandbox Code Playgroud)
我希望使用保存在 myCurrentField 上的值更新字段,而不是使用字段名称。
我从 MongoDB 迁移到 AWS DocumentDB,但在查询性能方面遇到了一些问题。运行配置文件时,我发现几个查询花费了很长的时间,远远超过了几秒钟。我试图与文档一起理解其中一些阶段的含义,例如 LIMIT_SKIP ,并理解为什么它们花了这么长时间。特别是那些针对 _id 的更新。
作为上下文,我将 Mongoid 6.4.4 与 AWS Document DB 结合使用。
这是我得到的个人资料,下面是模型统计数据:
{
"op": "update",
"ts": 1592322527421,
"ns": "mytable.paths",
"command": {
"q": {
"_id": "5ee8e93217f3830001cf9765"
},
"u": {
"$set": {
"last_active_at": "2020-06-16T15:48:19.774Z",
"updated_at": "2020-06-16T15:48:19.774Z"
}
}
},
"nMatched": 1,
"nModified": 1,
"protocol": "op_query",
"millis": 27643,
"planSummary": "IXSCAN",
"execStats": {
"stage": "UPDATE",
"nReturned": "0",
"executionTimeMillisEstimate": "27638.070",
"inputStages": [
{
"stage": "LIMIT_SKIP",
"nReturned": "1",
"executionTimeMillisEstimate": "27637.167",
"inputStage": {
"stage": "IXSCAN",
"nReturned": "1",
"executionTimeMillisEstimate": "0.046",
"indexName": "_id_", …
Run Code Online (Sandbox Code Playgroud) 我正在将 MongoDB 中的数据库移动到 AWS DocumentDB。我正在 Java 中使用mongodb-driver
(版本 3.6.4)来写入和读取数据,以及配置我使用的集合。特别是,我有一个架构验证(编写为 JSON 架构),它似乎与 AWS DocumentDB 不兼容。
这里的文档说不$jsonSchema
支持,但我一般没有看到任何关于文档验证的明确断言。
真的不支持吗?有没有其他选择(除了在Java代码中这样做)?
aws-documentdb ×10
mongodb ×7
aws-lambda ×1
java ×1
metabase ×1
performance ×1
pymongo ×1
spring ×1
ubuntu ×1