Search code examples
apache-kafkaconfluent-platformksqldbstreaming-analytics

How to write the query to extract all the field value along with condition satisfied values in KSQL


Created stream with the following field

CREATE STREAM pageviews_original_string(view_time string, user_id varchar, pageid varchar) WITH (kafka_topic='pageviews',value_format='DELIMITED',KEY='pageid');

Changed the pageid into the uppercase along with following values.

create stream up_case AS SELECT UCASE(pageid), user_id FROM PAGEVIEWS_ORIGINAL_STRING where user_id = 'User_9';

outcome

PAGE_26 | User_9
PAGE_67 | User_9
PAGE_39 | User_9
PAGE_80 | User_9
PAGE_40 | User_9
PAGE_92 | User_9

Now what i want is the condition satisfied data has to be modified and extracted along with remaining field values something like this

****PAGE_26 | User_9
PAGE_67 | User_9
PAGE_39 | User_9
PAGE_80 | User_9
PAGE_40 | User_9
PAGE_92 | User_9****
Page_66 | User_7
Page_25 | User_2
Page_41 | User_3
Page_34 | User_1
Page_28 | User_2
Page_55 | User_5
Page_77 | User_5
Page_32 | User_8
Page_60 | User_4

can you please help me in solving this use case


Solution

  • Step 1: Created stream with condition changing the uppercase of pageid of the user_9

    create stream step1 AS SELECT UCASE(pageid) AS upcase, user_id FROM PAGEVIEWS_ORIGINAL_STRING where user_id = 'User_9';
    

    Step 2: Then created another stream with condition without changing anycases of all user by excluding user_9

    create stream step2 AS select pageid AS upcase, user_id from PAGEVIEWS_ORIGINAL_STRING where user_id != 'User_9';
    

    then created another stream that merges the two streams

    ksql> create stream merge_two AS select step1;
    insert into merge_two select * from step2;
    

    outcome

    1544165217141 | 50671 | Page_35 | User_2
    1544165217103 | 50661 | Page_10 | User_6
    1544165217580 | 50681 | Page_54 | User_2
    1544165217699 | 50691 | Page_45 | User_6
    1544165218104 | 50701 | Page_20 | User_8
    1544165218511 | 50711 | Page_13 | User_2
    1544165218931 | 50721 | Page_45 | User_4
    1544165219146 | 50731 | Page_68 | User_8
    1544165219505 | 50741 | Page_65 | User_4
    1544165219713 | 50751 | Page_74 | User_8
    1544165219787 | 50761 | Page_30 | User_5
    1544165220104 | 50771 | Page_72 | User_2
    1544165220421 | 50781 | Page_39 | User_2
    1544165220523 | 50791 | Page_55 | User_8
    1544165220645 | 50801 | Page_76 | User_6
    1544165220818 | 50821 | Page_46 | User_5
    1544165220781 | 50811 | Page_60 | User_4
    1544165220911 | 50831 | Page_24 | User_4
    1544165221102 | 50841 | PAGE_11 | User_9
    1544165221377 | 50851 | PAGE_95 | User_9
    1544165221685 | 50861 | PAGE_56 | User_9
    1544165222164 | 50871 | Page_50 | User_3
    1544165222233 | 50881 | Page_12 | User_8
    1544165222500 | 50891 | Page_18 | User_3
    1544165222913 | 50901 | Page_67 | User_5
    1544165223237 | 50911 | Page_30 | User_3
    1544165223665 | 50921 | Page_62 | User_8