{"id":253856,"date":"2022-10-06T08:06:33","date_gmt":"2022-10-06T00:06:33","guid":{"rendered":"https:\/\/gulass.cn\/?p=253856"},"modified":"2022-09-28T11:06:57","modified_gmt":"2022-09-28T03:06:57","slug":"redis-hyperloglog-linux","status":"publish","type":"post","link":"https:\/\/gulass.cn\/redis-hyperloglog-linux.html","title":{"rendered":"\u7b80\u5355\u4ecb\u7ecdRedis\u4e2dHyperLogLog\u7684\u4f7f\u7528\u8be6\u60c5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86Redis\u4e2dHyperLogLog\u7684\u4f7f\u7528\u8be6\u60c5\uff0c\u6587\u7ae0\u56f4\u7ed5\u4e3b\u9898\u5c55\u5f00\u8be6\u7ec6\u7684\u5185\u5bb9\u4ecb\u7ecd\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u524d\u8a00<\/strong><\/div>\n

HyperLogLog \uff0c\u57fa\u6570\u7edf\u8ba1\uff1b<\/p>\n

\u90a3\u4ec0\u4e48\u662f\u57fa\u6570\uff1f<\/p>\n

\u6bd4\u5982\u6709\u4e24\u4e2a\u6570\u7ec4<\/p>\n

\u6570\u7ec4A = [1,2,3,4,5];<\/p>\n

\u6570\u7ec4B = [3,4,5,6,7];<\/p>\n

\u8fd9\u65f6\u5019\u57fa\u6570\u5c31\u662f [1,2,3,4,5,6,7]\uff0c\u603b\u5171\u67097\u4e2a\u6570\uff1b<\/p>\n

\u5c31\u662f\u53bb\u91cd\u4e4b\u540e\u7684\u6570\u636e\uff1b<\/p>\n

HyperLogLog \u5c31\u662f\u7528\u6765\u505a\u53bb\u91cd\u590d\u7edf\u8ba1\u7684\uff1b<\/p>\n

bitmap \u5728\u505a\u7edf\u8ba1\u65f6\uff0c\u867d\u7136\u4f7f\u7528\u7684\u662f bit \u6765\u505a\u8bb0\u5f55\uff0c\u5df2\u7ecf\u5f88\u8282\u7701\u7a7a\u95f4\u4e86\uff1b<\/p>\n

\u4f46\u662f\u5728\u968f\u7740\u6570\u636e\u91cf\u5feb\u901f\u589e\u957f\u7684\u60c5\u51b5\u4e0b\uff0cbitmap \u4e5f\u662f\u5f88\u5360\u5185\u5b58\u7a7a\u95f4\u7684\uff1b<\/p>\n

\u800c HyperLogLog \u5c31\u4e0d\u540c\u4e86\uff0cHyperLogLog \u7684\u6bcf\u4e2a key \u53ea\u5360\u7528 12kb \u7684\u5185\u5b58\uff0c<\/p>\n

\u5c31\u53ef\u4ee5\u7edf\u8ba1 2\u768464 \u6b21\u65b9\u4e2a\u57fa\u6570\uff1b<\/p>\n

\u800c\u4e14\u4e0d\u4f1a\u968f\u7740\u6570\u636e\u91cf\u7684\u589e\u591a\u800c\u53d8\u5927\uff0c\u5c31\u662f\u56fa\u5b9a\u7684 12kb\uff1b<\/p>\n

\u8fd9\u662f\u56e0\u4e3a HyperLogLog \u6bcf\u6b21\u53ea\u4f1a\u6839\u636e\u6dfb\u52a0\u7684\u6570\u636e\u53bb\u8ba1\u7b97\u57fa\u6570\uff0c\u800c\u4e0d\u4fdd\u5b58\u6dfb\u52a0\u7684\u6570\u636e\u672c\u8eab\uff1b<\/p>\n

\u4f46\u8fd9\u4e5f\u9020\u6210\u4e86\u4e00\u4e2a\u95ee\u9898\uff0c\u5c31\u662f HyperLogLog \u57fa\u6570\u7edf\u8ba1\u7684\u51c6\u786e\u7387\u4e0d\u662f100%\uff0c\u4f1a\u6709 0.81% \u7684\u8bef\u5dee\uff1b<\/p>\n

\u4f46\u5bf9\u4e8e\u4ebf\u7ea7\u6570\u636e\u7684\u7edf\u8ba1\uff0c\u8fd9\u70b9\u8bef\u5dee\u4f3c\u4e4e\u53ef\u4ee5\u5ffd\u7565\u4e0d\u8ba1\uff1b<\/p>\n

\u540c\u6837\u7684\uff0cbitmap \u8bb0\u5f55\u6570\u636e\u672c\u8eab\uff0c\u5b83\u7684\u8ba1\u7b97\u662f\u7cbe\u786e\u7684\uff1b<\/p>\n

\u6240\u6709\uff0c\u4e5f\u53ef\u4ee5\u8bf4 HyperLogLog \u662f\u4e00\u79cd\u6982\u7387\u8ba1\u7b97\uff0c\u4f46\u662f\u8bef\u5dee\u662f\u5f88\u5c0f\u7684\u3002<\/p>\n

\u8fd9\u662freidis\u4f5c\u8005\u5199\u7684\u5173\u4e8eHyperLogLog\u7684\u6587\u7ae0<\/p>\n

HyperLogLog \u7684\u4f7f\u7528\u4e5f\u975e\u5e38\u7b80\u5355\uff0c\u5c313\u4e2a\u64cd\u4f5capi<\/p>\n

\u6dfb\u52a0\u5143\u7d20<\/strong><\/div>\n
pfadd key value [value ...]<\/pre>\n
pfadd 20220628:uv ip1 ip2 ip3\r\npfadd 20220629:uv ip1 ip2 ip3 ip4 ip5\r\npfadd 20220630:uv ip2 ip4 ip5 ip6 ip7 ip8<\/pre>\n

\u91cd\u590d\u6570\u636e\u4e0d\u4f1a\u88ab\u4fdd\u5b58\uff0c\u8fd4\u56de\u7684\u662f0<\/p>\n

> pfadd 20220628:uv ip1 ip2 ip3\r\n1\r\n> pfadd 20220628:uv ip1 ip2 ip3\r\n0<\/pre>\n

\u83b7\u53d6 HyperLogLog \u7684\u57fa\u6570\u4f30\u7b97\u503c<\/p>\n

PFCOUNT key [key ...]<\/pre>\n
> pfcount 20220628:uv 20220629:uv\r\n5<\/pre>\n

\u5408\u5e76\u5f97\u51fa\u7684\u4e0d\u4f1a\u88ab\u50a8\u5b58\uff0c\u4f7f\u7528\u4e4b\u540e\u5c31\u4f1a\u88ab\u5220\u6389<\/p>\n

\u5408\u5e76\u591a\u4e2akey<\/p>\n

pfmerge destkey key [key ...]<\/pre>\n
> pfmerge result 20220628:uv 20220629:uv 20220630:uv\r\nOK<\/pre>\n

pfmerge \u5408\u5e76\u591a\u4e2akey\u540e\uff0c\u4f1a\u751f\u6210\u4e00\u6761 result \u6570\u636e<\/p>\n

\u7136\u540e\u518d\u5bf9 result \u505a pfcount \u64cd\u4f5c<\/p>\n

> pfcount result\r\n8<\/pre>\n

\u8fd9\u65f6\u5019\u62ff\u5230\u7684\u503c\u5c31\u662f \u4e0a\u97623\u4e2akey \u8ba1\u7b97\u51fa\u6765\u7684\u57fa\u6570\u3002<\/p>\n

HyperLogLog \u5728 spring \u91cc\u7684\u4f7f\u7528\u4e5f\u975e\u5e38\u7b80\u5355\uff0c<\/p>\n

pfadd \u5bf9\u5e94\u7684\u662f .add() \u64cd\u4f5c\uff1b<\/p>\n

pfcount \u5bf9\u5e94\u7684\u662f .size() \u64cd\u4f5c\uff1b<\/p>\n

pfmerge \u5bf9\u5e94\u7684\u662f .union() \u64cd\u4f5c\uff1b<\/p>\n

\u4e0b\u9762\u662f\u4f7f\u7528\u7684 Demo<\/p>\n

import org.junit.jupiter.api.Test;\r\nimport org.junit.runner.RunWith;\r\nimport org.springframework.boot.test.context.SpringBootTest;\r\nimport org.springframework.data.redis.core.HyperLogLogOperations;\r\nimport org.springframework.data.redis.core.RedisTemplate;\r\nimport org.springframework.test.context.junit4.SpringRunner;\r\n \r\nimport javax.annotation.Resource;\r\n \r\n@SpringBootTest\r\n@RunWith(SpringRunner.class)\r\nclass RedisTest {\r\n     \r\n    @Resource\r\n    private RedisTemplate redisTemplate;\r\n \r\n    @Test\r\n    void hyperLogLogTest() {\r\n        HyperLogLogOperations hyperLogLogOperations = redisTemplate.opsForHyperLogLog();\r\n        \/\/ \u6dfb\u52a0\u5143\u7d20\r\n        Long add = hyperLogLogOperations.add(\"20220628:uv\", \"ip1\", \"ip2\", \"ip3\");\r\n        System.out.println(\"add : \" + add);\r\n \r\n        hyperLogLogOperations.add(\"20220629:uv\", \"ip1\", \"ip2\", \"ip3\", \"ip4\", \"ip5\");\r\n        hyperLogLogOperations.add(\"20220630:uv\", \"ip2\", \"ip4\", \"ip5\", \"ip6\", \"ip7\", \"ip8\");\r\n \r\n        \/\/ \u83b7\u53d6\u5143\u7d20\u57fa\u6570\r\n        Long size = hyperLogLogOperations.size(\"20220628:uv\", \"20220629:uv\");\r\n        System.out.println(\"size : \" + size);\r\n \r\n        \/\/ \u5408\u5e76\u591a\u4e2a\u5143\u7d20\r\n        Long result = hyperLogLogOperations.union(\"unionResult\", \"20220628:uv\", \"20220629:uv\", \"20220630:uv\");\r\n        System.out.println(\"result : \" + result);\r\n        Long unionResult = hyperLogLogOperations.size(\"unionResult\");\r\n        System.out.println(\"unionResult : \" + unionResult);\r\n    }\r\n}<\/pre>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8eRedis\u4e2dHyperLogLog\u7684\u4f7f\u7528\u8be6\u60c5\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"

HyperLogLog \uff0c\u57fa\u6570\u7edf\u8ba1\uff1b \u90a3\u4ec0\u4e48\u662f\u57fa\u6570\uff1f \u6bd4\u5982\u6709\u4e24\u4e2a\u6570\u7ec4 \u6570\u7ec4A = [1,2,3,4,5]; \u6570 […]<\/p>\n","protected":false},"author":668,"featured_media":7058,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-253856","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/253856","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=253856"}],"version-history":[{"count":4,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/253856\/revisions"}],"predecessor-version":[{"id":253860,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/253856\/revisions\/253860"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/7058"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=253856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=253856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=253856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}