wenjin272 commented on code in PR #48: URL: https://github.com/apache/flink-agents/pull/48#discussion_r2192186182
########## python/flink_agents/runtime/remote_execution_environment.py: ########## @@ -0,0 +1,165 @@ +################################################################################ +# 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. +################################################################################# +import uuid +from typing import Any, Dict, Generator, List + +import cloudpickle +from pyflink.common import Row +from pyflink.common.typeinfo import PickledBytesTypeInfo, RowTypeInfo +from pyflink.datastream import ( + DataStream, + KeyedProcessFunction, + KeyedStream, + KeySelector, + StreamExecutionEnvironment, +) +from pyflink.util.java_utils import invoke_method + +from flink_agents.api.event import InputEvent +from flink_agents.api.execution_enviroment import AgentsExecutionEnvironment +from flink_agents.api.workflow import Workflow +from flink_agents.plan.workflow_plan import WorkflowPlan + + +class MapKeyedProcessFunctionAdapter(KeyedProcessFunction): + """Util class for converting element in KeyedStream to Row.""" + + def process_element( + self, value: Any, ctx: "KeyedProcessFunction.Context" + ) -> Generator: + """Convert element to Row contains key.""" + ctx.get_current_key() + yield Row(ctx.get_current_key(), InputEvent(input=value)) + + +class RemoteExecutionEnvironment(AgentsExecutionEnvironment): + """Implementation of AgentsExecutionEnvironment for execution with DataStream.""" + + __env: StreamExecutionEnvironment + __input: DataStream + __workflow_plan: WorkflowPlan + __output: DataStream + + def from_datastream( + self, input: DataStream, key_selector: KeySelector = None + ) -> "AgentsExecutionEnvironment": + """Set input for agents. + + Parameters + ---------- + input : DataStream + Receive a DataStream as input. + key_selector : KeySelector + Extract key from each input record. + """ + if isinstance(input, KeyedStream): + self.__input = input.process( + MapKeyedProcessFunctionAdapter(), + output_type=RowTypeInfo( + [PickledBytesTypeInfo(), PickledBytesTypeInfo()] + ), + ) + else: + self.__input = input.map( + lambda x: Row( + key_selector.get_key(x) + if key_selector is not None + else uuid.uuid4(), + InputEvent(input=x), Review Comment: I will update this after @codenohup update PR #47 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
