从 node.js 连接到本地运行的 bigtable 模拟器,我需要能够确定特定行是否存在。虽然这对于确实存在的行按预期工作,但对于表中未包含的键,似乎 row.exits()
无限期挂起。
这是一个最小示例:
const { Bigtable } = require('@google-cloud/bigtable');
const btInstance = Bigtable().instance('test');
const table = btInstance.table('testTable');
const [tableExists] = await table.exists();
if (!exists) {
await table.create();
await table.createFamily('testFamily');
}
const row = await table.row('testkey');
console.log('Table existence ensured. Checking if row exists...');
// fine till here...
const [rowExists] = await row.exists();
// may never get here, if connected to the emulator and row *doesn't* exist
if (!rowExists) {
console.log('Row doesn\'t exist!');
} else {
console.log('Row already exists.');
}
当连接到 Bigtable 的实时 Google Cloud 实例时,代码运行良好:报告不存在的行。但是,当连接到模拟器时,如果行不存在,则代码永远不会经过 row.exists()
调用;也没有抛出任何错误。
我想要做的事情有什么问题吗,或者是否可以采取某种解决方法(可能是检查给定键是否存在行的不同方法)?
最佳答案
tableExists 变量似乎有拼写错误。使用以下示例,我能够重现该问题。请关注此处的更新: https://github.com/googleapis/nodejs-bigtable/issues/555
mkdir cbt-hang
cd cbt-hang
npm init --yes
npm install @google-cloud/bigtable
cat <<- EOF > index.js
const Bigtable = require('@google-cloud/bigtable');
async function main() {
console.log("getting instance" )
const btInstance = Bigtable().instance('test');
console.log("getting table" )
const table = btInstance.table('testTable');
console.log("checking table existence" )
const [tableExists] = await table.exists();
if (!tableExists) {
console.log("creating table" )
await table.create();
console.log("creating family" )
await table.createFamily('testFamily');
}
console.log("getting row" )
const row = await table.row('testkey');
console.log("checking row exists")
const [rowExists] = await row.exists();
if (!rowExists) {
console.log('Row doesn\'t exist!');
} else {
console.log('Row already exists.');
}
}
main().catch(e => console.log(e));
EOF
gcloud beta emulators bigtable start --host-port=localhost:8086 &
BIGTABLE_EMULATOR_HOST=localhost:8086 node index.js
kill %1
关于node.js - Bigtable 尝试确定行是否存在,模拟器因行不存在而挂起(在 row.exists() 上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57205288/