怎么让Aerospike中的SET显示PK?

Aerospike是一款性能强劲的NoSql数据库,以低延迟和高吞吐量而闻名。在SET(可以理解为表)中,一般来说PK作为主键,是不能被查询到的,但是有时候我们需要知道PK的值,这个如何解决呢?
看下com.aerospike.client.Key的源码,部分摘录如下:

/**
 * Unique record identifier. Records can be identified using a specified namespace,
 * an optional set name, and a user defined key which must be unique within a set.
 * Records can also be identified by namespace/digest which is the combination used 
 * on the server.
 */
public final class Key {
  /**
   * Namespace. Equivalent to database name.
   */
  public final String namespace;
  
  /**
   * Optional set name. Equivalent to database table.
   */
  public final String setName;
  
  /**
   * Unique server hash value generated from set name and user key.
   */
  public final byte[] digest;
  
  /**
   * Original user key. This key is immediately converted to a hash digest.
   * This key is not used or returned by the server by default. If the user key needs 
   * to persist on the server, use one of the following methods: 
   * <ul>
   * <li>Set "WritePolicy.sendKey" to true. In this case, the key will be sent to the server for storage on writes
   * and retrieved on multi-record scans and queries.</li>
   * <li>Explicitly store and retrieve the key in a bin.</li>
   * </ul>
   */
  public final Value userKey;
}

 

可以看到,Key有一个byte[] digest的数组对象,是根据set name 和key(PK)hash后的结果,但是这个是不可逆的。继续往下看能看到有个userKey,看解释是Original 的user key。但是默认它的值默认是null,除非显式的设置WritePolicy.sendKey=true,这样才会将PK显式的存储在Key中。

 

此条目发表在技术, 未分类分类目录,贴了标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。