使用 rethinkdbdash 正确链接 RethinkDB table 和对象创建命令
Properly chaining RethinkDB table and object creation commands with rethinkdbdash
我正在处理文本数据流,我事先不知道其值的分布是什么,但我知道每个值都是这样的:
{
"datetime": "1986-11-03T08:30:00-07:00",
"word": "wordA",
"value": "someValue"
}
我试图根据它的值将它存储到 RethinkDB 对象中,其中对象如下所示:
{
"bucketId": "1",
"bucketValues": {
"wordA": [
{"datetime": "1986-11-03T08:30:00-07:00"},
{"datetime": "1986-11-03T08:30:00-07:00"}
],
"wordB": [
{"datetime": "1986-11-03T08:30:00-07:00"},
{"datetime": "1986-11-03T08:30:00-07:00"}
]
}
}
目的是最终统计每个词在每个桶中出现的次数。
因为我要处理大约一百万个桶,并且事先不知道这些词,所以计划是动态创建这个对象。然而,我是 RethinkDB 的新手,我已尽力做到这一点,我不会尝试将 word
键添加到尚不存在的存储桶中,但我不是完全确定我是否遵循此处的最佳实践,如下链接命令(请注意,我 运行 在 Node.js 服务器上使用 :
var bucketId = "someId";
var word = "someWordValue"
r.do(r.table("buckets").get(bucketId), function(result) {
return r.branch(
// If the bucket doesn't exist
result.eq(null),
// Create it
r.table("buckets").insert({
"id": bucketId,
"bucketValues" : {}
}),
// Else do nothing
"Bucket already exists"
);
})
.run()
.then(function(result) {
console.log(result);
r.table("buckets").get(bucketId)
.do(function(bucket) {
return r.branch(
// if the word already exists
bucket("bucketValues").keys().contains(word),
// Just append to it (code not implemented yet)
"Word already exists",
// Else create the word and append it
r.table("buckets").get(bucketId).update(
{"bucketValues": r.object(word, [/*Put the timestamp here*/])}
)
);
})
.run()
.then(function(result) {
console.log(result);
});
});
我需要在这里执行两次 运行,还是我偏离了您应该如何正确地将事物与 RethinkDB 链接在一起?在我深入了解之前,我只是想确保我没有按照 wrong/hard 的方式进行操作。
你不必多次执行run
,这取决于你想要什么。基本上,run()
结束链并向服务器发送查询。所以我们做了所有的事情来构建查询,并以 run()
结束它来执行它。如果你使用 run()
两次,这意味着它被发送到服务器 2 次。
所以如果我们可以只使用 RethinkDB 函数完成所有处理,我们只需要调用一次 运行。但是,如果我们想要某种 post-processing 数据,使用客户端,那么我们别无选择。通常我尝试使用 RethinkDB 进行所有处理:通过控制结构、循环和匿名函数,我们可以走得很远而无需让客户端执行一些逻辑。
对于您的情况,可以使用官方驱动程序使用 NodeJS 重写查询:
var r = require('rethinkdb')
var bucketId = "someId2";
var word = "someWordValue2";
r.connect()
.then((conn) => {
r.table("buckets").insert({
"id": bucketId,
"bucketValues" : {}
})
.do((result) => {
// We don't care about result at all
// We just want to ensure it's there
return r.table('buckets').get(bucketId)
.update(function(bucket) {
return {
'bucketValues': r.object(
word,
bucket('bucketValues')(word).default([])
.append(r.now()))
}
})
})
.run(conn)
.then((result) => { conn.close() })
})
我正在处理文本数据流,我事先不知道其值的分布是什么,但我知道每个值都是这样的:
{
"datetime": "1986-11-03T08:30:00-07:00",
"word": "wordA",
"value": "someValue"
}
我试图根据它的值将它存储到 RethinkDB 对象中,其中对象如下所示:
{
"bucketId": "1",
"bucketValues": {
"wordA": [
{"datetime": "1986-11-03T08:30:00-07:00"},
{"datetime": "1986-11-03T08:30:00-07:00"}
],
"wordB": [
{"datetime": "1986-11-03T08:30:00-07:00"},
{"datetime": "1986-11-03T08:30:00-07:00"}
]
}
}
目的是最终统计每个词在每个桶中出现的次数。
因为我要处理大约一百万个桶,并且事先不知道这些词,所以计划是动态创建这个对象。然而,我是 RethinkDB 的新手,我已尽力做到这一点,我不会尝试将 word
键添加到尚不存在的存储桶中,但我不是完全确定我是否遵循此处的最佳实践,如下链接命令(请注意,我 运行 在 Node.js 服务器上使用 :
var bucketId = "someId";
var word = "someWordValue"
r.do(r.table("buckets").get(bucketId), function(result) {
return r.branch(
// If the bucket doesn't exist
result.eq(null),
// Create it
r.table("buckets").insert({
"id": bucketId,
"bucketValues" : {}
}),
// Else do nothing
"Bucket already exists"
);
})
.run()
.then(function(result) {
console.log(result);
r.table("buckets").get(bucketId)
.do(function(bucket) {
return r.branch(
// if the word already exists
bucket("bucketValues").keys().contains(word),
// Just append to it (code not implemented yet)
"Word already exists",
// Else create the word and append it
r.table("buckets").get(bucketId).update(
{"bucketValues": r.object(word, [/*Put the timestamp here*/])}
)
);
})
.run()
.then(function(result) {
console.log(result);
});
});
我需要在这里执行两次 运行,还是我偏离了您应该如何正确地将事物与 RethinkDB 链接在一起?在我深入了解之前,我只是想确保我没有按照 wrong/hard 的方式进行操作。
你不必多次执行run
,这取决于你想要什么。基本上,run()
结束链并向服务器发送查询。所以我们做了所有的事情来构建查询,并以 run()
结束它来执行它。如果你使用 run()
两次,这意味着它被发送到服务器 2 次。
所以如果我们可以只使用 RethinkDB 函数完成所有处理,我们只需要调用一次 运行。但是,如果我们想要某种 post-processing 数据,使用客户端,那么我们别无选择。通常我尝试使用 RethinkDB 进行所有处理:通过控制结构、循环和匿名函数,我们可以走得很远而无需让客户端执行一些逻辑。
对于您的情况,可以使用官方驱动程序使用 NodeJS 重写查询:
var r = require('rethinkdb')
var bucketId = "someId2";
var word = "someWordValue2";
r.connect()
.then((conn) => {
r.table("buckets").insert({
"id": bucketId,
"bucketValues" : {}
})
.do((result) => {
// We don't care about result at all
// We just want to ensure it's there
return r.table('buckets').get(bucketId)
.update(function(bucket) {
return {
'bucketValues': r.object(
word,
bucket('bucketValues')(word).default([])
.append(r.now()))
}
})
})
.run(conn)
.then((result) => { conn.close() })
})