{"id":43972,"date":"2024-12-01T11:27:55","date_gmt":"2024-12-01T03:27:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/43972\/"},"modified":"2024-12-01T11:27:55","modified_gmt":"2024-12-01T03:27:55","slug":"redis%e4%b8%adhyperloglog%e7%9a%84%e4%bd%bf%e7%94%a8%e8%af%a6%e6%83%85","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/43972\/","title":{"rendered":"Redis\u4e2dHyperLogLog\u7684\u4f7f\u7528\u8be6\u60c5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u4e2dHyperLogLog\u7684\u4f7f\u7528\u8be6\u60c5<\/h1>\n<p><span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u600e\u4e48\u5165\u95e8\u6570\u636e\u5e93\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762golang\u5b66\u4e60\u7f51\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aRedis\u4e2dHyperLogLog\u7684\u4f7f\u7528\u8be6\u60c5\u300b\uff0c\u6d89\u53ca\u5230RedisHyperLogLog\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p>\u90a3\u4ec0\u4e48\u662f\u57fa\u6570\uff1f<\/p>\n<p>\u6bd4\u5982\u6709\u4e24\u4e2a\u6570\u7ec4<\/p>\n<p>\u6570\u7ec4A = [1,2,3,4,5];<\/p>\n<p>\u6570\u7ec4B = [3,4,5,6,7];<\/p>\n<p>\u8fd9\u65f6\u5019\u57fa\u6570\u5c31\u662f&nbsp;<strong>[1,2,3,4,5,6,7]<\/strong>\uff0c\u603b\u5171\u67097\u4e2a\u6570\uff1b<\/p>\n<p>\u5c31\u662f\u53bb\u91cd\u4e4b\u540e\u7684\u6570\u636e\uff1b<\/p>\n<p><strong>HyperLogLog<\/strong>&nbsp;\u5c31\u662f\u7528\u6765\u505a\u53bb\u91cd\u590d\u7edf\u8ba1\u7684\uff1b<\/p>\n<p><strong>bitmap<\/strong>&nbsp;\u5728\u505a\u7edf\u8ba1\u65f6\uff0c\u867d\u7136\u4f7f\u7528\u7684\u662f&nbsp;<strong>bit<\/strong>&nbsp;\u6765\u505a\u8bb0\u5f55\uff0c\u5df2\u7ecf\u5f88\u8282\u7701\u7a7a\u95f4\u4e86\uff1b<\/p>\n<p>\u4f46\u662f\u5728\u968f\u7740\u6570\u636e\u91cf\u5feb\u901f\u589e\u957f\u7684\u60c5\u51b5\u4e0b\uff0c<strong>bitmap<\/strong>&nbsp;\u4e5f\u662f\u5f88\u5360\u5185\u5b58\u7a7a\u95f4\u7684\uff1b<\/p>\n<p>\u800c&nbsp;<strong>HyperLogLog<\/strong>&nbsp;\u5c31\u4e0d\u540c\u4e86\uff0c<strong>HyperLogLog<\/strong>&nbsp;\u7684\u6bcf\u4e2a&nbsp;<strong>key<\/strong>&nbsp;\u53ea\u5360\u7528&nbsp;<strong>12kb<\/strong>&nbsp;\u7684\u5185\u5b58\uff0c<\/p>\n<p>\u5c31\u53ef\u4ee5\u7edf\u8ba1&nbsp;<strong>2\u768464<\/strong>&nbsp;\u6b21\u65b9\u4e2a\u57fa\u6570\uff1b<\/p>\n<p>\u800c\u4e14\u4e0d\u4f1a\u968f\u7740\u6570\u636e\u91cf\u7684\u589e\u591a\u800c\u53d8\u5927\uff0c\u5c31\u662f\u56fa\u5b9a\u7684&nbsp;<strong>12kb<\/strong>\uff1b<\/p>\n<p>\u8fd9\u662f\u56e0\u4e3a&nbsp;<strong>HyperLogLog<\/strong>&nbsp;\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<p>\u4f46\u8fd9\u4e5f\u9020\u6210\u4e86\u4e00\u4e2a\u95ee\u9898\uff0c\u5c31\u662f&nbsp;<strong>HyperLogLog<\/strong>&nbsp;\u57fa\u6570\u7edf\u8ba1\u7684\u51c6\u786e\u7387\u4e0d\u662f100%\uff0c\u4f1a\u6709&nbsp;<strong>0.81%<\/strong>&nbsp;\u7684\u8bef\u5dee\uff1b<\/p>\n<p>\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<p>\u540c\u6837\u7684\uff0c<strong>bitmap<\/strong>&nbsp;\u8bb0\u5f55\u6570\u636e\u672c\u8eab\uff0c\u5b83\u7684\u8ba1\u7b97\u662f\u7cbe\u786e\u7684\uff1b<\/p>\n<p>\u6240\u6709\uff0c\u4e5f\u53ef\u4ee5\u8bf4&nbsp;<strong>HyperLogLog<\/strong>&nbsp;\u662f\u4e00\u79cd\u6982\u7387\u8ba1\u7b97\uff0c\u4f46\u662f\u8bef\u5dee\u662f\u5f88\u5c0f\u7684\u3002<\/p>\n<\/p>\n<p><strong>HyperLogLog<\/strong>&nbsp;\u7684\u4f7f\u7528\u4e5f\u975e\u5e38\u7b80\u5355\uff0c\u5c313\u4e2a\u64cd\u4f5capi<\/p>\n<h2>\u6dfb\u52a0\u5143\u7d20<\/h2>\n<pre>pfadd key value [value ...]<\/pre>\n<pre>pfadd 20220628:uv ip1 ip2 ip3\npfadd 20220629:uv ip1 ip2 ip3 ip4 ip5\npfadd 20220630:uv ip2 ip4 ip5 ip6 ip7 ip8<\/pre>\n<p>\u91cd\u590d\u6570\u636e\u4e0d\u4f1a\u88ab\u4fdd\u5b58\uff0c\u8fd4\u56de\u7684\u662f0<\/p>\n<pre>&gt; pfadd 20220628:uv ip1 ip2 ip3\n1\n&gt; pfadd 20220628:uv ip1 ip2 ip3\n0<\/pre>\n<ul>\n<li>\u83b7\u53d6 HyperLogLog \u7684\u57fa\u6570\u4f30\u7b97\u503c<\/li>\n<\/ul>\n<pre>PFCOUNT key [key ...]<\/pre>\n<pre>&gt; pfcount 20220628:uv 20220629:uv\n5<\/pre>\n<p>\u5408\u5e76\u5f97\u51fa\u7684\u4e0d\u4f1a\u88ab\u50a8\u5b58\uff0c\u4f7f\u7528\u4e4b\u540e\u5c31\u4f1a\u88ab\u5220\u6389<\/p>\n<ul>\n<li>\u5408\u5e76\u591a\u4e2akey<\/li>\n<\/ul>\n<pre>pfmerge destkey key [key ...]<\/pre>\n<pre>&gt; pfmerge result 20220628:uv 20220629:uv 20220630:uv\nOK<\/pre>\n<p><strong>pfmerge<\/strong>&nbsp;\u5408\u5e76\u591a\u4e2akey\u540e\uff0c\u4f1a\u751f\u6210\u4e00\u6761 result \u6570\u636e<\/p>\n<p>\u7136\u540e\u518d\u5bf9 result \u505a pfcount \u64cd\u4f5c<\/p>\n<pre>&gt; pfcount result\n8<\/pre>\n<p>\u8fd9\u65f6\u5019\u62ff\u5230\u7684\u503c\u5c31\u662f \u4e0a\u97623\u4e2akey \u8ba1\u7b97\u51fa\u6765\u7684\u57fa\u6570\u3002<\/p>\n<p><strong>HyperLogLog<\/strong>&nbsp;\u5728&nbsp;<strong>spring<\/strong>&nbsp;\u91cc\u7684\u4f7f\u7528\u4e5f\u975e\u5e38\u7b80\u5355\uff0c<\/p>\n<p><strong>pfadd<\/strong>&nbsp;\u5bf9\u5e94\u7684\u662f&nbsp;<strong>.add()<\/strong>&nbsp;\u64cd\u4f5c\uff1b<\/p>\n<p><strong>pfcount<\/strong>&nbsp;\u5bf9\u5e94\u7684\u662f&nbsp;<strong>.size()<\/strong>&nbsp;\u64cd\u4f5c\uff1b<\/p>\n<p><strong>pfmerge<\/strong>&nbsp;\u5bf9\u5e94\u7684\u662f&nbsp;<strong>.union()<\/strong>&nbsp;\u64cd\u4f5c\uff1b<\/p>\n<p>\u4e0b\u9762\u662f\u4f7f\u7528\u7684&nbsp;<strong>Demo<\/strong><\/p>\n<pre>import org.junit.jupiter.api.Test;\nimport org.junit.runner.RunWith;\nimport org.springframework.boot.test.context.SpringBootTest;\nimport org.springframework.data.redis.core.HyperLogLogOperations;\nimport org.springframework.data.redis.core.RedisTemplate;\nimport org.springframework.test.context.junit4.SpringRunner;\n\nimport javax.annotation.Resource;\n\n@SpringBootTest\n@RunWith(SpringRunner.class)\nclass RedisTest {\n    \n    @Resource\n    private RedisTemplate\n   \n     redisTemplate;\n\n    @Test\n    void hyperLogLogTest() {\n        HyperLogLogOperations\n    \n      hyperLogLogOperations = redisTemplate.opsForHyperLogLog(); \/\/ \u6dfb\u52a0\u5143\u7d20 Long add = hyperLogLogOperations.add(\"20220628:uv\", \"ip1\", \"ip2\", \"ip3\"); System.out.println(\"add : \" + add); hyperLogLogOperations.add(\"20220629:uv\", \"ip1\", \"ip2\", \"ip3\", \"ip4\", \"ip5\"); hyperLogLogOperations.add(\"20220630:uv\", \"ip2\", \"ip4\", \"ip5\", \"ip6\", \"ip7\", \"ip8\"); \/\/ \u83b7\u53d6\u5143\u7d20\u57fa\u6570 Long size = hyperLogLogOperations.size(\"20220628:uv\", \"20220629:uv\"); System.out.println(\"size : \" + size); \/\/ \u5408\u5e76\u591a\u4e2a\u5143\u7d20 Long result = hyperLogLogOperations.union(\"unionResult\", \"20220628:uv\", \"20220629:uv\", \"20220630:uv\"); System.out.println(\"result : \" + result); Long unionResult = hyperLogLogOperations.size(\"unionResult\"); System.out.println(\"unionResult : \" + unionResult); } }\n    \n   <\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300aRedis\u4e2dHyperLogLog\u7684\u4f7f\u7528\u8be6\u60c5\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8eredis\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\uff01<\/p>\n<p>   \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1a\u811a\u672c\u4e4b\u5bb6 \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664  <\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u751f\u4ea7redisson\u5ef6\u65f6\u961f\u5217\u4e0d\u6d88\u8d39\u95ee\u9898\u6392\u67e5\u89e3\u51b3\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u8be6\u89e3RedisStream\u505a\u6d88\u606f\u961f\u5217\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u4e2dHyperLogLog\u7684\u4f7f\u7528\u8be6\u60c5 0\u6d4f\u89c8 \u6536\u85cf \u600e\u4e48\u5165\u95e8\u6570\u636e\u5e93\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762golang\u5b66\u4e60\u7f51\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aRedis\u4e2dHyperLogLog\u7684\u4f7f\u7528\u8be6\u60c5\u300b\uff0c\u6d89\u53ca\u5230RedisHyperLogLog\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b \u90a3\u4ec0\u4e48\u662f\u57fa\u6570\uff1f \u6bd4\u5982\u6709\u4e24\u4e2a\u6570\u7ec4 \u6570\u7ec4A = [1,2,3,4,5]; \u6570\u7ec4B = [3,4,5,6,7]; \u8fd9\u65f6\u5019\u57fa\u6570\u5c31\u662f&nbsp;[1,2,3,4,5,6,7]\uff0c\u603b\u5171\u67097\u4e2a\u6570\uff1b \u5c31\u662f\u53bb\u91cd\u4e4b\u540e\u7684\u6570\u636e\uff1b HyperLogLog&nbsp;\u5c31\u662f\u7528\u6765\u505a\u53bb\u91cd\u590d\u7edf\u8ba1\u7684\uff1b bitmap&nbsp;\u5728\u505a\u7edf\u8ba1\u65f6\uff0c\u867d\u7136\u4f7f\u7528\u7684\u662f&nbsp;bit&nbsp;\u6765\u505a\u8bb0\u5f55\uff0c\u5df2\u7ecf\u5f88\u8282\u7701\u7a7a\u95f4\u4e86\uff1b \u4f46\u662f\u5728\u968f\u7740\u6570\u636e\u91cf\u5feb\u901f\u589e\u957f\u7684\u60c5\u51b5\u4e0b\uff0cbitmap&nbsp;\u4e5f\u662f\u5f88\u5360\u5185\u5b58\u7a7a\u95f4\u7684\uff1b \u800c&nbsp;HyperLogLog&nbsp;\u5c31\u4e0d\u540c\u4e86\uff0cHyperLogLog&nbsp;\u7684\u6bcf\u4e2a&nbsp;key&nbsp;\u53ea\u5360\u7528&nbsp;12kb&nbsp;\u7684\u5185\u5b58\uff0c \u5c31\u53ef\u4ee5\u7edf\u8ba1&nbsp;2\u768464&nbsp;\u6b21\u65b9\u4e2a\u57fa\u6570\uff1b \u800c\u4e14\u4e0d\u4f1a\u968f\u7740\u6570\u636e\u91cf\u7684\u589e\u591a\u800c\u53d8\u5927\uff0c\u5c31\u662f\u56fa\u5b9a\u7684&nbsp;12kb\uff1b \u8fd9\u662f\u56e0\u4e3a&nbsp;HyperLogLog&nbsp;\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 \u4f46\u8fd9\u4e5f\u9020\u6210\u4e86\u4e00\u4e2a\u95ee\u9898\uff0c\u5c31\u662f&nbsp;HyperLogLog&nbsp;\u57fa\u6570\u7edf\u8ba1\u7684\u51c6\u786e\u7387\u4e0d\u662f100%\uff0c\u4f1a\u6709&nbsp;0.81%&nbsp;\u7684\u8bef\u5dee\uff1b \u4f46\u5bf9\u4e8e\u4ebf\u7ea7\u6570\u636e\u7684\u7edf\u8ba1\uff0c\u8fd9\u70b9\u8bef\u5dee\u4f3c\u4e4e\u53ef\u4ee5\u5ffd\u7565\u4e0d\u8ba1\uff1b \u540c\u6837\u7684\uff0cbitmap&nbsp;\u8bb0\u5f55\u6570\u636e\u672c\u8eab\uff0c\u5b83\u7684\u8ba1\u7b97\u662f\u7cbe\u786e\u7684\uff1b \u6240\u6709\uff0c\u4e5f\u53ef\u4ee5\u8bf4&nbsp;HyperLogLog&nbsp;\u662f\u4e00\u79cd\u6982\u7387\u8ba1\u7b97\uff0c\u4f46\u662f\u8bef\u5dee\u662f\u5f88\u5c0f\u7684\u3002 HyperLogLog&nbsp;\u7684\u4f7f\u7528\u4e5f\u975e\u5e38\u7b80\u5355\uff0c\u5c313\u4e2a\u64cd\u4f5capi \u6dfb\u52a0\u5143\u7d20 pfadd key value [value &#8230;] pfadd 20220628:uv ip1 ip2 ip3 pfadd 20220629:uv ip1 ip2 ip3 ip4 ip5 pfadd 20220630:uv ip2 ip4 ip5 ip6 ip7 ip8 \u91cd\u590d\u6570\u636e\u4e0d\u4f1a\u88ab\u4fdd\u5b58\uff0c\u8fd4\u56de\u7684\u662f0 &gt; pfadd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-43972","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=43972"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43972\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=43972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=43972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=43972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}