# 啟動您的Datalab

Datalab為Google在大資料家族中針對連結各資料來源進行互動，進一步對資料進行檢視、分析、視覺化呈現的工具，更進一步可以搭配Machine Learning進行更進一步的運算。

## 事先準備工作

* 安裝gcloud工具
* 認證gcloud工具
* 設定ADC(Application Default Certification)
* 設定預設的zone與project id

gcloud的詳細設定部分，可以參考前面章節：[GCP指令列工具 - gcloud](https://github.com/gcpug-tw/google-cloud-platform-in-practice/tree/fb75f8263141bcca574cb9c93ba57cd623b0f7eb/datalab/gcloud-intro.md)。

## 啟動

收先，需要先行安裝gcloud component中的datalab的元件，可以透過下面指令來安裝：

```
gcloud components install datalab
```

安裝完成後，可以直接在gcloud的環境中直接使用datalab create來建置，如下：

```
$ datalab create simonlab
Creating the instance simonlab
Created [https://www.googleapis.com/compute/v1/projects/mitac-simonsu-2017/zones/asia-east1-c/instances/simonlab].
Connecting to simonlab.
This will create an SSH tunnel and may prompt you to create an rsa key pair.
Waiting for Datalab to be reachable at http://localhost:8081/

The connection to Datalab is now open and will remain until this command is killed.
You can connect to Datalab at http://localhost:8081/
```

建立過程會需要一些時間... 當一切建立無誤，則系統會搭配reverse proxy將雲端上的8081 port服務串連回本機，讓您可以使用 <http://localhost:8081/> 來連線datalab服務。

在建立好後，我們也可以透過list顯示專案中目前有多少的datalab主機可以使用：

```
$ datalab list
NAME      ZONE          MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS
simonlab  asia-east1-c  n1-standard-1               10.140.0.2   123.123.123.123  RUNNING
```

下面是實際連線上的狀態：

![](https://3161027175-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAi6osZ1qQ03eJeBJ3R%2F-LAi6qOiNHB4qVyC6dPI%2F-LAi6xYEC8NEyQLx4DE3%2Fdatalab-web-01.png?generation=1524413491800876\&alt=media)

## Datalab Tutorials

我們可以透過/datalab/dosc/turtorials來找到許多與GCP Data Source連線的範例，下面是一個BigQuery連線的畫面..

![](https://3161027175-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAi6osZ1qQ03eJeBJ3R%2F-LAi6qOiNHB4qVyC6dPI%2F-LAi6xZPphoAE0IN9jQo%2Fdatalab-tutorial.png?generation=1524413491852836\&alt=media)

我們可以開啟其中一個範例後，點選其中的一個查詢範例，然後點選最上方的執行按鈕，系統就會開始執行連接BigQuery的查詢動作...

![](https://3161027175-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAi6osZ1qQ03eJeBJ3R%2F-LAi6qOiNHB4qVyC6dPI%2F-LAi6x_dA8HnRCt0OWnO%2Fdatalab-run-bigquery.png?generation=1524413492406574\&alt=media)

接下來，執行結果會顯示在該段SQL下面...

![](https://3161027175-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAi6osZ1qQ03eJeBJ3R%2F-LAi6qOiNHB4qVyC6dPI%2F-LAi6xaWiYn4f3r8pJNu%2Fdataflow-run-bq-result.png?generation=1524413491931163\&alt=media)

## 停止Datalab

最後完成測試後，可以透過點選右上方人頭後出現的"Stop VM"的按鈕，將Datalab停止。

![](https://3161027175-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAi6osZ1qQ03eJeBJ3R%2F-LAi6qOiNHB4qVyC6dPI%2F-LAi6xb_CRv4dLV9CYKM%2Fdatalab-stop-vm.png?generation=1524413494074366\&alt=media)

Stop後的VM在GCE的介面中是以"Stopped"的狀態呈現，如果需要再次把它打開，可以試試從GCE介面中將該主機打開，然後透過datalab connect來連線服務...

```
$ datalab connect simonlab 
Connecting to simonlab.
This will create an SSH tunnel and may prompt you to create an rsa key pair.
Waiting for Datalab to be reachable at http://localhost:8081/

The connection to Datalab is now open and will remain until this command is killed.
You can connect to Datalab at http://localhost:8081/
```

連線上後，系統會打開瀏覽器，讓您直接連線使用。

如果最後需要刪除這個資源，可以直接使用delete指令將該資源刪除...

```
$ datalab delete simonlab
Deleting simonlab
The following instances will be deleted. Attached disks configured to
be auto-deleted will be deleted unless they are attached to any other
instances. Deleting a disk is irreversible and any data on the disk
will be lost.
 - [simonlab] in [asia-east1-c]

Do you want to continue (Y/n)?  Y

Deleted [https://www.googleapis.com/compute/v1/projects/mitac-simonsu-2017/zones/asia-east1-c/instances/simonlab].
```

請注意，刪除的動作將回連同硬碟一起刪除，如果有需要，請先進行備份。

## 參考

* 啟動Datalab: <https://cloud.google.com/datalab/docs/quickstarts>
* Datalab Github: <https://github.com/googledatalab/datalab>
