介紹:
MurmurHash 是一種非加密型哈希函數,適用於一般的哈希檢索操作。由Austin Appleby在2008年發明,並出現了多個變種,都已經發布到了公有領域(public domain)。與其它流行的哈希函數相比,對於規律性較強的key,MurmurHash的隨機分佈特徵表現更良好。
當前的版本是MurmurHash3,能夠產生出32-bit或128-bit哈希值。
較早的MurmurHash2能產生32-bit或64-bit哈希值。
應用場景:
比如按關鍵詞分桶,將數據相對平均分到N個桶。
演示:下面用openresty演示MurmurHash2的使用
1,安裝openresty(http://openresty.org/cn/installation.html)
2,安裝murmurHash2的方法一:
2.1,安裝LuaRocks(https://github.com/luarocks/luarocks/wiki/Installation-instructions-for-Unix)
wget https://luarocks.org/releases/luarocks-3.1.3.tar.gz
tar zxpf luarocks-3.1.3.tar.gz
cd luarocks-3.1.3
# 替換爲openresty的安裝路徑
./configure --prefix=/usr/local/openresty/luajit \
--with-lua=/usr/local/openresty/luajit/ \
--lua-suffix=jit \
--with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1
make build
make install
2.2,安裝murmurhash2
開源:https://github.com/bungle/lua-resty-murmurhash2
/usr/local/openresty/luajit/bin/luarocks install lua-resty-murmurhash2
#成功後,/usr/local/openresty/luajit/share/lua/5.1/resty目錄下會有murmurhash2.lua
3,安裝murmurHash2的方法二:
開源:https://github.com/bungle/lua-resty-murmurhash2
直接將murmurhash2.lua拷貝至/usr/local/openresty/luajit/share/lua/5.1/resty
4,測試代碼
location / {
content_by_lua_block {
local mmh2 = require "resty.murmurhash2"
local hash = mmh2("test")
ngx.say(hash)
}
# 輸出 403862830
原文出自:https://blog.csdn.net/daiyudong2020/article/details/98114812
End;