

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Hive 檢視
<a name="hive-views"></a>

您可以使用 Athena 來查詢在外部 Apache Hive 中繼存放區中的現有檢視。Athena 在執行時間即時翻譯您的檢視，而無需變更原始檢視或存放翻譯。

例如，假設您有一個 Hive 檢視 (如以下所示)，其使用了 Athena 不支援的語法，如 `LATERAL VIEW explode()`：

```
CREATE VIEW team_view AS 
SELECT team, score 
FROM matches 
LATERAL VIEW explode(scores) m AS score
```

Athena 將 Hive 檢視查詢字串翻譯為 Athena 可以執行的以下陳述式：

```
SELECT team, score
FROM matches
CROSS JOIN UNNEST(scores) AS m (score)
```

如需將外部 Hive 中繼存放區連線到 Athena 的相關資訊，請參閱[使用外部 Hive 中繼存放區](connect-to-data-source-hive.md)。

## 考量和限制
<a name="hive-views-considerations-and-limitations"></a>

當從 Athena 查詢 Hive 檢視時，請考慮下列幾點：
+ Athena 不支援建立 Hive 檢視。您可以在外部 Hive 中繼存放區中建立 Hive 檢視，然後您可以從 Athena 查詢該檢視。
+ Athena 不支援 Hive 檢視的自訂 UDF。
+ 由於 Athena 主控台中存在已知問題，Hive 檢視顯示在資料表清單下，而不是檢視清單下。
+ 雖然翻譯過程是自動的，但某些 Hive 函數在 Hive 檢視中不受支援或需要特殊處理。如需詳細資訊，請參閱下一節。

## Hive 函數支援限制
<a name="hive-views-function-limitations"></a>

本節重點介紹了 Athena 不支援 Hive 檢視或需要特殊處理的 Hive 函數。目前，由於 Athena 主要支援 Hive 2.2.0 中的函數，因此無法使用僅在更高版本 (如 Hive 4.0.0) 中可用的函數。如需 Hive 函數的完整清單，請參閱 [Hive language manual UDF](https://cwiki.apache.org/confluence/display/hive/languagemanual+udf) (Hive 語言手冊 UDF)。

### 彙總函數
<a name="hive-views-aggregate-functions"></a>

#### 需要特殊處理的彙總函數
<a name="hive-views-aggregate-functions-special-handling"></a>

Hive 檢視的以下彙總函數需要特殊處理。
+ **Avg** – 使用 `avg(CAST(i AS DOUBLE))` 而不是 `avg(INT i)`。

#### 不支援彙總函數
<a name="hive-views-aggregate-functions-not-supported"></a>

在 Athena 中，Hive 檢視不支援以下 Hive 彙總函數。

```
covar_pop
histogram_numeric
ntile
percentile
percentile_approx
```

在 Athena 中，Hive 檢視不支援迴歸函數，例如 `regr_count`、`regr_r2` 和 `regr_sxx`。

### 不支援日期函數
<a name="hive-views-date-functions-not-supported"></a>

在 Athena 中，Hive 檢視不支援以下 Hive 日期函數。

```
date_format(date/timestamp/string ts, string fmt)
day(string date)
dayofmonth(date)
extract(field FROM source)
hour(string date)
minute(string date)
month(string date)
quarter(date/timestamp/string)
second(string date)
weekofyear(string date)
year(string date)
```

### 不支援遮罩函數
<a name="hive-views-masking-functions-not-supported"></a>

在 Athena 中，Hive 檢視不支援 Hive 遮罩函數，例如 `mask()` 和 `mask_first_n()`。

### 其他函數
<a name="hive-views-miscellaneous-functions"></a>

#### 需要特殊處理的其他函數
<a name="hive-views-supported-miscellaneous-functions-special-handling"></a>

Hive 檢視的以下其他函數需要特殊處理。
+ **md5** – Athena 支援 `md5(binary)` 而非 `md5(varchar)`。
+ **Explode** – 在以下語法中使用時，Athena 支援 `explode`：

  ```
  LATERAL VIEW [OUTER] EXPLODE(<argument>)
  ```
+ **Posexplode** – 在以下語法中使用時，Athena 支援 `posexplode`：

  ```
  LATERAL VIEW [OUTER] POSEXPLODE(<argument>)           
  ```

  在 `(pos, val)` 輸出中，Athena 將 `pos` 資料欄視為 `BIGINT`。因此，您可能需要將 `pos` 資料欄轉換為 `BIGINT`，以避免檢視過時。以下範例說明此技術。

  ```
  SELECT CAST(c AS BIGINT) AS c_bigint, d 
  FROM table LATERAL VIEW POSEXPLODE(<argument>) t AS c, d
  ```

#### 不支援的其他函數
<a name="hive-views-unsupported-miscellaneous-functions-not-supported"></a>

在 Athena 中，Hive 檢視不支援以下 Hive 函數。

```
aes_decrypt
aes_encrypt
current_database
current_user
inline
java_method
logged_in_user
reflect
sha/sha1/sha2
stack
version
```

### 運算子
<a name="hive-views-operators"></a>

#### 需要特殊處理的運算子
<a name="hive-views-operators-special-handling"></a>

Hive 檢視的以下運算子需要特殊處理。
+ **求餘運算子 (%)** – 因為 `DOUBLE` 類型隱含地轉換為 `DECIMAL(x,y)`，以下語法可能會導致 View is stale (檢視過時) 錯誤訊息：

  ```
  a_double % 1.0 AS column
  ```

  若要解決此問題，請使用 `CAST`，如下列範例所示。

  ```
  CAST(a_double % 1.0 as DOUBLE) AS column
  ```
+ **除法運算子 (/)** – 在 Hive 中，`int` 除以 `int` 等於 `double`。在 Athena 中，相同的運算會產生一個遭截斷的 `int`。

#### 不支援的運算子
<a name="hive-views-operators-not-supported"></a>

Athena 不支援 Hive 檢視的以下運算子。

**\$1A** – 位元 `NOT`

**A ^ b** – 位元 `XOR`

**A & b** – 位元 `AND`

**A \$1 b** – 位元 `OR`

**A <=> b** – 對於非 null 運算元，會傳回與等於 (`=`) 運算子相同的結果。如果兩個皆為 `NULL`，則會傳回 `TRUE`；如果其中一個是 `NULL`，則會傳回 `FALSE`。

### 字串函數
<a name="hive-views-string-functions"></a>

#### 需要特殊處理的字串函數
<a name="hive-views-string-functions-special-handling"></a>

Hive 檢視的以下 Hive 字串函數需要特殊處理。
+ **chr(bigint\$1double a)** – Hive 允許負值引數；Athena 則不允許。
+ **instr(string str, string substr)** – 因為 `instr` 函數的 Athena 映射會傳回 `BIGINT` 而不是 `INT`，請使用以下語法：

  ```
  CAST(instr(string str, string substr) as INT)         
  ```

  如果沒有這個步驟，檢視將被視為過時。
+ **length(string a)** – 因為 `length` 函數的 Athena 映射會傳回 `BIGINT` 而不是 `INT`，請使用以下語法，以使檢視不會被視為過時：

  ```
  CAST(length(string str) as INT)
  ```

#### 不支援字串函數
<a name="hive-views-string-functions-not-supported"></a>

在 Athena 中，Hive 檢視不支援以下 Hive 字串函數。

```
ascii(string str)
character_length(string str)
decode(binary bin, string charset)
encode(string src, string charset)
elt(N int,str1 string,str2 string,str3 string,...)
field(val T,val1 T,val2 T,val3 T,...)
find_in_set(string str, string strList)
initcap(string A)
levenshtein(string A, string B)
locate(string substr, string str[, int pos])
octet_length(string str)
parse_url(string urlString, string partToExtract [, string keyToExtract])
printf(String format, Obj... args)
quote(String text)
regexp_extract(string subject, string pattern, int index)
repeat(string str, int n)
sentences(string str, string lang, string locale)
soundex(string A)
space(int n)
str_to_map(text[, delimiter1, delimiter2])
substring_index(string A, string delim, int count)
```

### 不支援 XPath 函數
<a name="hive-views-xpath-functions-not-supported"></a>

在 Athena 中，Hive 檢視不支援 Hive XPath 函數，例如 `xpath`、`xpath_short` 和 `xpath_int`。

## 疑難排解
<a name="hive-views-troubleshooting"></a>

當您在 Athena 中使用 Hive 檢視時，您可能會遇到以下問題：
+ **檢視 *<檢視名稱>* 已過時** – 此訊息通常表示 Hive 和 Athena 中的檢視類型不符。如果 [Hive LanguageManual UDF](https://cwiki.apache.org/confluence/display/hive/languagemanual+udf) (Hive 語言手冊 UDF) 和 [Presto functions and operators](https://prestodb.io/docs/current/functions.html) (Presto 函數和運算子) 文件中的相同函數具有不同的簽章，請嘗試轉換不符的資料類型。
+ **未註冊函數** – Athena 目前不支援此函數。如需詳細資訊，請參閱本文件的前述資訊。