You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Licensed to the Apache Software Foundation (ASF) under one or more# contributor license agreements. See the NOTICE file distributed with# this work for additional information regarding copyright ownership.# The ASF licenses this file to You under the Apache License, Version 2.0# (the "License"); you may not use this file except in compliance with# the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.#frompywy.graph.graphimportGraphfrompywy.graph.traversalimportTraversalfrompywy.protobuf.planwriterimportMessageWriterfrompywy.orchestrator.operatorimportOperatorimportpywy.orchestrator.operatorimportitertoolsimportcollectionsimportloggingfromfunctoolsimportreduce# Wraps a Source operation to create an iterableclassDataQuantaBuilder:
def__init__(self, descriptor):
self.descriptor=descriptordefsource(self, source):
iftype(source) isstr:
source_ori=open(source, "r")
else:
source_ori=sourcereturnDataQuanta(
Operator(
operator_type="source",
udf=source,
iterator=iter(source_ori),
previous=[],
python_exec=False
),
descriptor=self.descriptor
)
# Wraps an operation over an iterableclassDataQuanta:
def__init__(self, operator=None, descriptor=None):
self.operator=operatorself.descriptor=descriptorifself.operator.is_source():
self.descriptor.add_source(self.operator)
ifself.operator.is_sink():
self.descriptor.add_sink(self.operator)
# Operational Functionsdeffilter(self, udf):
deffunc(iterator):
returnfilter(udf, iterator)
returnDataQuanta(
Operator(
operator_type="filter",
udf=func,
previous=[self.operator],
python_exec=True
),
descriptor=self.descriptor
)
defflatmap(self, udf):
defauxfunc(iterator):
returnitertools.chain.from_iterable(map(udf, iterator))
deffunc(iterator):
mapped=map(udf, iterator)
flattened=flatten_single_dim(mapped)
yieldfromflatteneddefflatten_single_dim(mapped):
foriteminmapped:
forsubiteminitem:
yieldsubitemreturnDataQuanta(
Operator(
operator_type="flatmap",
udf=func,
previous=[self.operator],
python_exec=True
),
descriptor=self.descriptor
)
defgroup_by(self, udf):
deffunc(iterator):
# TODO key should be given by "udf"returnitertools.groupby(iterator, key=operator.itemgetter(0))
#return itertools.groupby(sorted(iterator), key=itertools.itemgetter(0))returnDataQuanta(
Operator(
operator_type="group_by",
udf=func,
previous=[self.operator],
python_exec=True
),
descriptor=self.descriptor
)
defmap(self, udf):
deffunc(iterator):
returnmap(udf, iterator)
returnDataQuanta(
Operator(
operator_type="map",
udf=func,
previous=[self.operator],
python_exec=True
),
descriptor=self.descriptor
)
# Key specifies pivot dimensions# UDF specifies reducer functiondefreduce_by_key(self, keys, udf):
op=Operator(
operator_type="reduce_by_key",
udf=udf,
previous=[self.operator],
python_exec=False
)
print(len(keys), keys)
foriinrange(0, len(keys)):
"""if keys[i] is int: op.set_parameter("vector_position|"+str(i), keys[i]) else: op.set_parameter("dimension_key|"+str(i), keys[i])"""# TODO maybe would be better just leave the number as keyop.set_parameter("dimension|"+str(i+1), keys[i])
returnDataQuanta(
op,
descriptor=self.descriptor
)
defreduce(self, udf):
deffunc(iterator):
returnreduce(udf, iterator)
returnDataQuanta(
Operator(
operator_type="reduce",
udf=func,
previous=[self.operator],
python_exec=True
),
descriptor=self.descriptor
)
defsink(self, path, end="\n"):
defconsume(iterator):
withopen(path, 'w') asf:
forxiniterator:
f.write(str(x) +end)
deffunc(iterator):
consume(iterator)
# return self.__run(consume)returnDataQuanta(
Operator(
operator_type="sink",
udf=path,
# To execute directly uncomment# udf=func,previous=[self.operator],
python_exec=False
),
descriptor=self.descriptor
)
defsort(self, udf):
deffunc(iterator):
returnsorted(iterator, key=udf)
returnDataQuanta(
Operator(
operator_type="sort",
udf=func,
previous=[self.operator],
python_exec=True
),
descriptor=self.descriptor
)
# This function allow the union to be performed by Python# Nevertheless, current configuration runs it over Javadefunion(self, other):
deffunc(iterator):
returnitertools.chain(iterator, other.operator.getIterator())
returnDataQuanta(
Operator(
operator_type="union",
udf=func,
previous=[self.operator, other.operator],
python_exec=False
),
descriptor=self.descriptor
)
def__run(self, consumer):
consumer(self.operator.getIterator())
# Execution Functionsdefconsole(self, end="\n"):
defconsume(iterator):
forxiniterator:
print(x, end=end)
self.__run(consume)
# Only for debugging purposes!# To execute the plan directly in the program driverdefexecute(self):
logging.warn("DEBUG Execution")
logging.info("Reminder to swap SINK UDF value from path to func")
logging.debug(self.operator.previous[0].operator_type)
ifself.operator.is_sink():
logging.debug(self.operator.operator_type)
logging.debug(self.operator.udf)
logging.debug(len(self.operator.previous))
self.operator.udf(self.operator.previous[0].getIterator())
else:
logging.error("Plan must call execute from SINK type of operator")
raiseRuntimeError# Converts Python Functional Plan to valid Wayang Plandefto_wayang_plan(self):
sinks=self.descriptor.get_sinks()
iflen(sinks) ==0:
returngraph=Graph()
graph.populate(self.descriptor.get_sinks())
# Uncomment to check the Graph built# graph.print_adjlist()# Function to be consumed by Traverse# Separates Python Plan into a List of Pipelinesdefdefine_pipelines(node1, current_pipeline, collection):
defstore_unique(pipe_to_insert):
forpipeincollection:
ifequivalent_lists(pipe, pipe_to_insert):
returncollection.append(pipe_to_insert)
defequivalent_lists(l1, l2):
ifcollections.Counter(l1) ==collections.Counter(l2):
returnTrueelse:
returnFalseifnotcurrent_pipeline:
current_pipeline= [node1]
elifnode1.operator.is_boundary():
store_unique(current_pipeline.copy())
current_pipeline.clear()
current_pipeline.append(node1)
else:
current_pipeline.append(node1)
ifnode1.operator.sink:
store_unique(current_pipeline.copy())
current_pipeline.clear()
returncurrent_pipeline# Works over the graphtrans=Traversal(
graph=graph,
origin=self.descriptor.get_sources(),
# udf=lambda x, y, z: d(x, y, z)# UDF always will receive:# x: a Node object,# y: an object representing the result of the last iteration,# z: a collection to store final results inside your UDFudf=lambdax, y, z: define_pipelines(x, y, z)
)
# Gets the results of the traverse processcollected_stages=trans.get_collected_data()
# Passing the Stages to a Wayang message writerwriter=MessageWriter()
a=0# Stage is composed of class Node objectsforstageincollected_stages:
a+=1logging.info("///")
logging.info("stage"+str(a))
writer.process_pipeline(stage)
writer.set_dependencies()
# Uses a file to provide the plan# writer.write_message(self.descriptor)# Send the plan to Wayang REST api directlywriter.send_message(self.descriptor)
c1f2ed0fa73244efb915223a8c6334a93055dc1c
The text was updated successfully, but these errors were encountered:
key should be given by "udf"
UDF specifies reducer function
incubator-wayang/python/old_code/pywayang/src/pywy/orchestrator/dataquanta.py
Line 104 in c25c656
c1f2ed0fa73244efb915223a8c6334a93055dc1c
The text was updated successfully, but these errors were encountered: