datax源码阅读一:python文件

一、前面主要是怎么使用datax和datax的插件编写,后面主要说明源码阅读部分,python相关文件

二、datax关键代码(python datax.py test.json)
1、datax.py文件

printCopyright()
parser = getOptionParser(sys.argv[1:])
options, args = parser.parse_args(sys.argv[1:])
if options.reader is not None and options.writer is not None:
    generateJobConfigTemplate(options.reader,options.writer)
    sys.exit(RET_STATE['OK'])
if len(args) == 0:
    parser.print_help()
    sys.exit(RET_STATE['FAIL'])
startCommand = buildStartCommand(options, args)
child_process = subprocess.Popen(startCommand, shell=True)

上面代码关键部分是函数buildStartCommand(options, args)拼出来的java命令,然后启动一个新的子进程去执行java命令

def buildStartCommand(options, args):
commandMap = {}
tempJVMCommand = DEFAULT_JVM
if options.jvmParameters:
    tempJVMCommand = tempJVMCommand + " " + options.jvmParameters

if options.remoteDebug:
    tempJVMCommand = tempJVMCommand + " " + REMOTE_DEBUG_CONFIG
    print 'local ip: ', getLocalIp()

if options.loglevel:
    tempJVMCommand = tempJVMCommand + " " + ("-Dloglevel=%s" % (options.loglevel))

if options.mode:
    commandMap["mode"] = options.mode

# jobResource 可能是 URL,也可能是本地文件路径(相对,绝对)
jobResource = args[0]
if not isUrl(jobResource):
    jobResource = os.path.abspath(jobResource)
    if jobResource.lower().startswith("file://"):
        jobResource = jobResource[len("file://"):]

jobParams = ("-Dlog.file.name=%s") % (jobResource[-20:].replace('/', '_').replace('.', '_'))
if options.params:
    jobParams = jobParams + " " + options.params

if options.jobid:
    commandMap["jobid"] = options.jobid

commandMap["jvm"] = tempJVMCommand
commandMap["params"] = jobParams
commandMap["job"] = jobResource

return Template(ENGINE_COMMAND).substitute(**commandMap)

java命令是通过模板ENGINE_COMMAND和commandMap拼接而成,其中ENGINE_COMMAND是主要的java脚本命令

ENGINE_COMMAND = "java -server ${jvm} %s -classpath %s  ${params} com.alibaba.datax.core.Engine -mode ${mode} -jobid ${jobid} -job ${job}" % (DEFAULT_PROPERTY_CONF, CLASS_PATH)

commandMap中包含jvm参数、任务动态参数和job文件信息(json配置文件),根据这个python文件,用户可以自行修改jvm相关参数,如

DEFAULT_JVM = "-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=%s/log" % (DATAX_HOME)

2、上述python文件执行完毕之后会启动java命令,主函数是com.alibaba.datax.core.Engine,至此python文件主要流程完毕

你可能感兴趣的:(python,shell,java)