flink python rest接口实现上传、运行、取消功能

 flink没有python的rest接口,只能通过脚本调用,通过flask调脚本实现rest接口形式

from flask import Flask, make_response,request
import uuid
import os
from werkzeug.utils import secure_filename


app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = '/opt/work/flink-1.12.2/my_examples/upload_pythons'

file_dict = {}

@app.after_request
def af_request(*resp_raw):
    resp = make_response(*resp_raw)
    resp.headers['Access-Control-Allow-Origin'] = '*'
    resp.headers['Access-Control-Allow-Methods'] = 'GET,POST'
    resp.headers['Access-Control-Allow-Headers'] = 'x-requested-with,content-type'
    return resp


@app.route('/api/p/run/<string:file_id>', methods=['GET', 'POST'])
def run(file_id):
    file_path = file_dict[file_id]
    start = f'/opt/work/flink-1.12.2/bin/flink run -d -py {file_path} hadoop-master:9092'
    a = os.popen(start).readlines()[0]
    print('a', a)
    print(type(a))
    print(a.split())
    job_id = (a.split()[-1])
    print('job_id', job_id)
    return {'job_id': job_id}


@app.route('/api/p/cancel/<string:job_id>', methods=['GET', 'POST'])
def cancel(job_id):
    cancel = f'/opt/work/flink-1.12.2/bin/flink cancel {job_id}'
    a = os.popen(cancel)
    print(a.read())
    return {}


@app.route('/api/p/upload', methods=['GET', 'POST'])
def upload():
    f = request.files['file']
    path = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename))
    f.save(path)
    file_id = str(uuid.uuid4())
    file_dict[file_id] = path
    return {'file_id': file_id}


if __name__ == '__main__':
    app.run(host='0.0.0.0')

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值