【用户画像】将数据迁移到ClickHouse(源码实现)、位图的介绍(bitmap)、位图在用户分群中的应用、位图的使用

一 数据迁移至Clickhouse

1 为何要迁移

标签计算完成后保存在hive虽然可以查询但是性能非常糟糕。而标签的使用往往是即时的。最常见的场景就是“用户分群”,也称“人群圈选”、“圈人”等等。

分群操作就是根据多个标签组合,产生一个用户集合,供营销、广告等部门使用。而这些操作计算量大,产生结果需要时效性高。

2 方案选型

选择方案最重要的依据就是数据量和时效性要求。

时效性 数据量 分群方案
能接受隔天 无所谓 HIVE宽表
即时产生 千万以下,标签百级 OLAP宽表(Elasticsearch,Clickhouse,Tidb…)
即时产生 亿级,标签千级 Bitmap方式(Clickhouse,doris)

适合的才是最好的,此任务选择用Clickhouse实现Bitmap方式存储。

3 任务目标

把hive中标签宽表数据,写入至Clickhouse的宽表。

4 设计分析

  • 读取hive的宽表,在clickhouse中建立对应的宽表。

    因为并不是hive表到hive表,所以并不能够直接用insert select 解决。

  • 先通过把数据查询成为Dataframe ,再通过行动算子写入至Clickhouse的宽表。

5 代码实现

搭建模块 – task-export-ck

(1 )pom.xml

在poml文件中添加配置

    <dependencies>
        <dependency>
            <groupId>com.hzy.userprofile</groupId>
            <artifactId>task-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 该插件用于将Scala代码编译成class文件 -->
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.4.6</version>
                <executions>
                    <execution>
                        <!-- 声明绑定到maven的compile阶段 -->
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single
  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Sure! Here's a simple example of how to create a 64-bit bitmap in ClickHouse: ``` CREATE TABLE bitmap_example ( id UInt64, bitmap_column Bitmap(64) ) ENGINE = Memory; INSERT INTO bitmap_example (id, bitmap_column) VALUES (1, toBitmap(0b01)); INSERT INTO bitmap_example (id, bitmap_column) VALUES (2, toBitmap(0b10)); INSERT INTO bitmap_example (id, bitmap_column) VALUES (3, toBitmap(0b100)); SELECT id, bitmap_column, toUInt64(bitmap_column) AS decimal_representation FROM bitmap_example; ``` This will create a table called `bitmap_example` with a single column `bitmap_column` of type `Bitmap(64)`. The `toBitmap` function is used to convert a binary representation of the bitmap into the actual bitmap data type. The `toUInt64` function is used to convert the bitmap back into a decimal representation for easier viewing. The output of the `SELECT` statement would look something like this: ``` id bitmap_column decimal_representation 1 \x01 1 2 \x02 2 3 \x04 4 ``` ### 回答2: 在ClickHouse,可以使用bitmap数据类型来实现64位的位图。以下是一个示例: 首先,创建一个包含bitmap列的表: ```sql CREATE TABLE test_bitmap ( id UInt32, bitmap Bitmap ) ENGINE = MergeTree() ORDER BY id; ``` 然后,可以插入数据到表: ```sql INSERT INTO test_bitmap (id, bitmap) VALUES (1, bitmapSet(bitmap(), 10)), -- 将第10位设置为1 (2, bitmapSet(bitmapSet(bitmap(), 20), 30)); -- 将第20位和第30位设置为1 ``` 接下来,可以查询表数据,并使用bitmapGet函数来获取位图的信息: ```sql SELECT id, bitmapGet(bitmap, 10) AS is_bit_10_set, bitmapGet(bitmap, 20) AS is_bit_20_set, bitmapGet(bitmap, 30) AS is_bit_30_set FROM test_bitmap; ``` 上述查询将返回每个id的对应位图的位信息。例如,对于上述示例数据,查询结果将类似于: ``` ┌─id─┬─is_bit_10_set─┬─is_bit_20_set─┬─is_bit_30_set─┐ │ 1 │ 1 │ 0 │ 0 │ │ 2 │ 0 │ 1 │ 1 │ └────┴───────────────┴───────────────┴───────────────┘ ``` 这表明第一个id的位图只有第10位被设置为1,而第二个id的位图的第20位和第30位都被设置为1。 这就是在ClickHouse实现64位位图的一个例子。您可以按照类似的方式使用位图函数来设置和查询其他位。 ### 回答3: 在ClickHouse实现64位位图的例子可以使用Bitmap类型来实现Bitmap是一种高效的数据结构,它可以用来表示集合的成员关系,适用于需要快速进行集合的union、intersection和difference等操作。 首先,我们可以创建一个表来存储64位位图数据: CREATE TABLE bitmap_table (bitmap Bitmap(DEFAULT)) ENGINE = Memory; 然后,我们可以将位图的值插入到表,以64位位图为例: INSERT INTO bitmap_table VALUES (toInt64(toBitmap(1, 2, 3, 4, 5))); 在以上示例,toBitmap函数将给定的数值(1、2、3、4、5)转换为一个64位位图,并使用toInt64函数将位图转换为64位整数,然后将该整数插入到bitmap_table表。 要查询具有特定位图值的行,可以使用get函数: SELECT * FROM bitmap_table WHERE has(bitmap, toBitmap(1, 2, 3, 4, 5)); 在以上示例,has函数用于检查bitmap列是否包含给定的位图值。如果位图的某个位图包含要查询的位图,则返回相应的行。 以上是一个在ClickHouse实现64位位图的简单示例。使用位图数据类型可以很容易地实现高效的集合操作和查询,提供更快速的分析和查询功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OneTenTwo76

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值