TL; DR我应该使用Transaction.getAll()还是使用for循环并使用来逐个更新文档Transaction.get()
考虑以下架构:
- someTopLevelCollection
- accountId1
- projects
- tasks
- users
- accountId2
- projects
- tasks
- users
Run Code Online (Sandbox Code Playgroud)
每当在项目,任务子集合中创建项目/任务时,用户集合中的计数器都会更新,例如projectsCount和taskCount。
对用户的引用作为userId的数组保存在项目和任务中,如下所示:
注意:为简洁起见,删除了其他字段
项目/任务结构:
{
"name": "someName",
"status": "someStatus",
"users": [
"userId1",
"userId2",
....
],
...
}
Run Code Online (Sandbox Code Playgroud)
现在,我需要使用事务为用户数组内的所有userId更新一个计数器。
方法1:
const accountId = context.params.accountId;
const userIds = snapshot.data().users;
userIds.forEach(userId => {
const userDocRef = db.collection(someTopLevelCollection)
.doc(accountId)
.collection('users')
.doc(userId);
let transaction = db.runTransaction(transaction => {
return transaction.get(userDocRef)
.then(doc => {
const snapshotData = doc.data();
let newCounterValue = …Run Code Online (Sandbox Code Playgroud) TL; DR
想要这个流程:
ws://...
websocket client 1 <-----------> websocket client 2
^
|
server (send messages via views)
Run Code Online (Sandbox Code Playgroud)
所以我有以下内容:
views.py
def alarm(request):
layer = get_channel_layer()
async_to_sync(layer.group_send)('events', {
'type': 'events.alarm',
'content': 'triggered'
})
return HttpResponse('<p>Done</p>')
Run Code Online (Sandbox Code Playgroud)
consumers.py
class EventConsumer(JsonWebsocketConsumer):
def connect(self):
print('inside EventConsumer connect()')
async_to_sync(self.channel_layer.group_add)(
'events',
self.channel_name
)
self.accept()
def disconnect(self, close_code):
print('inside EventConsumer disconnect()')
print("Closed websocket with code: ", close_code)
async_to_sync(self.channel_layer.group_discard)(
'events',
self.channel_name
)
self.close()
def receive_json(self, content, **kwargs):
print('inside EventConsumer receive_json()')
print("Received event: {}".format(content))
self.send_json(content)
def events_alarm(self, event):
print('inside EventConsumer …Run Code Online (Sandbox Code Playgroud) Pycharm 提供:
通过refactor > Move(f6-键盘快捷键)将函数/类/变量从一个文件移动到另一个文件
导入当前文件中缺少的导入
在 vscode 中是否可用?我试图寻找和整个自动进口pull请求来到这里。但是在移动重构中找不到任何东西。
目前是否可以通过任何扩展等在 vscode 中这样做?
我有一个在Amazon EC2上运行的Spark Master。我尝试从另一个ec2实例中使用pyspark连接到它,如下所示:
spark = SparkSession.builder.appName("MyApp") \
.master("spark_url_as_obtained_in_web_ui") \
.getOrCreate()
Run Code Online (Sandbox Code Playgroud)
以下是错误:
要调整日志记录级别,请使用sc.setLogLevel(newLevel)。对于SparkR,请使用setLogLevel(newLevel)。
2018-04-04 20:03:04 WARN Utils:66-服务'sparkDriver'无法在随机的免费端口上绑定。您可以检查是否配置了适当的绑定地址。
............
java.net.BindException:无法分配请求的地址:16次重试(在随机的空闲端口上)后,服务“ sparkDriver”失败!考虑将服务“ sparkDriver”的适当绑定地址(例如SparkDriver的spark.driver.bindAddress)显式设置为正确的绑定地址。
我尝试了此处描述的所有解决方案,但无济于事:
怎么可能出问题了?
我目前有一个Redis集群,实例类型为cache.t2.micro
有没有选项来自动备份Redis的数据到S3这个实例类型,也一个不能运行BGSAVE为命令其描述限制这里
我注意到,如果重新启动Redis群集中的主节点,或者Redis引擎版本从较低版本更改为较高版本(即从3.x到4.x),则会完全删除节点中的数据,尽管他们声称尽力而为.
此处还不支持为此实例类型创建快照
我能想到的唯一选择是使用DUMP命令并获取密钥的序列化版本并为所有DBS归档此数据,然后使用RESTORE命令将其还原回新集群.但这可能不是最好的方法,因为它不可扩展,最终需要一些时间来处理更大的数据集.
同样对于启用了TTL的密钥,我必须运行TTL命令,获取TTL(这是额外的开销).
但DUMP命令发出错误DUMP有效负载版本或校验和错误,该规则将该选项排除(难怪此命令不受限制)
除了读取所有键及其值之外,还有其他方法可以执行备份和还原吗?
谢谢.
编辑:
所以我知道这不是最好的方法,但这是我能想到的.
TL; DR
读取所有密钥并从一个群集迁移到另一个群集.
对于具有config的集群,这应该不是问题.大于t2.*
码:
import traceback
from redis import Redis
import itertools
def migrate_to_another_redis_node(source_node, source_node_port_no, dest_node, dest_node_port_no):
'''
migrates the keys from one redis node to the other
:param source_node: source redis node url
:param source_node_port_no: source redis node port number
:param dest_node: destination redis node url
:param dest_node_port_no: destination redis node port …Run Code Online (Sandbox Code Playgroud) TL;DR不想在不必每次都设置消息属性的情况下动态指定交易/促销消息类型(作为参数)。
所以我想使用亚马逊 SNS 向客户发送 OTP,这是以下代码:
import boto3
client = boto3.client('sns')
response = client.publish(
PhoneNumber='some_phone_number',
Message='some_message'
)
Run Code Online (Sandbox Code Playgroud)
根据他们的文档,有 2 种消息类型:
1. 交易(时间关键交付)
2. 促销(非时间关键交付和成本效益)
我可以选择使用set_sms_attributes()如下设置默认消息属性:
client.set_sms_attributes(
attributes={"DefaultSMSType": "Transactional" | "Promotional" }
)
Run Code Online (Sandbox Code Playgroud)
我不想继续更改此参数,因为它们是默认值。我不能将消息类型动态指定为参数publish()
我检查了MessageAttributes但根据他们的文档,它不是指定消息类型,而是包含客户端在处理消息之前处理消息的元数据。
有没有办法即时切换消息类型,而不必使用set_sms_attributes?
TL; 博士
DocumentDB in another region <-------- ec2 instance in another region
access
Run Code Online (Sandbox Code Playgroud)
所以 AWS 推出了 MongoDB 作为服务,并称之为 DocumentDB。目前支持的地区有:
1. 弗吉尼亚北部
2. 俄亥俄
3. 俄勒冈
4. 爱尔兰
所以根据他们的文档,访问documentDB:
要与您的 Amazon DocumentDB 集群交互,您必须在您创建 Amazon DocumentDB 集群的同一 AWS 区域中的默认 VPC 中启动一个 Amazon Elastic Compute Cloud (Amazon EC2) 实例。
我这样做了,它按预期工作。
问题是,我如何从另一个 VPC、安全组和另一个区域上的另一个 ec2 实例访问它。
我试图访问它,这是例外:
pymongo.errors.ServerSelectionTimeoutError: ....
[Errno 113] No route to host
Run Code Online (Sandbox Code Playgroud)
注意:如果我在同一个区域和同一个安全组中启动一个 ec2 实例,则上述方法有效。
我可以做些什么来使用防火墙设置将另一个区域中的 ec2 实例列入白名单?
Scenerio:我正在尝试生成 s3 中对象的 Cloudfront 签名 URL。
步骤:
1. 在 s3 存储桶中创建一个对象并将其公开。
2. 创建一个指向该 s3 存储桶的 cloudfront 分发。
3.使用下面的代码生成签名的url
以下是从其文档生成 cloudfront 签名 url 的代码。
import datetime
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding
from botocore.signers import CloudFrontSigner
def rsa_signer(message):
with open('path/to/key.pem', 'rb') as key_file:
private_key = serialization.load_pem_private_key(
key_file.read(),
password=None,
backend=default_backend()
)
return private_key.sign(message, padding.PKCS1v15(), hashes.SHA1())
key_id = 'AKIAIOSFODNN7EXAMPLE'
url = 'https://d2949o5mkkp72v.cloudfront.net/hello.txt'
expire_date = datetime.datetime(2017, 1, 1)
cloudfront_signer = CloudFrontSigner(key_id, rsa_signer)
# …Run Code Online (Sandbox Code Playgroud) python amazon-web-services amazon-cloudfront pre-signed-url boto3
所以我试图通过云功能更新云 Firestore 中的日期时间字段,如下所示:
transaction.update(doc_ref, {'dateTimeField1': dateTimeValue})
Run Code Online (Sandbox Code Playgroud)
Google 将日期时间对象作为字符串发送到云函数的事件参数中,格式为%Y-%m-%dT%H:%M:%SZ或%Y-%m-%dT%H:%M:%S.%fZ。
例如:2019-01-25T15:25:03.881Z
我将其转换为 datetime 对象,如下所示:
try:
datetime_obj = datetime.datetime.strptime(datetime_obj, '%Y-%m-%dT%H:%M:%S.%fZ')
except:
datetime_obj = datetime.datetime.strptime(datetime_obj, '%Y-%m-%dT%H:%M:%SZ')
datetime_obj = datetime_obj.replace(tzinfo=timezone('UTC'))
Run Code Online (Sandbox Code Playgroud)
但是当我尝试执行操作时,我遇到了以下错误:
AttributeError: _nanosecond
追溯:
File "/env/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/batch.py", line 112, in update
reference._document_path, field_updates, option
File "/env/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/_helpers.py", line 822, in pbs_for_update
update_pb = extractor.get_update_pb(document_path)
File "/env/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/_helpers.py", line 459, in get_update_pb
name=document_path, fields=encode_dict(self.set_fields)
File "/env/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/_helpers.py", line 215, in encode_dict
return {key: encode_value(value) for key, value in six.iteritems(values_dict)}
File "/env/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/_helpers.py", line 215, in <dictcomp> …
python transactions firebase firebase-admin google-cloud-firestore
python ×4
python-3.x ×3
boto3 ×2
firebase ×2
transactions ×2
amazon-ec2 ×1
amazon-sns ×1
amazon-vpc ×1
apache-spark ×1
django ×1
firewall ×1
node.js ×1
pyspark ×1
redis ×1
security ×1