从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/

10-09 06:20
查看更多