pig脚本不需要后缀名(python tempfile模块生成pig脚本临时文件,执行)

pig 脚本运行不需要后缀名

pig脚本名为tempfile,无后缀名
用pig -f tempfile 可直接运行
另外,pig tempfile也可以直接运行

这样就可以用python临时文件存储pig脚本内容直接调用

python调用pig脚本的一种方式

将pig脚本用任意文件存储,执行时写入python的临时文件(tempfile模块操作),执行结束后删除。执行过程:
    用tempfile模块NamedTemporaryFile生成临时文件,名字默认随机,然后,可以用tempfile.name直接调用该文件(无后缀名.pig),pig脚本内容存放在任意文本文件中。

这样处理的好处是:参数传入很方便,pig脚本内容中参数全部使用python的格式化字符串,如%s、%d,调用时读取作为字符串对象command,用%将实际参数拼入字符串command,这样就避免了使用pig脚本 -p传入大量参数的繁琐。
坏处:多此一举、麻烦。字符直接拼入-p后用 default获取,也是极好的

    pig_script = tempfile.NamedTemporaryFile(delete=False)
    pig_script.write('set default_parallel %d; SET mapred.job.queue.name %s; %s %s' % (config.PIG_PARALLEL, job_queue, udf_jar_str, command_piece % args))
    pig_script.flush()

    command = '''%s -Dmapred.cache.files="%s,%s,%s,%s" -Dmapred.create.symlink=yes -Dmapred.child.java.opts=-Xmx%dm -f %s''' % 
              (config.PIG_BIN, metadata_dir, quadkey_dir, region_template_dir, ipdb_file, config.PIG_TASK_MAX_MEM, pig_script.name)

    if logger:
        logger.debug(command)

    result = exec_command(command, task_id)
原文地址:https://www.cnblogs.com/cl1024cl/p/6205365.html