使用Restful方式提交Flink任务详解

Flink提供了丰富的客户端操作来提交任务,本文在Restful方式上提供扩展,其余四种方式可观看flink-china系列教程-客户端操作的具体分享

Flink从1.7版本开始支持RESTClient提交任务,RESTClient可能很多人不熟悉。使用RESTClient提交任务,换句话说就是Flink可支持API调用方式,提交你编写好的Flink代码,提交到Flink集群运行。本文演示读取kafka数据,使用Springcloud微服务框架调用启动,下面是具体实现步骤。

编写Flink程序

新建Springboot父工程,新建flink-service模块。flink-service新建以下代码,然后打包项目。打包项目的目的是把flink任务执行需要用到jar包,在使用RESTClient提交任务是,一起提交到集群运行。

public class ReadFromsinkKafka {
  public static void main(String[] args) throws Exception {
    // create execution environment
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    Properties properties = new Properties();
    properties.setProperty("bootstrap.servers", "192.168.11.12:9092");
    properties.setProperty("group.id", "flinksink");
    DataStream<String> stream = env.addSource(new FlinkKafkaConsumer("flinktest", 
        new SimpleStringSchema(), properties));
    stream.map(new MapFunction<String, String>() {
      private static final long serialVersionUID = -6867736771747690202L;
      @Override
      public String map(String value) throws
  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的Java代码示例,用于调用Flink RESTful接口提交批处理作业: ```java import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import java.io.IOException; public class FlinkRestClient { public static void main(String[] args) throws IOException { String flinkRestUrl = "http://localhost:8081/jars/upload"; // Flink RESTful接口URL String jarPath = "/path/to/your/jar/file"; // 批处理作业jar包路径 // 构造请求体 String requestBody = "{\"entryClass\":\"com.example.batch.BatchJob\"," + "\"programArgs\":[\"--input\",\"/path/to/input\",\"--output\",\"/path/to/output\"]}"; // 构造POST请求 HttpPost postRequest = new HttpPost(flinkRestUrl); postRequest.setHeader("Accept", "application/json"); postRequest.setHeader("Content-type", "application/json"); postRequest.setHeader("User-Agent", "FlinkRestClient/1.0"); // 设置jar文件实体 StringEntity fileEntity = new StringEntity(jarPath, ContentType.APPLICATION_OCTET_STREAM); postRequest.setEntity(fileEntity); // 设置请求体实体 StringEntity bodyEntity = new StringEntity(requestBody, ContentType.APPLICATION_JSON); postRequest.setEntity(bodyEntity); // 发送POST请求 HttpResponse response = HttpClientBuilder.create().build().execute(postRequest); // 解析响应 if (response.getStatusLine().getStatusCode() == 200) { System.out.println("Job submitted successfully!"); } else { System.out.println("Failed to submit job."); } } } ``` 以上代码仅供参考,实际情况中可能需要根据不同的Flink版本和RESTful接口进行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值