範例 - ARRAYSTOMAP 函式

來源:

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

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"]

轉換:

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

您可以使用下列轉換指令,將第一個陣列的第一個元素對應為第二個陣列中第一個元素的索引鍵,此為第二陣列的值。您可能會發現索引鍵數目和值的數目並不一致。針對第二個陣列中的額外元素,系統會使用 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 說明文件
需要協助嗎?請前往我們的支援網頁