内容简介:本文基于Python2.7 进行测试转载请注明出处,本文采用
本文基于 Python 2.7 进行测试
源码
# -*- coding:utf-8 -*- import BaseHTTPServer import threading import logging import time import urllib import urlparse import shlex import subprocess import io, shutil import re import random import time import os from multiprocessing import Queue, Pool import multiprocessing, time, random class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): '''处理请求并返回页面''' def do_GET(s): try: if None != re.search('/api/v1/startjar*', s.path) and '?' in s.path: print("in controller") content = "" startjar = "" query = urllib.splitquery(s.path) action = query[0] if query[1]: # 接收get参数 queryParams = {} for qp in query[1].split('&'): kv = qp.split('=') queryParams[kv[0]] = urllib.unquote(kv[1]).decode("utf-8", 'ignore') content += kv[0] + ':' + queryParams[kv[0]] + "\r\n" if kv[0] == 'jar': startjar = queryParams[kv[0]] shell_cmd = ' hadoop fs -copyToLocal /task/mr/'+startjar+' /opt/cm/lib/cloudera-scm-agent/jobs/myjar.jar && hadoop jar /opt/cm/lib/cloudera-scm-agent/jobs/myjar.jar && yes|rm /opt/cm/lib/cloudera-scm-agent/jobs/myjar.jar' print("cmd is " + shell_cmd) cmd = shlex.split(shell_cmd) p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while p.poll() is None: line = p.stdout.readline() line = line.strip() if line: s.wfile.write(line + "\n") if p.returncode == 0: print('Subprogram success') else: print('Subprogram failed') except Exception: s.wfile.close() print("Error ") # --------------------------------------------------------------------- if __name__ == '__main__': serverAddress = ('', 8881) server = BaseHTTPServer.HTTPServer(serverAddress, RequestHandler) server.serve_forever()
python 远程执行作业,(作业通过serverless 上传到hdfs)
- 用的http服务是python2.7 自带的(简单)
- 实现比较简单就是利用了 subprocess 的PIPE 通过管道将结果输出,实现一个watch on 的效果(大佬勿喷)
补充 serverless 上传jar的代码
-
注意 kubeless 1.0.0版本 Python 的runtime是基于
bottle
还是可以做点手脚的。
# /usr/bin/env python # coding=utf-8 import paramiko import os from hdfs import Client from bottle import route, run from bottle import request client = Client("http://hdfs-web-svc.cloudera:50070", root="/", timeout=100, session=False) # 文件上传的HTML模板,这里没有额外去写html模板了,直接写在这里,方便点吧 @route('/upload') def upload(): return ''' <html> <head> </head> <body> <form action"/upload" method="post" enctype="multipart/form-data"> <input type="file" name="data" /> <input type="submit" value="Upload" /> </form> </body> </html> ''' # 文件上传,overwrite=True为覆盖原有的文件, # 如果不加这参数,当服务器已存在同名文件时,将返回“IOError: File exists.”错误 @route('/upload', method='POST') def doupload(): upload = request.files.getall('data') for meta in upload: buf = meta.file.read() name, ext = os.path.splitext(meta.filename) if ext not in ('.jar', '.tar'): return 'File extension not allowed. type [JAR]' path = '/task/mr/' + name + ext # ssh root@agent-2.agent-svc.cloudera.svc.cluster.local print("--------------save Task To" + path) with client.write(path, overwrite=True) as writer: writer.write(buf); return 'ok'
转载请注明出处,本文采用 CC4.0 协议授权
以上所述就是小编给大家介绍的《python 远程启动mapreduce作业》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
微信营销与运营一册通
何秀芳、葛存山 / 人民邮电出版社 / 2014-10
《微信营销与运营一册通》深入介绍了当今最为火热的话题——微信营销,内容全面、系统和深入。它基于微信的最新版本,从策略、技巧与案例等多角度详细解析了微信的营销与运营,所有内容都是行业经验的结晶,旨在为企业或个人运用微信提供有价值的参考。《微信营销与运营一册通》主要内容如下。 * 5大微信营销利器:书中介绍了5大微信营销利器,包括漂流瓶、二维码、LBS功能、朋友圈和公众平台等。 * 6大微......一起来看看 《微信营销与运营一册通》 这本书的介绍吧!