{"id":173058,"date":"2020-03-03T09:29:42","date_gmt":"2020-03-03T01:29:42","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=173058"},"modified":"2020-02-24T09:30:09","modified_gmt":"2020-02-24T01:30:09","slug":"big-data-bitmap","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/big-data-bitmap.html","title":{"rendered":"\u5927\u6570\u636e\u4e0b\u7684Distinct Count\uff08\u4e8c\uff09\uff1aBitmap\u7bc7"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5927\u6570\u636e\uff08big data\uff09\uff0cIT\u884c\u4e1a\u672f\u8bed\uff0c\u662f\u6307\u65e0\u6cd5\u5728\u4e00\u5b9a\u65f6\u95f4\u8303\u56f4\u5185\u7528\u5e38\u89c4\u8f6f\u4ef6\u5de5\u5177\u8fdb\u884c\u6355\u6349\u3001\u7ba1\u7406\u548c\u5904\u7406\u7684\u6570\u636e\u96c6\u5408\uff0c\u662f\u9700\u8981\u65b0\u5904\u7406\u6a21\u5f0f\u624d\u80fd\u5177\u6709\u66f4\u5f3a\u7684\u51b3\u7b56\u529b\u3001\u6d1e\u5bdf\u53d1\u73b0\u529b\u548c\u6d41\u7a0b\u4f18\u5316\u80fd\u529b\u7684\u6d77\u91cf\u3001\u9ad8\u589e\u957f\u7387\u548c\u591a\u6837\u5316\u7684\u4fe1\u606f\u8d44\u4ea7\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

1. Bitmap\u4ecb\u7ecd<\/strong><\/div>\n

\u300a\u7f16\u7a0b\u73e0\u7391\u300b\u4e0a\u662f\u8fd9\u6837\u4ecb\u7ecdbitmap\u7684\uff1a<\/p>\n

Bitmap\u662f\u4e00\u4e2a\u5341\u5206\u6709\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002\u6240\u8c13\u7684Bitmap\u5c31\u662f\u7528\u4e00\u4e2abit\u4f4d\u6765\u6807\u8bb0\u67d0\u4e2a\u5143\u7d20\u5bf9\u5e94\u7684Value\uff0c\u800cKey\u5373\u662f\u8be5\u5143\u7d20\u3002\u7531\u4e8e\u91c7\u7528\u4e86Bit\u4e3a\u5355\u4f4d\u6765\u5b58\u50a8\u6570\u636e\uff0c\u56e0\u6b64\u5728\u5185\u5b58\u5360\u7528\u65b9\u9762\uff0c\u53ef\u4ee5\u5927\u5927\u8282\u7701\u3002<\/p>\n

\u7b80\u800c\u8a00\u4e4b\u2014\u2014\u7528\u4e00\u4e2abit\uff080\u62161\uff09\u8868\u793a\u67d0\u5143\u7d20\u662f\u5426\u51fa\u73b0\u8fc7\uff0c\u5176\u5728bitmap\u7684\u4f4d\u7f6e\u5bf9\u5e94\u4e8e\u5176index\u3002\u300a\u7f16\u7a0b\u73e0\u7391\u300b\u7ed9\u51fa\u4e86\u4e00\u4e2a\u7528bitmap\u505a\u6392\u5e8f\u7684\u4f8b\u5b50\uff1a<\/p>\n

\r\n\/* Copyright (C) 1999 Lucent Technologies *\/\r\n\/* From 'Programming Pearls' by Jon Bentley *\/\r\n\/* bitsort.c -- bitmap sort from Column 1\r\n* Sort distinct integers in the range [0..N-1]\r\n*\/\r\n#include \r\n\r\n#define BITSPERWORD 32\r\n#define SHIFT 5\r\n#define MASK 0x1F\r\n#define N 10000000\r\nint a[1 + N \/ BITSPERWORD];\r\n\r\nvoid set(int i) { a[i >> SHIFT] |= (1 << (i & MASK)); }\r\n\r\nvoid clr(int i) { a[i >> SHIFT] &= ~(1 << (i & MASK)); }\r\n\r\nint test(int i) { return a[i >> SHIFT] & (1 << (i & MASK)); }\r\n\r\nint main() {\r\n    int i;\r\n    for (i = 0; i < N; i++)\r\n        clr(i);\r\n    \/* Replace above 2 lines with below 3 for word-parallel init\r\n    int top = 1 + N\/BITSPERWORD;\r\n    for (i = 0; i < top; i++)\r\n    a[i] = 0;\r\n    *\/\r\n    while (scanf(\"%d\", &i) != EOF)\r\n        set(i);\r\n    for (i = 0; i < N; i++)\r\n        if (test(i))\r\n            printf(\"%d\\n\", i);\r\n    return 0;\r\n}\r\n<\/pre>\n

\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c\u7528int\u7684\u6570\u7ec4\u5b58\u50a8bitmap\uff0c\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u5f85\u6392\u5e8f\u7684int\u6570\uff0c\u5176\u5bf9\u5e94\u7684index\u4e3a\u5176int\u503c\u3002<\/p>\n

2. Distinct Count\u4f18\u5316<\/strong><\/div>\n
index\u751f\u6210<\/strong><\/span><\/div>\n

\u4e3a\u4e86\u4f7f\u7528bitmap\u505aDistinct Count\uff0c\u9996\u5148\u9700\u5f97\u5230\u6bcf\u4e2a\u7528\u6237\uff08uid\uff09\u5bf9\u5e94\uff08\u5728bitmap\u4e2d\uff09\u7684index\u3002\u6709\u4e24\u79cd\u529e\u6cd5\u53ef\u4ee5\u5f97\u5230\u4ece1\u5f00\u59cb\u7f16\u53f7index\u8868\uff08\u4e0euid\u4e00\u4e00\u5bf9\u5e94\uff09\uff1a<\/p>\n

  • hash\uff0c\u4f46\u662f\u8981\u627e\u5230\u65e0\u78b0\u649e\u4e14hash\u503c\u5747\u5300\u5206\u5e03[1, +\u221e)\u533a\u95f4\u7684hash\u51fd\u6570\u662f\u975e\u5e38\u56f0\u96be\u7684\uff1b<\/li>\n
  • \u7ef4\u62a4\u4e00\u5f20uid\u4e0eindex\u4e4b\u95f4\u7684\u6620\u5c04\u8868\uff0c\u5e76\u589e\u91cf\u66f4\u65b0<\/li>\n

    \u6bd4\u8f83\u4e24\u79cd\u65b9\u6cd5\uff0c\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u66f4\u4e3a\u7b80\u5355\u53ef\u884c\u3002<\/p>\n

    UV\u8ba1\u7b97<\/strong><\/span><\/div>\n

    \u5728index\u751f\u6210\u5b8c\u6210\u540e\uff0cRDD[(uid, V)]\u4e0eRDD[(uid, index)]join\u5f97\u5230index\u5316\u7684RDD\u3002bitmap\u7684\u5f00\u6e90\u5b9e\u73b0\u6709EWAH\uff0c\u91c7\u7528RLE\uff08Run Length Encoding\uff09\u538b\u7f29\uff0c\u5f88\u597d\u5730\u89e3\u51b3\u4e86\u5b58\u50a8\u7a7a\u95f4\u7684\u6d6a\u8d39\u3002Distinct Count\u8ba1\u7b97\u8f6c\u53d8\u6210\u4e86\u6c42bitmap\u4e2d1\u7684\u4e2a\u6570\uff1a<\/p>\n

    \r\n\/\/ distinct count for rdd(not pair) and the rdd must be sorted in each partition\r\ndef distinctCount(rdd: RDD[Int]): Int = {\r\n    val bitmap = rdd.aggregate[EWAHCompressedBitmap](new EWAHCompressedBitmap())(\r\n      (u: EWAHCompressedBitmap, v: Int) => {\r\n        u.set(v)\r\n        u\r\n      },\r\n      (u1: EWAHCompressedBitmap, u2: EWAHCompressedBitmap) => u1.or(u2)\r\n    )\r\n    bitmap.cardinality()\r\n}\r\n\r\n\/\/ the tuple_2 is the index\r\ndef groupCount[K: ClassTag](rdd: RDD[(K, Int)]): RDD[(K, Int)] = {\r\n    val grouped: RDD[(K, EWAHCompressedBitmap)] = rdd.combineByKey[EWAHCompressedBitmap](\r\n      (v: Int) => EWAHCompressedBitmap.bitmapOf(v),\r\n      (c: EWAHCompressedBitmap, v: Int) => {\r\n        c.set(v)\r\n        c\r\n      },\r\n      (c1: EWAHCompressedBitmap, c2: EWAHCompressedBitmap) => c1.or(c2))\r\n    grouped.map(t => (t._1, t._2.cardinality()))\r\n}\r\n<\/pre>\n

    \u4f46\u662f\uff0c\u5728\u4e0a\u8ff0\u8ba1\u7b97\u4e2d\uff0c\u7531\u4e8eEWAHCompressedBitmap\u7684set\u65b9\u6cd5\u8981\u6c42int\u503c\u662f\u5347\u5e8f\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4RDD\u7684\u6bcf\u4e00\u4e2apartition\u7684index\u5e94\u662f\u5347\u5e8f\u6392\u5217\uff1a<\/p>\n

    \r\n\/\/ sort pair RDD by value\r\ndef sortPairRDD[K](rdd: RDD[(K, Int)]): RDD[(K, Int)] = {\r\n    rdd.mapPartitions(iter => {\r\n      iter.toArray.sortWith((x, y) => x._2.compare(y._2) < 0).iterator\r\n    })\r\n}\r\n<\/pre>\n

    \u4e3a\u4e86\u907f\u514d\u6392\u5e8f\uff0c\u53ef\u4ee5\u4e3a\u6bcf\u4e00\u4e2auid\u751f\u6210\u4e00\u4e2abitmap\uff0c\u7136\u540e\u5728Distinct Count\u65f6\u5c06bitmap\u8fdb\u884cor\u8fd0\u7b97\u4ea6\u53ef\uff1a<\/p>\n

    \r\nrdd.reduceByKey(_ or _)\r\n    .mapValues(_._2.cardinality())\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

    \u300a\u7f16\u7a0b\u73e0\u7391\u300b\u4e0a\u662f\u8fd9\u6837\u4ecb\u7ecdbitmap\u7684\uff1a Bitmap\u662f\u4e00\u4e2a\u5341\u5206\u6709\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002\u6240\u8c13\u7684Bitmap\u5c31\u662f\u7528\u4e00\u4e2ab […]<\/p>\n","protected":false},"author":1470,"featured_media":173021,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-173058","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/173058","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=173058"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/173058\/revisions"}],"predecessor-version":[{"id":173060,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/173058\/revisions\/173060"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/173021"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=173058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=173058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=173058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}