---
# System prepended metadata

title: 2020金象盃 大數據 教學範例
tags: [範例, 教學]

---

# 2020金象盃 大數據 教學範例

> [name=Junter.wjs,] 作者：JunterTW 翻譯：XiangOuO

## 語法

#### ==[將資料載入變數]==
變數 = LOAD '資料集' USING PigStorage as (欄位:型態);

> 範例
```sql
data = LOAD '/dataset/999004/999004.tsv' USING PigStorage ('\t') as (userid:chararray,gender:chararray,age:int,country:chararray,registered:chararray);
```

#### ==[查詢資料表Schema]==
describe 變數; 

> 範例
```sql
describe data;
```

#### ==[顯示資料]==
dump 變數;

> 範例
```sql
dump data;
```

#### ==[查詢特定欄位] / [FOREACH] 取的每一筆資料==
變數 = FOREACH 變數 GENERATE 欄位;

> 範例
```sql
data1 = FOREACH data GENERATE userid; /*[FOREACH] 取的每一筆資料
```

#### ==[新增欄位]==
變數 = FOREACH 變數 GENERATE 欄位,999;

> 範例
```sql
data1 = FOREACH data GENERATE userid,999;
```

#### ==[欄位轉型]==
變數 = FOREACH 變數 GENERATE (型態)$0;

> 範例
```sql
data1 = FOREACH data GENERATE (chararray)$0,$1,$2;
```

#### ==[欄位重新命名]==
變數 = FOREACH 變數 GENERATE $0 as 欄位名稱;

> 範例
```sql
data1 = FOREACH data GENERATE userid , gender ,$2 as number;
```

#### ==[欄位重新命名與轉型]==
變數 = FOREACH 變數 GENERATE $0 as 欄位名稱:型態;

> 範例
```sql
data1 = FOREACH data GENERATE $0 as userid:chararray , $1 as gender:chararray;
```

#### ==條件過濾 / 語法==

變數 = FILTER 變數 BY (條件);
變數 = FILTER 變數 BY 欄位 MATCHES 正則表示法;

> 範例
```sql
data1 = FILTER data BY (gender == 'f');
data1 = FILTER data BY country MATCHES 'B[A-Za-z0-9]*' B開頭所有字
```

#### ==群組資料 / 語法==

變數 = GROUP 變數 BY 欄位;
變數 = GROUP 變數 ALL;

> 範例
```sql
data1 = GROUP data BY country;
data1 = GROUP data All;
```

---

:::warning
:loudspeaker: 程式碼如有錯誤！請自行修正或聯絡我們。
:email: 4a990025@stust.edu.tw / c109181108@nkust.edu.tw
:::
###### Tags: `範例` `教學`