Hello, can't understand how to do next: I've got ...
# troubleshooting
и
Hello, can't understand how to do next: I've got few streams from few Kafka topics (if it possible to do other way and easier I could make one topic or any other modifications) with sensors measurements into JSON messages:
Copy code
topic1: {'data': {'temp':25.2, 'sensore_name': 'T1', 'timestamp': 123123131}, 'compare_with': 'T2'}
topic2: {'data': {'temp':28, 'sensore_name': 'T2', 'timestamp': 53543543}, 'compare_with': 'T1'}
topic3: {'data': {'temp':32, 'sensore_name': 'T3', 'timestamp': 6757575}, 'compare_with': 'T2'}
topic4: {'data': {'temp':12, 'sensore_name': 'T3', 'timestamp': 67856222}, 'compare_with': 'T1'}
I need to compare T1.data.temp - T2.data.temp (I need to compare it with EXACTLY last measurement of other sensor (Shown in compare_with), because measurements could come with different frequency: T1 1 message per sec, T2 1 message per 5 sec., T3 3 message per sec.) calculate AVG from this difference in 1 houre window, and if this difference more than AVG, then make Alarm to somewhere... Don't understand how to do it? Flink 1.17.1 Java 17
k
Hi You can use reduce operator, by that you can access your last log and perform whatever operation you want.
public static class Reducer implements ReduceFunction<Tuple6<String,String,String ,String ,Integer,Integer>>{ @Override public Tuple6<String, String, String, String, Integer, Integer> reduce(Tuple6<String, String, String, String, Integer, Integer> current, Tuple6<String, String, String, String, Integer, Integer> previous) throws Exception { return new Tuple6<>(current.f0, current.f1, current.f2, current.f3, current.f4+previous.f4,current.f5+previous.f5); } }
You can change your type from Tuple to a JSONObject
и
Sorry, but I can't understand how reduce can helps me in my case? How to calculate difference of temps from 2 streams? Now i did like this:
Copy code
DataStream<MergedTemps> comparisonStream = T1_stream
	.join(T2_stream)
	.where(T1 -> T1.getCompare_with())
	.equalTo(T2 -> T2.getSensore_name())
	.window(TumblingEventTimeWindows.of(Time.seconds(1)))
	.apply((JoinFunction<Sensor, Sensor, MergedTemps>) (T1, T2) -> {

		Tuple2<Double, Double> spread;
		
		double a = T1.getData().getTemp();
		double b = T2.getData().getTemp();
		spread = new Tuple2<>(a, b);
		
		return new MergedTemps(T1.getTimestamp(), T1.getSensore_name(), spread);
	});
But if I understand right: it merge mesurements not 1 last to 1 last...
k
Can you elaborate you want difference between last 2 element if yes then you can do with the Reduce by appending in existing operator If no you need to create windowing
и
I need difference between last 2 elements IN TWO DIFFEREND STREAMS! Last lelement from stream T1 - last element from stream T2! Stream<Double>= T1.data.temp - T2.data.temp That's why I'm not sure about reduce function, as I understand it works with one stream.
For example in stream T1 I've got 2 messages in 1sec window, and in T2 3 messages. It's gave me 6 messages in result stream. But I want to get max 3. Not more then in one of the streams.
k
Okay, I understand
Then you can implement like this Broadcast T1 last message with descriptor and then Process broadcasted element with T2