3.2.2 数据消费

如果通过 Kafka 客户端消费者可以读取到数据, 证明数据采集是成功的.

现在开始编写操作 HBase 的代码,用于消费数据,将产生的数据实时存储在 HBase 中。

思路

  • 编写 Kafka 消费者,读取 Kafka 集群中缓存的消息,并打印到控制台以观察是否成功;

  • 既然能够读取到 Kafka 中的数据了,就可以将读取出来的数据写入到 HBase 中,所以编写调用 HBaseAPI 相关方法,将从 Kafka 中读取出来的数据写入到 HBase;

  • 以上两步已经足够完成消费数据,存储数据的任务,但是涉及到解耦,所以过程中需要将一些属性文件外部化,HBase 通用性方法封装到某一个类中。


添加maven 依赖

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-client</artifactId>
        <version>1.3.2.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-server</artifactId>
        <version>1.3.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
        <version>0.11.0.3</version>
    </dependency>
</dependencies>

copy 配置文件到 resoureces 目录下

kafka-hbase.properties, log4j.properties,hbase-site.xml

Copyright © 尚硅谷大数据 2019 all right reserved,powered by Gitbook
该文件最后修订时间: 2018-12-27 19:50:11

results matching ""

    No results matching ""