flink自定义指标发送到pushgateway写入prometheus

flink官方提供了写metrics的方式,但是相对来说有些不灵活,不符合我当前的要求,也没法自定义动态的label值,于是自定义了sink写入到pushgateway里。
代码如下:

  class MyPushGateWaySink(pushgatewayipport:String) extends RichSinkFunction[(String,String,String)] {

    var pushgateway:PushGateway = _
    var gauge:Gauge = Gauge.build
      .name("name")
      .help("help")
      .labelNames("label1","label2", "label3").register
    // open函数用于初始化富函数运行时的上下文等环境
    override def open(parameters: Configuration): Unit = {
      println("----------------------------初始化连接-------------------------")
      super.open(parameters)
      pushgateway= new PushGateway(pushgatewayipport)
    }
    override def invoke(value: (String,String,String), context: SinkFunction.Context): Unit = {
      val timestamp=System.currentTimeMillis()
      gauge.labels(value._1,value._2,value._3).set(timestamp)
      pushgateway.push(gauge,"name")
    }
    // 关闭时做清理工作
    override def close(): Unit = {
      println("-----------------------关闭连接-----------------------")
    }
  }
   kafkaStream.addSink(new MyPushGateWaySink(pushgatewayipport))

所需依赖如下:

<!-- https://mvnrepository.com/artifact/io.dropwizard.metrics/metrics-core -->
<dependency>
    <groupId>io.dropwizard.metrics</groupId>
    <artifactId>metrics-core</artifactId>
    <version>4.1.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient -->
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient</artifactId>
    <version>0.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient_httpserver -->
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_httpserver</artifactId>
    <version>0.9.0</version>
</dependency>

<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_pushgateway</artifactId>
    <version>0.9.0</version>
</dependency>

但是运行一直报无法序列化问题,于是修改源码,添加序列化
因为使用的是Gauge类型,所以对该类添加序列化,如下图
在这里插入图片描述
同时父类也添加序列化
在这里插入图片描述
两个类的路径如下
在这里插入图片描述
再次运行,数据成功发送到pushgateway

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想使用 Flink 批量将数据写入 HBase,可以自定义一个 HBaseSinkFunction。下面是一个简单的示例: ```java public class HBaseBatchSinkFunction extends RichSinkFunction<List<Tuple2<String, String>>> { private transient Connection connection; private transient BufferedMutator bufferedMutator; @Override public void open(Configuration parameters) throws Exception { Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "localhost"); config.set("hbase.zookeeper.property.clientPort", "2181"); config.set("zookeeper.znode.parent", "/hbase"); config.set("hbase.client.write.buffer", "10000000"); config.set("hbase.client.retries.number", "3"); connection = ConnectionFactory.createConnection(config); TableName tableName = TableName.valueOf("my_table"); BufferedMutatorParams params = new BufferedMutatorParams(tableName); params.writeBufferSize(1024 * 1024); bufferedMutator = connection.getBufferedMutator(params); } @Override public void invoke(List<Tuple2<String, String>> values, Context context) throws Exception { List<Put> puts = new ArrayList<>(); for (Tuple2<String, String> value : values) { Put put = new Put(Bytes.toBytes(value.f0)); put.addColumn(Bytes.toBytes("my_cf"), Bytes.toBytes("my_col"), Bytes.toBytes(value.f1)); puts.add(put); } bufferedMutator.mutate(puts); } @Override public void close() throws Exception { if (bufferedMutator != null) { bufferedMutator.flush(); bufferedMutator.close(); } if (connection != null) { connection.close(); } } } ``` 在这个自定义的 HBaseSinkFunction 中,我们使用 BufferedMutator 批量写入数据。在 open() 方法中,我们获取 HBase 连接和缓冲器。在 invoke() 方法中,我们将数据转换为 Put 对象,并添加到缓冲器中。最后,在 close() 方法中,我们刷新缓冲器并关闭连接。 在你的 Flink 程序中,你可以使用这个自定义的 HBaseSinkFunction,例如: ```java DataStream<Tuple2<String, String>> dataStream = ...; dataStream.addSink(new HBaseBatchSinkFunction()); ``` 这样,你就可以批量将数据写入 HBase 了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值