[root@master pyflink]# cat test_keyby.py
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.strip()
def get_key(a):
return a
data.flat_map(MyMapFunction()).key_by(get_key).print()
s_env.execute('data')
[root@master pyflink]# python3 test_keyby.py
1> b
1> i
1> l
2> a
2> c
2> d
3> f
3> g
3> j
3> k
4>
4>
4>
4>
4> e
4>
4>
4>
4> h
4>
4>
4>
4>
2> a
2> c
2> d
1> b
4>
4>
4>
3> f
3> g
4>
4>
4>
4> e
4>
4>
2> a
2> c
2> d
4>
2> a
2> a
3> j
2> c
2> d
1> b
4>
4>
4>
4>
4>
3> k