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')