zoukankan      html  css  js  c++  java
  • Pig Control Structures控制结构

    PigLatin中没有类似于if-else的控制结构。如果需要完成类似的控制结构,则需要使用embedded pig来完成。例如我们可以在Python中嵌入Pig Latin语句和Pig命令(请确保Jython jar包含在类路径中)。
    以下例子为判断pig作业是否有输出结果,若无输出,则删除hdfs对应输出文件夹下的空文件:

    #/usr/bin/python3
    #sample_pig.py
    # explicitly import Pig class 
    from org.apache.pig.scripting import Pig
    # COMPILE: compile method returns a Pig object that represents the pipeline
    P = Pig.compileFromFile("load_data_to_hive.pig")
    
    path = '/tmp'
    time = '2019-02-20 12:00:00'
    #BIND and RUN
    pig_result = P.bind({'time':time, 'path':path}).runSingle()
    
    if not pig_result.isSuccessful():
    	raise 'Pig job failed'
    final_res = pig_result.result('OUTPUT_RES')
    if final_res.getNumberRecords() > 1:
    	print('No records generated, remove empty output dir.')
    	Pig.fs('rmr ' + final_res.getLocation())
    	
    
    $ pig -embeded jython sample_pig.py
    

    更多解释和例子请参见官方文档

  • 相关阅读:
    架构设计的方法学 【转】
    异常处理
    Java中---HashSet中的Set()方法不能加重复值的原因,唯一性
    java中Set集合
    java中foreach语法和总结
    泛型的处理
    迭代器错误处理
    防重复提交
    mqtt安装和使用
    字符串正则替换
  • 原文地址:https://www.cnblogs.com/lestatzhang/p/10611347.html
Copyright © 2011-2022 走看看