当前位置:   article > 正文

pyflink yield return

pyflink yield return

//yield

import json
import re
import logging
import sys
from collections import Counter

from pyflink.datastream import DataStream, StreamExecutionEnvironment
from pyflink.datastream.functions import RuntimeContext, FlatMapFunction, MapFunction,KeySelector
from pyflink.common.typeinfo import Types


s_env = StreamExecutionEnvironment.get_execution_environment()
data = DataStream(s_env._j_stream_execution_environment.socketTextStream('192.168.137.201', 8899))
    #data.print()
def get_key():
        return '999'
class LogEvent:
    world=None

    def __init__(self,world,count):
        self.world = world
        self.count = count
    def to_dict(self):
        return {
            "world": str(self.world),
            "count": str(self.count)

        }

class MyMapFunction(FlatMapFunction):
    def open(self, runtime_context: RuntimeContext):
        pass
    def flat_map(self, raw_message):
        arr_message=raw_message.split('\s')
        for x in arr_message:
           return x
arr_message=[]
def get_key(raw_message):
        arr_message = raw_message.split('\s+')
        yield   arr_message:
              
        
    
data.flat_map(MyMapFunction()).print()
s_env.execute('data')

[root@master pyflink]# python3 test_keyby.py 
3> a b c a d f

//return 
import json
import re
import logging
import sys
from collections import Counter

from pyflink.datastream import DataStream, StreamExecutionEnvironment
from pyflink.datastream.functions import RuntimeContext, FlatMapFunction, MapFunction,KeySelector
from pyflink.common.typeinfo import Types


s_env = StreamExecutionEnvironment.get_execution_environment()
data = DataStream(s_env._j_stream_execution_environment.socketTextStream('192.168.137.201', 8899))
    #data.print()
def get_key():
        return '999'
class LogEvent:
    world=None

    def __init__(self,world,count):
        self.world = world
        self.count = count
    def to_dict(self):
        return {
            "world": str(self.world),
            "count": str(self.count)

        }

class MyMapFunction(FlatMapFunction):
    def open(self, runtime_context: RuntimeContext):
        pass
    def flat_map(self, raw_message):
        arr_message=raw_message.split('\s')
        for x in arr_message:
           return x
arr_message=[]
def get_key(raw_message):
        arr_message = raw_message.split('\s+')
        for x in  arr_message:
            yield x 
        
    
data.flat_map(MyMapFunction()).print()
s_env.execute('data')
[root@master pyflink]# python3 test_keyby.py 
4> a
4>  
4> b
4>  
4>  
4> c
4>  
4> d
4>  
4> e
4>  
4> f
4>  
4> g
4>  
4> h
4>  
4> i

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/548810
推荐阅读
  

闽ICP备14008679号