hash函数

hash()函数返回参数的哈希值。其参数可以是数字、字符串、列表、布尔值、NULL等类型的值,或者计算结果为这些类型的表达式。

hash()函数采用MurmurHash2算法,种子(seed)为0xc70f6907UL。用户可以在MurmurHash2.h中查看其源代码。

在Java中的调用方式如下:

  1. MurmurHash2.hash64("to_be_hashed".getBytes(),"to_be_hashed".getBytes().length, 0xc70f6907)

历史版本兼容性

nGQL 1.0不支持字符串类型的VID,一种常用的处理方式是用hash函数获取字符串的哈希值,然后将该值设置为VID。但nGQL 2.0同时支持了字符串和整数类型的VID,所以无需再使用这种方式设置VID。

计算数字的哈希值

  1. nebula> YIELD hash(-123);
  2. +--------------+
  3. | hash(-(123)) |
  4. +--------------+
  5. | -123 |
  6. +--------------+

计算字符串的哈希值

  1. nebula> YIELD hash("to_be_hashed");
  2. +----------------------+
  3. | hash(to_be_hashed) |
  4. +----------------------+
  5. | -1098333533029391540 |
  6. +----------------------+

计算列表的哈希值

  1. nebula> YIELD hash([1,2,3]);
  2. +----------------+
  3. | hash([1,2,3]) |
  4. +----------------+
  5. | 11093822460243 |
  6. +----------------+

计算布尔值的哈希值

  1. nebula> YIELD hash(true);
  2. +------------+
  3. | hash(true) |
  4. +------------+
  5. | 1 |
  6. +------------+
  7. nebula> YIELD hash(false);
  8. +-------------+
  9. | hash(false) |
  10. +-------------+
  11. | 0 |
  12. +-------------+

计算NULL的哈希值

  1. nebula> YIELD hash(NULL);
  2. +------------+
  3. | hash(NULL) |
  4. +------------+
  5. | -1 |
  6. +------------+

计算表达式的哈希值

  1. nebula> YIELD hash(toLower("HELLO NEBULA"));
  2. +-------------------------------+
  3. | hash(toLower("HELLO NEBULA")) |
  4. +-------------------------------+
  5. | -8481157362655072082 |
  6. +-------------------------------+

最后更新: August 13, 2021