如何在 Aerospike Node.js 客户端中获取主键

4
我正在尝试从Aerospike获取所有记录以及主键。我尝试使用以下代码使用client.query功能:
var query = client.query(aerospikeDBParams.dbName,"testRecords");
var stream = query.execute();

通过此代码,我可以获取除主键之外的所有字段。如何同时获取主键和其他字段呢?
提前感谢您的帮助。
1个回答

3
你需要首先将主键与记录一起存储。客户端和服务器使用其摘要定位记录,客户端使用(命名空间,集,主键)信息进行哈希处理。键写入策略的默认值为Aerospike.policy.key.DIGEST。你需要将其明确设置为Aerospike.policy.key.SEND

请参阅Aerospike:module文档。其中包含以下示例:

// global policy, applied to all commands that do not override it
var config = {
  policies: {
    timeout: 100,
    retry: Aerospike.policy.retry.ONCE
  }
}

Aerospike.connect(config, (error, client) => {
  if (error) throw error

  var key = new Aerospike.Key('test', 'demo', 'k1')
  var record = {i: 1234}

  // override policy for put command
  var policy = {
    exists: Aerospike.policy.exists.CREATE,
    key: Aerospike.policy.key.SEND
  }

  client.put(key, record, {}, policy, (error) => {
    if (error && error.code === Aerospike.status.AEROSPIKE_ERR_RECORD_EXISTS) {
      console.info('record already exists')
    } else if (error) {
      throw error
    }
    client.close()
  })
})

当密钥与记录一起存储时,查询将返回它们。

1
这是将关键信息存储到服务器的方法。但查询方法返回的是recordStream,数据事件仅返回(bins,meta),我找不到‘key’。然而,在Java客户端中使用也没有问题。 - sel-fish
1
@sel-fish,你是对的:从v2.0.3版本开始,客户端的记录流data事件不再包含记录键。这是与v1客户端相比的一个回归,我将在下一个补丁发布中修复这个问题。 - Jan Hecking

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接