KEYS 函式

從物件資料類型資料欄中擷取鍵值,並將它們儲存於字串值陣列中。

基本用法

資料欄參照範例:

derive type:single value:KEYS('object1') as:'objKeys'

輸出:產生新的 objKeys 資料欄,其中含有 object1 資料欄中所有鍵/值物件的值陣列。

陣列常值參照範例:

請參閱下方的範例。

語法

derive type:single value:KEYS(obj_col)

引數是否必要?資料類型說明
obj_col字串或物件將擷取鍵至陣列的資料欄名稱或物件常值

若要進一步瞭解語法標準,請參閱語言文件語法附註

obj_col

您想擷取鍵至陣列的物件常值或物件資料欄名稱。

使用須知:

是否必要?資料類型示例值
物件常值或資料欄參照myObj

範例

範例:基本鍵範例

來源:

下列資料集含有個別功能的設定區塊。這些範例區塊屬於「物件」類型。

為方便使用者瞭解,系統已套用代碼格式至物件資料。

FeatureNameConfiguration
Whiz Widget

{
 "enabled": "true",
 "maxRows": "1000",
 "maxCols": "100"
}
Magic Button

{
 "enabled": "false",
 "startDirectory": "/home",
 "maxDepth": "15"
}
Happy Path Finder

{
 "enabled": "true"
}

轉換:

下列轉換指令會擷取 Configuration 資料欄中來自物件資料的鍵值。

derive type:single value: KEYS(Configuration) as: 'keys_Configuration'

結果:

keys_Configuration 資料欄含有包含鍵值的陣列。

FeatureNameConfigurationkeys_Configuration
Whiz Widget

{
 "enabled": "true",
 "maxRows": "1000",
 "maxCols": "100"
}

["enabled","maxRows","maxCols"]
Magic Button

{
 "enabled": "false",
 "startDirectory": "/home",
 "maxDepth": "15"
}

["enabled","startDirectory","maxDepth"]
Happy Path Finder

{
 "enabled": "true"
}

["enabled"]

範例:建立產品屬性的物件

來源:

您的資料集含有主要產品資料及產品屬性,這些資料儲存在由索引鍵和值組成的兩個陣列中。

ProdIdProdCategoryProdNameProdKeysProdProperties
S001襯衫圓領 T 恤["type", "color", "fabric", "sizes"]["crew","blue","cotton","S,M,L","in stock","padded"]
S002襯衫V 領 T 恤["type", "color", "fabric", "sizes"]["v-neck","white","blend","S,M,L,XL","in stock","discount - seasonal"]
S003襯衫坦克背心["type", "color", "fabric", "sizes"]["tank","red","mesh","XS,S,M","discount - clearance","in stock"]
S004襯衫高領衫["type", "color", "fabric", "sizes"]["turtle","black","cotton","M,L,XL","out of stock","padded"]

轉換:

將上述資料載入轉換頁面時,您可能需要清理這兩個陣列的資料欄。

您可以使用下列轉換指令,將第一個陣列的第一個元素對應為第二個陣列中第一個元素的索引鍵,此為第二陣列的值。您可能會發現索引鍵數目和值的數目並不一致。針對第二個陣列中的額外元素,系統會使用 ProdMiscProperties 的預設索引鍵:

derive type:single value: ARRAYSTOMAP(ProdProperties, ProdValues, 'ProdMiscProperties') as: 'prodPropertyMap'

您現在可以使用下列步驟來產生新版本的索引鍵:

drop col:ProdKeys

derive type:single value:KEYS(prodPropertyMap) as:'ProdKeys'

結果:

ProdIdProdCategoryProdNameProdKeysProdPropertiesprodPropertyMap
S001襯衫圓領 T 恤["type", "color", "fabric", "sizes","ProdMiscProperties"]["crew","blue","cotton","S,M,L","in stock","padded"]

{
  "type": [ "crew" ],
  "color": [ "blue" ],
  "fabric": [ "cotton" ],
  "sizes": [ "S,M,L" ],
  "ProdMiscProperties": [ "in stock", "padded" ] }
S002襯衫V 領 T 恤["type", "color", "fabric", "sizes","ProdMiscProperties"]["v-neck","white","blend","S,M,L,XL","in stock","discount - seasonal"]

{
  "type": [ "v-neck" ],
  "color": [ "white" ],
  "fabric": [ "blend" ],
  "sizes": [ "S,M,L,XL" ],
  "ProdMiscProperties": [ "in stock", "discount - seasonal" ] }
S003襯衫坦克背心["type", "color", "fabric", "sizes","ProdMiscProperties"]["tank","red","mesh","XS,S,M","discount - clearance","in stock"]

{
  "type": [ "tank" ],
  "color": [ "red" ],
  "fabric": [ "mesh" ],
  "sizes": [ "XS,S,M" ],
  "ProdMiscProperties": [ "discount - clearance", "in stock" ] }
S004襯衫高領衫["type", "color", "fabric", "sizes","ProdMiscProperties"]["turtle","black","cotton","M,L,XL","out of stock","padded"]

{
  "type": [ "turtle" ],
  "color": [ "black" ],
  "fabric": [ "cotton" ],
  "sizes": [ "M,L,XL" ],
  "ProdMiscProperties": [ "out of stock", "padded" ] }

本頁內容對您是否有任何幫助?請提供意見:

傳送您對下列選項的寶貴意見...

這個網頁
Google Cloud Dataprep 說明文件
需要協助嗎?請前往我們的支援網頁