Hi,

LAST_VAL is not a built-in function, so you'd need to implement it as a
user-defined aggregate function (UDAGG) and register it.

The problem with joining an append only table with an updating table is the
following.

Consider two tables: users (uid, name, zip) and orders (oid, uid, product),
with user being an updating table and orders being append only.

On January 1st, the tables look like this:

Users:
uid_1, Fred, 12345
uid_2, Mary, 67890

Orders
oid_1, uid_1, Popcorn
oid_2, uid_2, Carrots

Joining both tables with the following query SELECT oid, product, name, zip
FROM users u, orders o WHERE u.uid = o.uid results in:

oid_1, Popcorn, Fred, 12345
oid_2, Carrots, Mary, 67890

Whenever, a new order is appended, we look up the corresponding user data,
perform the join and emit the results.
Let's say on July 1st we have received 100 orders from our two users all is
fine. However, on July 2nd Fred updates his zip code because he moved to
another city.
Our data now looks like this:

Users:
uid_1, Fred, 24680
uid_2, Mary, 67890

Orders
oid_1, uid_1, Popcorn
oid_2, uid_2, Carrots
....
oid_100, uid_2, Potatoes

The result of the same query as before is:

oid_1, Popcorn, Fred, 24680
oid_2, Carrots, Mary, 67890
....
oid_100, Potatoes, Mary, 67890

Notice how the first row changed?
If we strictly follow SQL semantics (which we do in Flink SQL) the query
needs to update the ZIP code of the first result row.
In order to do so, we need access to the original data of the orders table,
which is the append only table in our scenario.
Consequently, we need to fully materialize append only tables when they are
joined with an updating table without temporal constraints.

In many situations, the indented semantics for such a query would be to
join the order with the ZIP code of the user *that was valid at the time
when the order was placed*.
However, this is *not* semantics of the query of our example. For such a
query, we need to model the data differently. The users table needs to
store all modifications, i.e., the full history of all updates.
Each update needs a timestamp and each order needs a timestamp as well.
With these timestamps, we can write a query that joins an order with the
user data that we valid at the time when the order was placed.
This is the temporal constraint that I mentioned before. With this
constraint, Flink can use the information about progressing time to reason
about how much state it needs to keep because a change of the user table
will only affect future orders.

Flink makes this a lot easier with the concept of temporal tables [1] and
temporal table joins [2].

Best,
Fabian

[1]
https://ci.apache.org/projects/flink/flink-docs-release-1.8/dev/table/streaming/temporal_tables.html
[2]
https://ci.apache.org/projects/flink/flink-docs-release-1.8/dev/table/streaming/joins.html#join-with-a-temporal-table


Am Di., 6. Aug. 2019 um 21:09 Uhr schrieb Maatary Okouya <
maatarioko...@gmail.com>:

> Fabian,
>
> ultimately, i just want to perform a join on the last values for each
> keys.
>
> On Tue, Aug 6, 2019 at 8:07 PM Maatary Okouya <maatarioko...@gmail.com>
> wrote:
>
>> Fabian,
>>
>> could you please clarify the following statement:
>>
>> However joining an append-only table with this view without adding
>> temporal join condition, means that the stream is fully materialized as
>> state.
>> This is because previously emitted results must be updated when the view
>> changes.
>> It really depends on the semantics of the join and query that you need,
>> how much state the query will need to maintain.
>>
>>
>> I am not sure to understand the problem. If i have to append-only table
>> and perform some join on it, what's the issue ?
>>
>>
>> On Tue, Aug 6, 2019 at 8:03 PM Maatary Okouya <maatarioko...@gmail.com>
>> wrote:
>>
>>> Thank you for the clarification. Really appreciated.
>>>
>>> Is Last_val part of the API ?
>>>
>>> On Fri, Aug 2, 2019 at 10:49 AM Fabian Hueske <fhue...@gmail.com> wrote:
>>>
>>>> Hi,
>>>>
>>>> Flink does not distinguish between streams and tables. For the Table
>>>> API / SQL, there are only tables that are changing over time, i.e., dynamic
>>>> tables.
>>>> A Stream in the Kafka Streams or KSQL sense, is in Flink a Table with
>>>> append-only changes, i.e., records are only inserted and never deleted or
>>>> modified.
>>>> A Table in the Kafka Streams or KSQL sense, is in Flink a Table that
>>>> has upsert and delete changes, i.e., the table has a unique key and records
>>>> are inserted, deleted, or updated per key.
>>>>
>>>> In the current version, Flink does not have native support to ingest an
>>>> upsert stream as a dynamic table (right now only append-only tables can be
>>>> ingested, native support for upsert tables will be added soon.).
>>>> However, you can create a view with the following SQL query on an
>>>> append-only table that creates an upsert table:
>>>>
>>>> SELECT key, LAST_VAL(v1), LAST_VAL(v2), ...
>>>> FROM appendOnlyTable
>>>> GROUP BY key
>>>>
>>>> Given, this view, you can run all kinds of SQL queries on it.
>>>> However joining an append-only table with this view without adding
>>>> temporal join condition, means that the stream is fully materialized as
>>>> state.
>>>> This is because previously emitted results must be updated when the
>>>> view changes.
>>>> It really depends on the semantics of the join and query that you need,
>>>> how much state the query will need to maintain.
>>>>
>>>> An alternative to using Table API / SQL and it's dynamic table
>>>> abstraction is to use Flink's DataStream API and ProcessFunctions.
>>>> These APIs are more low level and expose access to state and timers,
>>>> which are the core ingredients for stream processing.
>>>> You can implement pretty much all logic of KStreams and more in these
>>>> APIs.
>>>>
>>>> Best, Fabian
>>>>
>>>>
>>>> Am Di., 23. Juli 2019 um 13:06 Uhr schrieb Maatary Okouya <
>>>> maatarioko...@gmail.com>:
>>>>
>>>>> I would like to have a KTable, or maybe in Flink term a dynamic Table,
>>>>> that only contains the latest value for each keyed record. This would 
>>>>> allow
>>>>> me to perform aggregation and join, based on the latest state of every
>>>>> record, as opposed to every record over time, or a period of time.
>>>>>
>>>>> On Sun, Jul 21, 2019 at 8:21 AM miki haiat <miko5...@gmail.com> wrote:
>>>>>
>>>>>> Can you elaborate more  about your use case .
>>>>>>
>>>>>>
>>>>>> On Sat, Jul 20, 2019 at 1:04 AM Maatary Okouya <
>>>>>> maatarioko...@gmail.com> wrote:
>>>>>>
>>>>>>> Hi,
>>>>>>>
>>>>>>> I am a user of Kafka Stream so far. However, because i have been
>>>>>>> face with several limitation in particular in performing Join on KTable.
>>>>>>>
>>>>>>> I was wondering what is the appraoch in Flink to achieve  (1) the
>>>>>>> concept of KTable, i.e. a Table that represent a changeLog, i.e. only 
>>>>>>> the
>>>>>>> latest version of all keyed records,  and (2) joining those.
>>>>>>>
>>>>>>> There are currently a lot of limitation around that on Kafka Stream,
>>>>>>> and i need that for performing some ETL process, where i need to mirror
>>>>>>> entire databases in Kafka, and then do some join on the table to emit 
>>>>>>> the
>>>>>>> logical entity in Kafka Topics. I was hoping that somehow i could 
>>>>>>> acheive
>>>>>>> that by using FLink as intermediary.
>>>>>>>
>>>>>>> I can see that you support any kind of join, but i just don't see
>>>>>>> the notion of Ktable.
>>>>>>>
>>>>>>>
>>>>>>>

Reply via email to