Flink写入Hbase

1、在构建实时数仓的时候,通常会把dim层的数据存入hbase,这样做的好处一个是利用hbase的幂等性的功能,维度表基本上都会有数据唯一性,第二个在实时性要求的场景下,可以做数据点查关联,效率上得到一定的保障。

部分sink代码如下:

private transient Connection hbaseConnection;

    private transient Connection hbaseConnection;

    private Admin hbaseAdmin;

    private Table driBaseInfoResTable;


    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);

        org.apache.hadoop.conf.Configuration configuration = HBaseConfiguration.create();

        configuration.set("hbase.zookeeper.quorum", "192.168.11.1,192.168.11.2,192.168.11.3");

        hbaseConnection = ConnectionFactory.createConnection(configuration);

        hbaseAdmin = hbaseConnection.getAdmin();


        if (!hbaseAdmin.tableExists(TableName.valueOf("test:dim_dri_base_info"))) {
            log.error("hbase table not exists: {}", "test:dim_dri_base_info");
        }

    }


    /**
     * @param value
     * @return 返回的是 把数据打平的一条元祖数据 Tuple6<String,Integer, Integer, Integer,Integer,Integer>
     */
    @Override
    public Tuple6<String, Integer, Integer, Integer, Integer, Long> map(UpOrderInfo value) throws Exception {


        Tuple6 tuple6 = new Tuple6<String, Integer, Integer, Integer, Integer, Long>();
        Integer idCardTaurus = -100;
        Integer vehTaurus = -100;
        String time = value.getUpDataTime();

        byte[] opDriverUuid = value.getDriverUuid().concat("_op").getBytes();

        try {

            Get getResOp = new Get(opDriverUuid);

            getResOp.addFamily("CF1".getBytes());

            Result resultResOp = driResOPTable.get(getResOp);

            List<Cell> columnResOpCells = resultResOp.getColumnCells("CF1".getBytes(), "res_op".getBytes());

            String resUuid = "";

            for (Cell cell : columnResOpCells) {
                resUuid = Bytes.toString(CellUtil.cloneValue(cell));
            }

            Get driTaurus = new Get(resUuid.getBytes());

            driTaurus.addFamily("CF1".getBytes());


            Result resultBaseInfo = driBaseInfoResTable.get(driTaurus);

            List<Cell> columnTaurusStatusCells = resultBaseInfo.getColumnCells("CF1".getBytes(), "driver_taurus_status".getBytes());

            for (Cell cell : columnTaurusStatusCells) {
                value.setIdCardTaurusStatus(Integer.parseInt(Bytes.toString(CellUtil.cloneValue(cell))));
            }

            Get getVehNo = new Get(Utils.reverse(value.getVehicleNo()).getBytes());

            getVehNo.addFamily("CF1".getBytes());
            Result resultVehNo = vehStatusTable.get(getVehNo);

            List<Cell> columnVehNoCells = resultVehNo.getColumnCells("CF1".getBytes(), "taurus_status".getBytes());

            for (Cell cell : columnVehNoCells) {
                value.setVehicleTaurusStatus(Integer.parseInt(Bytes.toString(CellUtil.cloneValue(cell))));
            }

            List<Cell> columnLocalVehNoCells = resultVehNo.getColumnCells("CF1".getBytes(), "status_".getBytes());

            for (Cell cell : columnLocalVehNoCells) {
                value.setVehicleGovStatus(Integer.parseInt(Bytes.toString(CellUtil.cloneValue(cell))));
            }


            idCardTaurus = value.getIdCardTaurusStatus() != null ? value.getIdCardTaurusStatus() : -100;

            vehTaurus = value.getVehicleTaurusStatus() != null ? value.getVehicleTaurusStatus() : -100;


        } catch (Exception e) {
            log.error("操作hbase出的错误为:" + e);
        }

        try {
            tuple6.setFields(time, value.getUploadStatus(), value.getIsIntercept(), idCardTaurus, vehTaurus, 1L);
        } catch (Exception e) {
            log.error("错误日志为:" + e);
            log.error("错误的数据为:" + value.toString());
        }

        return tuple6;
    }


    @Override
    public void close() throws Exception {
      

        if (driBaseInfoResTable != null) {
            driBaseInfoResTable.close();
        }

        if (hbaseAdmin != null) {
            hbaseAdmin.close();
        }

        if (hbaseConnection != null) {
            hbaseConnection.close();
        }

        super.close();
    }

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个示例代码,演示如何使用Apache Flink将数据写入HBase: ``` import org.apache.flink.api.common.functions.MapFunction; import org.apache.flink.api.java.DataSet; import org.apache.flink.api.java.ExecutionEnvironment; import org.apache.flink.api.java.tuple.Tuple2; import org.apache.flink.configuration.Configuration; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; public class FlinkHBaseExample { public static void main(String[] args) throws Exception { // 创建Flink的执行环境 ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); // 加载数据集 DataSet<Tuple2<String, String>> data = env.fromElements( new Tuple2<>("row1", "value1"), new Tuple2<>("row2", "value2"), new Tuple2<>("row3", "value3") ); // 配置HBase连接信息 Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "localhost"); config.set("hbase.zookeeper.property.clientPort", "2181"); Connection connection = ConnectionFactory.createConnection(config); Table table = connection.getTable(TableName.valueOf("mytable")); // 将数据写入HBase data.map(new MapFunction<Tuple2<String, String>, Put>() { @Override public Put map(Tuple2<String, String> value) throws Exception { Put put = new Put(Bytes.toBytes(value.f0)); put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1"), Bytes.toBytes(value.f1)); return put; } }).output(new HBaseOutputFormat(table)); // 执行程序 env.execute(); } } ``` 在上面的代码中,我们首先创建了Flink的执行环境,并加载了一个数据集。接下来,我们使用HBaseConfiguration类来配置HBase连接信息,然后创建一个HBase表对象。最后,我们使用map函数将数据转换为Put对象,并将其输出到HBase中。在output方法中,我们使用了一个自定义的HBaseOutputFormat对象,该对象用于将Put对象写入HBase表中。 需要注意的是,上面的代码中没有包含所有必要的依赖项。在实际使用中,您需要在项目中添加以下依赖项: ``` <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-core</artifactId> <version>${flink.version}</version> </dependency> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-java</artifactId> <version>${flink.version}</version> </dependency> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-streaming-java_2.11</artifactId> <version>${flink.version}</version> </dependency> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-hbase</artifactId> <version>${flink.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>${hadoop.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hbase-client</artifactId> <version>${hbase.version}</version> </dependency> ``` 其中,${flink.version}、${hadoop.version}和${hbase.version}是您选择的Flink、Hadoop和HBase版本的占位符。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值