文档

KEYVALUE

更新时间:

当您需要解析数据表存储的键值对字符串,并提取出键对应的值时,MaxCompute支持使用KEYVALUE函数,将字符串str按照split1分成Key-Value对,并按split2将Key-Value对分开,返回key所对应的Value。本文为您介绍KEYVALUE函数的说明和使用示例。

命令格式

keyvalue(string <str>,[string <split1>,string <split2>,] string <key>)
keyvalue(string <str>,string <key>) 

参数说明

  • str:必填。STRING类型。待拆分的字符串。

  • split1split2:可选。STRING类型。用于作为分隔符的字符串,按照指定的两个分隔符拆分源字符串。如果表达式中没有指定这两项,默认split1";"split2":"。当某个被split1拆分后的字符串中有多个split2时,返回结果未定义。

  • key:必填。STRING类型。将字符串按照split1split2拆分后,返回key值对应的Value。

返回值说明

返回STRING类型。返回规则如下:

  • split1split2值为NULL时,返回NULL。

  • strkey值为NULL或没有匹配的key时,返回NULL。

  • 如果有多个Key-Value匹配,返回第一个匹配上的key对应的Value。

使用示例

  • 示例1:将字符串0:1\;1:2拆分为Key-Value对,返回Key值1对应的Value。命令示例如下。

    --返回2。
    select keyvalue('0:1\;1:2', 1);

    没有指定split1split2,默认split1";"split2":"

    经过split1拆分后,Key-Value对为0:1\,1:2。经过split2拆分后变为如下。

    0 1/  
    1 2

    返回Key为1所对应的Value值2。

  • 示例2:将字符串“\;decreaseStore:1\;xcard:1\;isB2C:1\;tf:21910\;cart:1\;shipping:2\;pf:0\;market:shoes\;instPayAmount:0\;”按照“\;”拆分为Key-Value对,再按照":"将Key-Value分开,返回Key值tf对应的Value。命令示例如下。

    --返回21910。
    select keyvalue("\;decreaseStore:1\;xcard:1\;isB2C:1\;tf:21910\;cart:1\;shipping:2\;pf:0\;market:shoes\;instPayAmount:0\;","\;",":","tf");

    “\;decreaseStore:1\;xcard:1\;isB2C:1\;tf:21910\;cart:1\;shipping:2\;pf:0\;market:shoes\;instPayAmount:0\;”按照“\;”拆分后,得出的Key-Value对如下所示。

    decreaseStore:1,xcard:1,isB2C:1,tf:21910,cart:1,shipping:2,pf:0,market:shoes,instPayAmount:0 

    按照":"拆分后,结果如下所示。

    decreaseStore 1  
    xcard 1  
    isB2C 1  
    tf 21910  
    cart 1  
    shipping 2  
    pf 0  
    market shoes  
    instPayAmount 0

    返回Key为tf对应的Value值21910。

相关函数

KEYVALUE函数属于字符串函数,更多查找字符串、转换字符串格式的相关函数请参见字符串函数

  • 本页导读 (1)
文档反馈