

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

# 使用魔術命令
<a name="notebooks-spark-magics"></a>

魔術命令或魔術是可以在筆記本儲存格中執行的特殊命令。例如，`%env` 會在筆記本工作階段中顯示環境變數。Athena 支援 IPython 6.0.3 中的魔術函數。

本節介紹了 Athena for Apache Spark 的一些關鍵魔術命令。
+  若要在 Athena 中查看這些魔術命令的清單，請在筆記本儲存格中執行命令 **%lsmagic**。
+ 如需有關在 Athena 筆記本中使用魔法建立圖形的資訊，請參閱 [使用魔術建立資料圖表](notebooks-spark-magics-graphs.md)。
+ 如需有關其他魔術命令的資訊，則請參閱 IPython 文件中的[內建魔術命令](https://ipython.readthedocs.io/en/stable/interactive/magics.html)。

**注意**  
目前，`%pip` 命令在執行時失敗。這是已知問題。

**Topics**
+ [儲存格魔術](notebooks-spark-magics-cell-magics.md)
+ [行魔術](notebooks-spark-magics-line-magics.md)
+ [圖形魔術](notebooks-spark-magics-graphs.md)

# 使用儲存格魔術
<a name="notebooks-spark-magics-cell-magics"></a>

編寫在多行上的魔術前面有一個雙百分比符號 (`%%`)，被稱為儲存格魔術函數或儲存格魔術。

## %%sql
<a name="notebooks-spark-magics-sql"></a>

此儲存格魔術允許直接執行 SQL 陳述式，而不必用 Spark SQL 語句來進行裝飾。該命令還可隱式呼叫傳回的 Dataframe 上的 `.show()`，進而顯示輸出。

![\[使用%%sql。\]](http://docs.aws.amazon.com/zh_tw/athena/latest/ug/images/notebooks-spark-magics-1.png)


`%%sql` 命令會自動將資料欄輸出截斷為 20 個字元的寬度。這目前無法設定。若要解決此限制，請使用下列完整語法，並相應地修改 `show` 方法的參數。

```
spark.sql("""YOUR_SQL""").show(n=number, truncate=number, vertical=bool)
```
+ **n**`int`，選用。要顯示的資料列數目。
+ **截斷** – `bool` 或 `int`，可選-如為 `true`，則會截斷超過 20 個資源的字串。當設定為大於 1 的數字時，會將長字串截斷為指定長度，並靠右對齊儲存格。
+ **垂直** – `bool`，選用。如為 `true`，則會垂直列印輸出資料列 (每個資料欄值一行)。

# 使用行魔術
<a name="notebooks-spark-magics-line-magics"></a>

單行上的魔術前面有一個百分比符號 (`%`)，被稱為行魔術函數或行魔術。

## %help
<a name="notebooks-spark-magics-help"></a>

顯示可用魔術命令的描述。

![\[使用%help。\]](http://docs.aws.amazon.com/zh_tw/athena/latest/ug/images/notebooks-spark-magics-2.png)


## %list\$1sessions
<a name="notebooks-spark-magics-list_sessions"></a>

列出與筆記本相關聯的工作階段。每個工作階段的資訊包括工作階段 ID、工作階段狀態，以及工作階段開始和結束的日期和時間。

![\[使用%list_sessions。\]](http://docs.aws.amazon.com/zh_tw/athena/latest/ug/images/notebooks-spark-magics-3.png)


## %session\$1id
<a name="notebooks-spark-magics-session_id"></a>

擷取目前的工作階段 ID。

![\[使用session_id。\]](http://docs.aws.amazon.com/zh_tw/athena/latest/ug/images/notebooks-spark-magics-4.png)


## %set\$1log\$1level
<a name="notebooks-spark-magics-set_log_level"></a>

設定或重設記錄器，以使用指定的日誌級別。可能值為 `DEBUG`、`ERROR`、`FATAL`、`INFO` 以及 `WARN` 或 `WARNING`。值必須為大寫，不得用單引號或雙引號括住。

![\[使用%set_log_level。\]](http://docs.aws.amazon.com/zh_tw/athena/latest/ug/images/notebooks-spark-magics-5.png)


## %status
<a name="notebooks-spark-magics-status"></a>

描述目前的工作階段。輸出包括工作階段 ID、工作階段狀態、工作群組名稱、PySpark 引擎版本和工作階段開始時間。此魔術命令需要作用中的工作階段，以擷取工作階段詳細資訊。

以下是狀態的可能值：

**正在建立** – 工作階段正在啟動中，包括取得資源。

**已建立** – 工作階段已開始。

**閒置** – 工作階段能夠接受計算。

**忙碌** – 工作階段正在處理另一項作業，且無法接受計算。

**正在終止** – 工作階段正在關閉。

**已終止** – 工作階段及其資源不再執行。

**降級** – 工作階段沒有健康的協調者。

**失敗** – 由於失敗，工作階段及其資源不再執行。

![\[使用%status。\]](http://docs.aws.amazon.com/zh_tw/athena/latest/ug/images/notebooks-spark-magics-6.png)


# 使用魔術建立資料圖表
<a name="notebooks-spark-magics-graphs"></a>

本節中的行魔術專門用於為特定類型的資料渲染資料或與圖形庫結合使用。

## %table
<a name="notebooks-spark-magics-graphs-table"></a>

您可以使用 `%table` 魔術命令，以資料表格式顯示 dataframe 資料。

下下列範例建立了一個包含兩個資料欄和三個資料列資料的 dataframe，然後會以資料表格式顯示資料。

![\[使用 %table 魔術命令。\]](http://docs.aws.amazon.com/zh_tw/athena/latest/ug/images/notebooks-spark-magics-graphs-1.png)


## %matplot
<a name="notebooks-spark-magics-graphs-matplot"></a>

[Matplotlib](https://matplotlib.org/) 是用於在 Python 中建立靜態、動畫和交互式可視化的綜合程式庫。在將 matplotlib 程式庫匯入筆記本儲存格之後，您可以使用 `%matplot` 魔術指令來建立圖形。

下列範例會匯入 matplotlib 程式庫，建立一組 x 和 y 座標，然後使用 `%matplot` 魔術命令建立點的圖形。

```
import matplotlib.pyplot as plt 
x=[3,4,5,6,7,8,9,10,11,12] 
y= [9,16,25,36,49,64,81,100,121,144] 
plt.plot(x,y) 
%matplot plt
```

![\[使用 %matplot 魔術命令。\]](http://docs.aws.amazon.com/zh_tw/athena/latest/ug/images/notebooks-spark-magics-graphs-2.png)


### 同時使用 matplotlib 和 seaborn 程式庫
<a name="notebooks-spark-magics-graphs-using-the-matplotlib-and-seaborn-libraries-together"></a>

[Seaborn](https://seaborn.pydata.org/tutorial/introduction) 是一個用 Python 製作統計圖形程式庫。它以 matplotlib 為建置基礎，並與 [pandas](https://pandas.pydata.org/) (Python 資料分析) 資料結構緊密整合。您也可以使用 `%matplot` 魔術命令來渲染 seaborn 資料。

下列範例同時使用了 matplotlib 和 seaborn 程式庫來建立一個簡單的長條圖。

```
import matplotlib.pyplot as plt 
import seaborn as sns 

x = ['A', 'B', 'C'] 
y = [1, 5, 3] 

sns.barplot(x, y) 
%matplot plt
```

![\[使用 %matplot 來渲染 seaborn 資料。\]](http://docs.aws.amazon.com/zh_tw/athena/latest/ug/images/notebooks-spark-magics-graphs-3.png)


## %plotly
<a name="notebooks-spark-magics-graphs-plotly"></a>

[Plotly](https://plotly.com/python/) 是一個 Python 的開放原始碼圖形程式庫，您可以使用它來製作交互式圖形。您可使用 `%ploty` 魔法命令來渲染 ploty 資料。

下列範例基於股票價格資料使用 [StringIO](https://docs.python.org/3.13/library/io.html#io.StringIO)、plotly 和 pandas 程式庫來建立 2015 年 2 月和 3 月的股票活動圖表。

```
from io import StringIO 
csvString = """ 
Date,AAPL.Open,AAPL.High,AAPL.Low,AAPL.Close,AAPL.Volume,AAPL.Adjusted,dn,mavg,up,direction 
2015-02-17,127.489998,128.880005,126.919998,127.830002,63152400,122.905254,106.7410523,117.9276669,129.1142814,Increasing 
2015-02-18,127.629997,128.779999,127.449997,128.720001,44891700,123.760965,107.842423,118.9403335,130.0382439,Increasing 
2015-02-19,128.479996,129.029999,128.330002,128.449997,37362400,123.501363,108.8942449,119.8891668,130.8840887,Decreasing 
2015-02-20,128.619995,129.5,128.050003,129.5,48948400,124.510914,109.7854494,120.7635001,131.7415509,Increasing 
2015-02-23,130.020004,133,129.660004,133,70974100,127.876074,110.3725162,121.7201668,133.0678174,Increasing 
2015-02-24,132.940002,133.600006,131.169998,132.169998,69228100,127.078049,111.0948689,122.6648335,134.2347981,Decreasing 
2015-02-25,131.559998,131.600006,128.149994,128.789993,74711700,123.828261,113.2119183,123.6296667,134.0474151,Decreasing 
2015-02-26,128.789993,130.869995,126.610001,130.419998,91287500,125.395469,114.1652991,124.2823333,134.3993674,Increasing 
2015-02-27,130,130.570007,128.240005,128.460007,62014800,123.510987,114.9668484,124.8426669,134.7184854,Decreasing 
2015-03-02,129.25,130.279999,128.300003,129.089996,48096700,124.116706,115.8770904,125.4036668,134.9302432,Decreasing 
2015-03-03,128.960007,129.520004,128.089996,129.360001,37816300,124.376308,116.9535132,125.9551669,134.9568205,Increasing 
2015-03-04,129.100006,129.559998,128.320007,128.539993,31666300,123.587892,118.0874253,126.4730002,134.8585751,Decreasing 
2015-03-05,128.580002,128.75,125.760002,126.410004,56517100,121.539962,119.1048311,126.848667,134.5925029,Decreasing 
2015-03-06,128.399994,129.369995,126.260002,126.599998,72842100,121.722637,120.190797,127.2288335,134.26687,Decreasing 
2015-03-09,127.959999,129.570007,125.059998,127.139999,88528500,122.241834,121.6289771,127.631167,133.6333568,Decreasing 
2015-03-10,126.410004,127.220001,123.800003,124.510002,68856600,119.71316,123.1164763,127.9235004,132.7305246,Decreasing 
""" 
csvStringIO = StringIO(csvString) 
 
from io import StringIO 
import plotly.graph_objects as go 
import pandas as pd 
from datetime import datetime 
df = pd.read_csv(csvStringIO) 
fig = go.Figure(data=[go.Candlestick(x=df['Date'], 
open=df['AAPL.Open'], 
high=df['AAPL.High'], 
low=df['AAPL.Low'], 
close=df['AAPL.Close'])]) 
%plotly fig
```

![\[使用 %ploty 魔術命令。\]](http://docs.aws.amazon.com/zh_tw/athena/latest/ug/images/notebooks-spark-magics-graphs-4.png)
