优化后的逻辑运行计划被LogToPhyTranslationVisitor处理,生成物理运行计划。
这是一个经典的Vistor设计模式应用场景。
当中,LogToPhyTranslationVisitor的visit()为入口方法,通过DependencyOrderWalker遍历处理逻辑运行计划中的每个LogicalRelationalOperator。DependencyOrderWalker依照依赖顺序遍历DAG中节点,保证当且仅当节点的全部前驱都被訪问后,它才会被訪问。核心逻辑例如以下,doAllPredecessors递归调用自己,将符合无前驱条件的节点加入到fifo队列中,终于实现的效果等效于将图拓扑排序后顺序訪问。
public void walk(PlanVisitorvisitor) throws FrontendException {
List<Operator> fifo = new ArrayList<Operator>();
Set<Operator> seen = new HashSet<Operator>();
List<Operator> leaves = plan.getSinks();
if (leaves == null) return;
for (Operator op : leaves) {
doAllPredecessors(op, seen, fifo);
}
for (Operator op: fifo) {
op.accept(visitor);
}
}接下来,每一个LogicalRelationalOperator又反过来调用LogToPhyTranslationVisitor对应的visit方法对自身进行处理,转化成PhysicalOperator。终于生成完整的逻辑运行计划。下图是LogToPhyTranslationVisitor中全部的visit
operator方法。
分析之前Pig系统分析(3)中代码生成的运行计划,如图所看到的:
以下是完整的物理运行计划。物理运行计划与逻辑运行计划结构类似,部分Operator一一相应,但存在几个明显差别:
F:Store(output:org.apache.pig.builtin.PigStorage) - scope-28
|
|---F: New ForEach(false,false)[bag] - scope-27
| |
| Project[bytearray][0] - scope-22
| |
| POUserFunc(org.apache.pig.builtin.COUNT)[long] - scope-25
| |
| |---Project[bag][1] - scope-24
|
|---E: Package[tuple]{bytearray} - scope-19
|
|---E: Global Rearrange[tuple] -scope-18
|
|---E: LocalRearrange[tuple]{bytearray}(false) - scope-20
| |
| Project[bytearray][2] - scope-21
|
|---D: New ForEach(true,true)[tuple] - scope-17
| |
| Project[bag][1] - scope-15
| |
| Project[bag][2] - scope-16
|
|---D:Package[tuple]{bytearray} - scope-10
|
|---D: GlobalRearrange[tuple] - scope-9
|
|---D: LocalRearrange[tuple]{bytearray}(false) - scope-11
| | |
| | Project[bytearray][0] - scope-12
| |
| |---C: Filter[bag] - scope-1
| | |
| | Greater Than[boolean] - scope-5
| | |
| | |---Cast[int] - scope-3
| | | |
| | | |---Project[bytearray][1] - scope-2
| | |
| | |---Constant(0) - scope-4
| |
| |---A: Load(file:///D:/Develop/projects/pig/file1:org.apache.pig.builtin.PigStorage)- scope-0
|
|---D: LocalRearrange[tuple]{bytearray}(false) - scope-13
| |
| Project[bytearray][1] - scope-14
|
|---B:Load(file:///D:/Develop/projects/pig/file2:org.apache.pig.builtin.PigStorage) -scope-6
PhysicalPlan类代表物理运行计划,继承自OperatorPlan。(继承时会使用PhysicalOperator替换以下代码片段中泛型參数E)
public abstract class OperatorPlan<E extends Operator> implements Iterable<E>, Serializable, Cloneable {
protected Map<E, OperatorKey> mOps;
protected Map<OperatorKey, E> mKeys;
protected MultiMap<E, E> mFromEdges;
protected MultiMap<E, E> mToEdges;
}
Pig系统分析(5)-从Logical Plan到Physical Plan,布布扣,bubuko.com
Pig系统分析(5)-从Logical Plan到Physical Plan
原文:http://www.cnblogs.com/zfyouxi/p/3779253.html