Hello all - I am pretty new in Data Streams API. ...
# troubleshooting
s
Hello all - I am pretty new in Data Streams API. I need to implement the broadcast join. We have one big stream and multiple small streams. Can we connect multiple small streams with one big streams, considering there is no relationship between small streams. We can only join small streams with big stream. If this is possible, Can someone please help and share some example or links?
m
First, I don't think there is such a thing as "broadcast join' in Apache Flink. Broadcasting state (https://nightlies.apache.org/flink/flink-docs-release-1.19/docs/dev/datastream/fault-tolerance/broadcast_state/) exists though, and that is usually used to introduce control metadata to configure another stream. So yes, the data that you would broadcast would be a "smaller" stream than the larger stream that you're controlling. If that is not what you intend to do, then what do you mean by "broadcast join"? Also, in order to join data together, you need some sort of relationship, or key (just like two tables in SQL need a column to join upon). I think we need more information to help you, examples of the data, how they're related, etc. Your description doesn't have enough information.
s
Sorry for the delay in response. I am looking something similar to broadcast join which we have in Apache Spark. I am not sure if flink supports that.
m
So I read the docs on Apache Spark broadcast join, and perhaps this is my own misunderstanding, but I think there is no need to support that because Apache Flink works with stream elements and windows, not data frames. One stream can move fast, and another can move slowly, and that is no problem. The data will join if given a key and time window match regardless of the respective speed of each stream.
s
What about data shuffling? I believe if we broadcast then we can avoid shuffling. Please correct if I am wrong.
We need to join historical data as well. It means that on big streams we have 40 million records around and we have small stream which may have max 50000 records.
m
When you do a join that requires a key (no need to shuffle that) and a window.
Just make sure that your key is not hot
s
Can we use this in SQL API also?
I am using simple left joins in flink SQL
m
Does your “small” contain control meta for the large stream?
s
we have tested this with simple left joins where we have 6-7 small streams it took more than 2 days to complete the processing with 32 parallelism. we used AWS KDA. Right now we are checking if we can optimize the SQL joins.
kind of but this meta can also change.
m
So are you saving the smaller data in keyed state, and then enriching the larger stream ?
s
One dump question, in Flink SQL/Table API do we have the keys?
m
I tend to use the DataStream api more, but …
s
As of now we are not, we are just using Table APIs, and write the SQL with simple joins like below
Copy code
from tcs__units unit 
                 left join ads_unit__dispo_stat_typ dispo on unit.UT_DISPOSITION_IND = dispo.DISPO_STAT_TYP_ID
                 left join ads_invntry__feat_typ feat on unit.UT_FT_TYPE = feat.FEAT_TYP_CD
                 left join tcs__not_run_reasons nrr on unit.UT_NOT_RUN_CODE = nrr.NOT_RUN_CODE
                 left join ads_unit__ad_unit_mblty_typ mblty on unit.UT_MOBILITY_CODE = mblty.AD_UNIT_MBLTY_TYP_CD
                 left join tcs__makegood_types mkgd on unit.UT_MT_TYPE = mkgd.MT_TYPE
                 left join tcs__makegood_reason mkgd_rsn on unit.UT_MG_REASON_CODE = mkgd_rsn.MG_REASON_CODE 
                 left join tcs__system_users su on unit.UT_SU_SYS_USER_NAME = su.PERSON_GUID
                 left join tcs__system_users su2 on unit.UT_SU_SYS_USER_NAME = su2.SU_SYS_USER_NAME
                 left join tcs__products pdt on unit.UT_PDT_CODE = pdt.PDT_CODE
                 left join tcs__dayparts_tab dp on unit.UT_DP_CODE = dp.DP_CODE and dp.DP_APPL = 'SALES'
m
The tables don’t have a primary key, but you can use any column to perform a keyed join
I would love to help, but need to drive home now. Will catch you later
s
sure thank you!
m
So many joins!!
s
yes
m
So that means 5 streams?
s
10 small streams join with one main stream
m
Your data source, is it Kafka?
s
yes
but we can't use windowing as of now
m
How big is the window?
Oh no window
s
We can't use, we need to keep state as it is.
in future we are thinking to move state outside flink somewhere in-memory db like redis.
I am thinking to use
Flink *side output streams*
as mentioned in this doc: https://medium.com/@bijela.vrana/flink-enriching-stream-with-more-streams-b4b2f4da5495
m
I’ll get back to you. But I never did a join with that many tables in Flink, so not sure the answer just yet
s
sure, no worries. Thanks for helping me.
m
so,....I did some experiments with Flink SQL. I usually use the data-stream API, and it turns out that when you do a join, in the style above, it will cache the records on the right hand side forever. Although I don't know the idiosyncrasies of the SQL API, I would look into setting a reasonable TTL or Window Join that way all the data is not cached.
that said, always keep your fast moving stream on the left side of the join. And, put a TTL on right side that it is not cached forever. Or use a window a join so that you can constrain WHEN it should join with respect to time.
Here are the dangerous parts:
Copy code
For example, if there is a new record on the left side, it will be joined with all the previous and future records on the right side when the product id equals.
...
Thus, the required state for computing the query result might grow infinitely depending on the number of distinct input rows of all input tables and intermediate join results. You can provide a query configuration with an appropriate state time-to-live (TTL) to prevent excessive state size.
s
correct, but if we put TTL on right side, then if any record will come on left side ,might have null values on columns which we want to extract from right side form join
m
well, you'd need to rewrite the query so that your "fast" stream is always on the left side. You want to TTL the right side on the left join.
although I think windowing would work better.
I can't imagine why you'd want to cache so many apache flink records in a data stream and join them. I am really much more used to the DataStream API instead of the SQL one.
s
We have developed the config driven framework using SQL, where to write the new transformation developer just need to add the new config file which is in YAML.
I will now strated writing in datastream API but looks like it would be little difficult to write everything using genericRecord, so now I am using specificRecord. But still I am not sure if I can add windowing on it or TTL. As we need if anything gets changed either right or left side, user should get the updated one in near real time.
I am not sure if I need to use any other technology to achieve this scenario.
m
perhaps i am overthinking it. on the small stream size, total of all small streams, how many records are we talking about? depending on how many nodes, partitions, and memory, it might not be an issue at all.
I know that I was caching 100,000s of thousands records with no problem.
It could be millions.
but, in your case, when you do the join, ALL historical records on small stream will be joined per each stream element in the large stream.
that amplifies the number of records in the large stream.
s
There would be 1:1 relation between large stream and small streams
records on small stream would be less than 50K and in large streams it would be in millions around 90 M