天天看點

字元串函數split、array_contains

1、split(string str, string regex)函數:

TDW官方文檔上的解釋:

  函數說明: 

  Splits str around occurances that match

  用法示例: 

  SELECT split(‘ABCD:C:D:E’,’:’)[2] returns ‘D’.

那麼,是否可以按照多個字元進行分割呢,答案是可以的。在指定多個分隔符的時候,需要用’|’符号隔開:

SELECT split('hello world,zhuoru',' |,')1

傳回的結果是一個數組:[[“hello”,”world”,”zhuoru”]]

2、array_contains(array arr, element)函數:

TDW官方文檔上的解釋:

  函數說明: 

  Returns if the element is in the array, the element must hava same type as array

  用法示例: 

  SELECT array_CONTAINS(ARRAY(0,1),0)  returns true 

  SELECT array_CONTAINS(split(‘0=1’,’=’),’0’) returns true

3、兩個函數的實際應用:

在處理SDK日志的時候,日志中的other字段有的有leakscan屬性,有的沒有,leakscan和它的值之間以^B間隔,leakscan和其他屬性之間以^B間隔。有leakscan并且其值為’-‘表示是正常通路(沒有leakscan的情形全部認為是正常通路),如果是漏洞掃描通路,leakscan的值不為空且不為’-‘。最終要完成的任務是查詢出正常通路的記錄。我是這樣寫查詢SQL的:

SELECT

        *

    FROM

        mig_tdbank :: qqmap_openplatform_dsl_apikey_fht0

    WHERE

        tdbank_imp_date >= MACRO_DATA_DATE || '12'

        AND tdbank_imp_date <= MACRO_DATA_DATE || '13'

        AND SUBSTR(other, 8, 3) != '400'

        AND (

            array_CONTAINS(

                split(

                    other

                    ,'\\002'

                )

                ,'leakscan' || '\\003' || '-'

            ) = TRUE

            OR array_CONTAINS(

                split(

                    other

                    ,'\\002|\\003'

                )

                ,'leakscan'

            ) = FALSE

        ) limit 1000123456789101112131415161718192021222324

在where判斷條件中,

array_CONTAINS(

    split(

        other

        ,'\\002'

    )

    ,'leakscan' || '\\003' || '-'

) = TRUE1234567

表示有leakscan并且其值為’-‘的情況。

array_CONTAINS(

    split(

        other

        ,'\\002|\\003'

    )

    ,'leakscan'

) = FALSE

array_contains 分析函數使用示範

-- ========================================================================================

-- Purpose : array_contains 分析函數使用示範

------------------------------------ Change Log -------------------------------------------

-- Date Generated   Updated By     Description

-------------------------------------------------------------------------------------------   

-- 2018-12-26       shujuxiong     Initial Version

-- ========================================================================================

-- status_code枚舉:1生效中 2當機中 3失效中   

select

user_id

,

count

(*)

as

card_number   

-- 使用過的卡數

-- 隻要任意一張卡有效即判定為VIP有效

,

case

when

array_contains(collect_set(status_code),

cast

(1

as

smallint

))

then

1

else

end

effective_flag  

-- 卡有效辨別

from

edw_users.dwd_edw_user_vipcard_df 

-- 使用者V國際電話預付卡購買使用全量表

where

dt =

'${dt}'

and

user_id > 0

and

deleted_flag =

'N'

group

by

user_id

;

原文:https://blog.csdn.net/liluotuo/article/details/43986013