チュートリアル:Azure Databricks を使用してデータの抽出、変換、読み込みを行うTutorial: Extract, transform, and load data by using Azure Databricks
このチュートリアルでは、Azure Databricks を使用して ETL (データの抽出、変換、読み込み) 操作を実行します。In this tutorial, you perform an ETL (extract, transform, and load data) operation by using Azure Databricks. Azure Data Lake Storage Gen2 から Azure Databricks にデータを抽出し、Azure Databricks でそのデータに対する変換を実行した後、変換されたデータを Azure SQL Data Warehouse に読み込みます。You extract data from Azure Data Lake Storage Gen2 into Azure Databricks, run transformations on the data in Azure Databricks, and load the transformed data into Azure SQL Data Warehouse.
このチュートリアルの手順では、Azure Databricks 用の SQL Data Warehouse コネクタを使って Azure Databricks にデータを転送します。The steps in this tutorial use the SQL Data Warehouse connector for Azure Databricks to transfer data to Azure Databricks. その後、このコネクタによって、Azure Databricks クラスターと Azure SQL Data Warehouse の間で転送されるデータの一時記憶域として Azure Blob Storage が使用されます。This connector, in turn, uses Azure Blob Storage as temporary storage for the data being transferred between an Azure Databricks cluster and Azure SQL Data Warehouse.
次の図に、アプリケーション フローを示します。The following illustration shows the application flow:
このチュートリアルに含まれるタスクは次のとおりです。This tutorial covers the following tasks:
- Azure Databricks サービスを作成する。Create an Azure Databricks service.
- Azure Databricks で Spark クラスターを作成する。Create a Spark cluster in Azure Databricks.
- Data Lake Storage Gen2 アカウントでファイル システムを作成する。Create a file system in the Data Lake Storage Gen2 account.
- サンプル データを Azure Data Lake Storage Gen2 アカウントにアップロードする。Upload sample data to the Azure Data Lake Storage Gen2 account.
- サービス プリンシパルを作成する。Create a service principal.
- Azure Data Lake Storage Gen2 アカウントからデータを抽出する。Extract data from the Azure Data Lake Storage Gen2 account.
- Azure Databricks でデータを変換する。Transform data in Azure Databricks.
- Azure SQL Data Warehouse にデータを読み込む。Load data into Azure SQL Data Warehouse.
Azure サブスクリプションがない場合は、開始する前に無料アカウントを作成してください。If you don’t have an Azure subscription, create a free account before you begin.
注意
Azure 無料試用版サブスクリプションを使用してこのチュートリアルを実行することはできません。This tutorial cannot be carried out using Azure Free Trial Subscription. 無料アカウントをお持ちの場合は、お使いのプロファイルにアクセスし、サブスクリプションを [従量課金制] に変更します。If you have a free account, go to your profile and change your subscription to pay-as-you-go. 詳細については、Azure 無料アカウントに関するページをご覧ください。For more information, see Azure free account. 次に、リージョン内の vCPU について使用制限を削除し、クォータの増加を依頼します。Then, remove the spending limit, and request a quota increase for vCPUs in your region. Azure Databricks ワークスペースを作成するときに、 [Trial (Premium - 14-Days Free DBUs)](試用版 (Premium - 14 日間の無料 DBU)) の価格レベルを選択し、ワークスペースから 14 日間無料の Premium Azure Databricks DBU にアクセスできるようにします。When you create your Azure Databricks workspace, you can select the Trial (Premium - 14-Days Free DBUs) pricing tier to give the workspace access to free Premium Azure Databricks DBUs for 14 days.
前提条件Prerequisites
このチュートリアルを始める前に、以下のタスクを完了します。Complete these tasks before you begin this tutorial:
Azure SQL データ ウェアハウスを作成し、サーバーレベルのファイアウォール規則を作成して、サーバー管理者としてサーバーに接続します。「クイック スタート:Azure Portal で Azure SQL データ ウェアハウスを作成し、クエリを実行する」を参照してください。Create an Azure SQL data warehouse, create a server-level firewall rule, and connect to the server as a server admin. See Quickstart: Create and query an Azure SQL data warehouse in the Azure portal.
Azure SQL データ ウェアハウスに使用するマスター キーを作成します。Create a master key for the Azure SQL data warehouse. 「データベース マスター キーの作成」を参照してください。See Create a database master key.
Azure Blob Storage アカウントを作成し、そこにコンテナーを作成します。Create an Azure Blob storage account, and a container within it. また、ストレージ アカウントにアクセスするためのアクセス キーを取得します。Also, retrieve the access key to access the storage account. 「クイック スタート:Azure portal を使用して BLOB をアップロード、ダウンロード、および一覧表示する」を参照してください。See Quickstart: Upload, download, and list blobs with the Azure portal.
Azure Data Lake Storage Gen2 ストレージ アカウントを作成します。Create an Azure Data Lake Storage Gen2 storage account. 「クイック スタート:Azure Data Lake Storage Gen2 ストレージ アカウントを作成する」を参照してください。See Quickstart: Create an Azure Data Lake Storage Gen2 storage account.
サービス プリンシパルを作成する。Create a service principal. 「方法:リソースにアクセスできる Azure AD アプリケーションとサービス プリンシパルをポータルで作成する」のガイダンスに従って、サービス プリンシパルを作成します。See How to: Use the portal to create an Azure AD application and service principal that can access resources.
この記事の手順を実行する際に、いくつかの特定の作業を行う必要があります。There's a couple of specific things that you'll have to do as you perform the steps in that article.
記事の「アプリケーションをロールに割り当てる」セクションの手順を行うときは、必ず、Data Lake Storage Gen2 アカウントのスコープ内で ストレージ BLOB データ共同作成者ロールをサービス プリンシパルに割り当ててください。When performing the steps in the Assign the application to a role section of the article, make sure to assign the Storage Blob Data Contributor role to the service principal in the scope of the Data Lake Storage Gen2 account. 親リソース グループまたはサブスクリプションにロールを割り当てる場合、それらのロール割り当てがストレージ アカウントに伝達されるまで、アクセス許可関連のエラーが発生します。If you assign the role to the parent resource group or subscription, you'll receive permissions-related errors until those role assignments propagate to the storage account.
アクセス制御リスト (ACL) を使用して特定のファイルまたはディレクトリにサービス プリンシパルを関連付ける場合は、「Azure Data Lake Storage Gen2 のアクセス制御」を参照してください。If you'd prefer to use an access control list (ACL) to associate the service principal with a specific file or directory, reference Access control in Azure Data Lake Storage Gen2.
記事の「サインインするための値を取得する」セクションの手順を行うときは、テナント ID、アプリ ID、およびパスワードの値をテキスト ファイルに貼り付けてください。When performing the steps in the Get values for signing in section of the article, paste the tenant ID, app ID, and password values into a text file. これらはすぐに必要になります。You'll need those soon.
Azure Portal にサインインします。Sign in to the Azure portal.
必要な情報を収集するGather the information that you need
このチュートリアルの前提条件を満たしていることを確認します。Make sure that you complete the prerequisites of this tutorial.
開始する前に、以下の情報が必要です。Before you begin, you should have these items of information:
✔️Azure SQL Data Warehouse のデータベース名、データベース サーバー名、ユーザー名、およびパスワード。The database name, database server name, user name, and password of your Azure SQL Data warehouse.
✔️BLOB ストレージ アカウントのアクセス キー。The access key of your blob storage account.
✔️Data Lake Storage Gen2 ストレージ アカウントの名前。The name of your Data Lake Storage Gen2 storage account.
✔️サブスクリプションのテナント ID。The tenant ID of your subscription.
✔️Azure Active Directory (Azure AD) に登録したアプリのアプリケーション ID。The application ID of the app that you registered with Azure Active Directory (Azure AD).
✔️Azure AD に登録したアプリの認証キー。The authentication key for the app that you registered with Azure AD.
Azure Databricks サービスを作成するCreate an Azure Databricks service
このセクションでは、Azure portal を使用して Azure Databricks サービスを作成します。In this section, you create an Azure Databricks service by using the Azure portal.
Azure portal メニューから [リソースの作成] を選択します。From the Azure portal menu, select Create a resource.
次に、 [分析] > [Azure Databricks] の順に選択します。Then, select Analytics > Azure Databricks.
[Azure Databricks サービス] で次の値を指定して、Databricks サービスを作成します。Under Azure Databricks Service, provide the following values to create a Databricks service:
プロパティProperty 説明Description ワークスペース名Workspace name Databricks ワークスペースの名前を指定します。Provide a name for your Databricks workspace. サブスクリプションSubscription ドロップダウンから Azure サブスクリプションを選択します。From the drop-down, select your Azure subscription. リソース グループResource group 新しいリソース グループを作成するか、既存のリソース グループを使用するかを指定します。Specify whether you want to create a new resource group or use an existing one. リソース グループは、Azure ソリューションの関連するリソースを保持するコンテナーです。A resource group is a container that holds related resources for an Azure solution. 詳しくは、Azure リソース グループの概要に関するページをご覧ください。For more information, see Azure Resource Group overview. LocationLocation [米国西部 2] を選択します。Select West US 2. 使用可能な他のリージョンについては、「リージョン別の利用可能な製品」をご覧ください。For other available regions, see Azure services available by region. Pricing TierPricing Tier [Standard] を選択します。Select Standard. アカウントの作成には数分かかります。The account creation takes a few minutes. 操作の状態を監視するには、上部の進行状況バーを確認します。To monitor the operation status, view the progress bar at the top.
[ダッシュボードにピン留めする] チェック ボックスをオンにして、 [作成] を選択します。Select Pin to dashboard and then select Create.
Azure Databricks で Spark クラスターを作成するCreate a Spark cluster in Azure Databricks
Azure portal で、作成した Databricks サービスに移動し、 [Launch Workspace](ワークスペースの起動) を選択します。In the Azure portal, go to the Databricks service that you created, and select Launch Workspace.
Azure Databricks ポータルにリダイレクトされます。You're redirected to the Azure Databricks portal. ポータルで [クラスター] を選択します。From the portal, select Cluster.
[New cluster](新しいクラスター) ページで、クラスターを作成するための値を指定します。In the New cluster page, provide the values to create a cluster.
次のフィールドに値を入力し、他のフィールドの既定値はそのまま使用します。Fill in values for the following fields, and accept the default values for the other fields:
クラスターの名前を入力します。Enter a name for the cluster.
[Terminate after __ minutes of inactivity](アクティビティが __ 分ない場合は終了する) チェック ボックスを必ずオンにします。Make sure you select the Terminate after __ minutes of inactivity check box. クラスターが使われていない場合は、クラスターを終了するまでの時間 (分単位) を指定します。If the cluster isn't being used, provide a duration (in minutes) to terminate the cluster.
[クラスターの作成] を選択します。Select Create cluster. クラスターが実行されたら、ノートブックをクラスターにアタッチして、Spark ジョブを実行できます。After the cluster is running, you can attach notebooks to the cluster and run Spark jobs.
Azure Data Lake Storage Gen2 アカウントでファイル システムを作成するCreate a file system in the Azure Data Lake Storage Gen2 account
このセクションでは、Azure Databricks ワークスペースにノートブックを作成し、ストレージ アカウントを構成するコード スニペットを実行しますIn this section, you create a notebook in Azure Databricks workspace and then run code snippets to configure the storage account
Azure portal で、作成した Azure Databricks サービスに移動し、 [Launch Workspace](ワークスペースの起動) を選択します。In the Azure portal, go to the Azure Databricks service that you created, and select Launch Workspace.
左側の [ワークスペース] を選択します。On the left, select Workspace. [ワークスペース] ドロップダウンで、 [作成] > [ノートブック] の順に選択します。From the Workspace drop-down, select Create > Notebook.
[ノートブックの作成] ダイアログ ボックスでノートブックの名前を入力します。In the Create Notebook dialog box, enter a name for the notebook. 言語として [Scala] を選んで、前に作成した Spark クラスターを選びます。Select Scala as the language, and then select the Spark cluster that you created earlier.
作成 を選択します。Select Create.
次のコード ブロックでは、Spark セッションでアクセスされる ADLS Gen 2 アカウント用の既定のサービス プリンシパル資格情報を設定します。The following code block sets default service principal credentials for any ADLS Gen 2 account accessed in the Spark session. 2 つ目のコード ブロックでは、特定の ADLS Gen 2 アカウントの資格情報を指定する設定にアカウント名を追加します。The second code block appends the account name to the setting to specify credentials for a specific ADLS Gen 2 account. いずれかのコード ブロックをコピーして、Azure Databricks ノートブックの最初のセルに貼り付けます。Copy and paste either code block into the first cell of your Azure Databricks notebook.
セッションの構成Session configuration
val appID = "<appID>" val password = "<password>" val tenantID = "<tenant-id>" spark.conf.set("fs.azure.account.auth.type", "OAuth") spark.conf.set("fs.azure.account.oauth.provider.type", "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider") spark.conf.set("fs.azure.account.oauth2.client.id", "<appID>") spark.conf.set("fs.azure.account.oauth2.client.secret", "<password>") spark.conf.set("fs.azure.account.oauth2.client.endpoint", "https://login.microsoftonline.com/<tenant-id>/oauth2/token") spark.conf.set("fs.azure.createRemoteFileSystemDuringInitialization", "true")
アカウントの構成Account configuration
val storageAccountName = "<storage-account-name>" val appID = "<app-id>" val password = "<password>" val fileSystemName = "<file-system-name>" val tenantID = "<tenant-id>" spark.conf.set("fs.azure.account.auth.type." + storageAccountName + ".dfs.core.windows.net", "OAuth") spark.conf.set("fs.azure.account.oauth.provider.type." + storageAccountName + ".dfs.core.windows.net", "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider") spark.conf.set("fs.azure.account.oauth2.client.id." + storageAccountName + ".dfs.core.windows.net", "" + appID + "") spark.conf.set("fs.azure.account.oauth2.client.secret." + storageAccountName + ".dfs.core.windows.net", "" + password + "") spark.conf.set("fs.azure.account.oauth2.client.endpoint." + storageAccountName + ".dfs.core.windows.net", "https://login.microsoftonline.com/" + tenantID + "/oauth2/token") spark.conf.set("fs.azure.createRemoteFileSystemDuringInitialization", "true") dbutils.fs.ls("abfss://" + fileSystemName + "@" + storageAccountName + ".dfs.core.windows.net/") spark.conf.set("fs.azure.createRemoteFileSystemDuringInitialization", "false")
このコード ブロックでは、
<app-id>
、<password>
、<tenant-id>
、および<storage-account-name>
のプレースホルダー値を、このチュートリアルの前提条件の実行中に収集した値で置き換えます。In this code block, replace the<app-id>
,<password>
,<tenant-id>
, and<storage-account-name>
placeholder values in this code block with the values that you collected while completing the prerequisites of this tutorial.<file-system-name>
プレースホルダーの値を、ファイル システムに付けたい名前に置き換えます。Replace the<file-system-name>
placeholder value with whatever name you want to give the file system.<app-id>
および<password>
は、サービス プリンシパルの作成の一環として Active Directory に登録したアプリのものです。The<app-id>
, and<password>
are from the app that you registered with active directory as part of creating a service principal.<tenant-id>
は、自分のサブスクリプションのものです。The<tenant-id>
is from your subscription.<storage-account-name>
は、Azure Data Lake Storage Gen2 ストレージ アカウントの名前です。The<storage-account-name>
is the name of your Azure Data Lake Storage Gen2 storage account.
Shift + Enter キーを押して、このブロック内のコードを実行します。Press the SHIFT + ENTER keys to run the code in this block.
Azure Data Lake Storage Gen2 アカウントにサンプル データを取り込むIngest sample data into the Azure Data Lake Storage Gen2 account
このセクションで始める前に、次の前提条件を満たす必要があります。Before you begin with this section, you must complete the following prerequisites:
ノートブックのセルに次のコードを入力します。Enter the following code into a notebook cell:
%sh wget -P /tmp https://raw.githubusercontent.com/Azure/usql/master/Examples/Samples/Data/json/radiowebsite/small_radio_json.json
セル内で Shift + Enter キーを押して、コードを実行します。In the cell, press SHIFT + ENTER to run the code.
次に、その下の新しいセルに次のコードを入力します。ブラケットで囲まれている値は、前に使用したのと同じ値に置き換えてください。Now in a new cell below this one, enter the following code, and replace the values that appear in brackets with the same values you used earlier:
dbutils.fs.cp("file:///tmp/small_radio_json.json", "abfss://" + fileSystemName + "@" + storageAccountName + ".dfs.core.windows.net/")
セル内で Shift + Enter キーを押して、コードを実行します。In the cell, press SHIFT + ENTER to run the code.
Azure Data Lake Storage Gen2 アカウントからデータを抽出するExtract data from the Azure Data Lake Storage Gen2 account
これで、サンプル json ファイルをデータフレームとして Azure Databricks に読み込むことができます。You can now load the sample json file as a data frame in Azure Databricks. 新しいセルに次のコードを貼り付けます。Paste the following code in a new cell. 角かっこで囲まれているプレースホルダーは、実際の値に置き換えてください。Replace the placeholders shown in brackets with your values.
val df = spark.read.json("abfss://<file-system-name>@<storage-account-name>.dfs.core.windows.net/small_radio_json.json")
Shift + Enter キーを押して、このブロック内のコードを実行します。Press the SHIFT + ENTER keys to run the code in this block.
次のコードを実行して、データ フレームの内容を表示します。Run the following code to see the contents of the data frame:
df.show()
次のスニペットのような出力が表示されます。You see an output similar to the following snippet:
+---------------------+---------+---------+------+-------------+----------+---------+-------+--------------------+------+--------+-------------+---------+--------------------+------+-------------+------+ | artist| auth|firstName|gender|itemInSession| lastName| length| level| location|method| page| registration|sessionId| song|status| ts|userId| +---------------------+---------+---------+------+-------------+----------+---------+-------+--------------------+------+--------+-------------+---------+--------------------+------+-------------+------+ | El Arrebato |Logged In| Annalyse| F| 2|Montgomery|234.57914| free | Killeen-Temple, TX| PUT|NextSong|1384448062332| 1879|Quiero Quererte Q...| 200|1409318650332| 309| | Creedence Clearwa...|Logged In| Dylann| M| 9| Thomas|340.87138| paid | Anchorage, AK| PUT|NextSong|1400723739332| 10| Born To Move| 200|1409318653332| 11| | Gorillaz |Logged In| Liam| M| 11| Watts|246.17751| paid |New York-Newark-J...| PUT|NextSong|1406279422332| 2047| DARE| 200|1409318685332| 201| ... ...
Azure Data Lake Storage Gen2 から Azure Databricks にデータが抽出されました。You have now extracted the data from Azure Data Lake Storage Gen2 into Azure Databricks.
Azure Databricks でデータを変換するTransform data in Azure Databricks
未加工のサンプル データ ファイル small_radio_json.json は、ラジオ局のリスナー情報を収集したものであり、さまざまな列を含んでいます。The raw sample data small_radio_json.json file captures the audience for a radio station and has a variety of columns. このセクションでは、このデータを変換して、データセットから特定の列だけを取得します。In this section, you transform the data to only retrieve specific columns from the dataset.
まず、作成したデータフレームから、firstName、lastName、gender、location、level の各列だけを取得します。First, retrieve only the columns firstName, lastName, gender, location, and level from the dataframe that you created.
val specificColumnsDf = df.select("firstname", "lastname", "gender", "location", "level") specificColumnsDf.show()
次のスニペットに示されているような出力が得られます。You receive output as shown in the following snippet:
+---------+----------+------+--------------------+-----+ |firstname| lastname|gender| location|level| +---------+----------+------+--------------------+-----+ | Annalyse|Montgomery| F| Killeen-Temple, TX| free| | Dylann| Thomas| M| Anchorage, AK| paid| | Liam| Watts| M|New York-Newark-J...| paid| | Tess| Townsend| F|Nashville-Davidso...| free| | Margaux| Smith| F|Atlanta-Sandy Spr...| free| | Alan| Morse| M|Chicago-Napervill...| paid| |Gabriella| Shelton| F|San Jose-Sunnyval...| free| | Elijah| Williams| M|Detroit-Warren-De...| paid| | Margaux| Smith| F|Atlanta-Sandy Spr...| free| | Tess| Townsend| F|Nashville-Davidso...| free| | Alan| Morse| M|Chicago-Napervill...| paid| | Liam| Watts| M|New York-Newark-J...| paid| | Liam| Watts| M|New York-Newark-J...| paid| | Dylann| Thomas| M| Anchorage, AK| paid| | Alan| Morse| M|Chicago-Napervill...| paid| | Elijah| Williams| M|Detroit-Warren-De...| paid| | Margaux| Smith| F|Atlanta-Sandy Spr...| free| | Alan| Morse| M|Chicago-Napervill...| paid| | Dylann| Thomas| M| Anchorage, AK| paid| | Margaux| Smith| F|Atlanta-Sandy Spr...| free| +---------+----------+------+--------------------+-----+
さらにこのデータを変換し、level 列の名前を subscription_type に変更することができます。You can further transform this data to rename the column level to subscription_type.
val renamedColumnsDF = specificColumnsDf.withColumnRenamed("level", "subscription_type") renamedColumnsDF.show()
次のスニペットに示されているような出力が得られます。You receive output as shown in the following snippet.
+---------+----------+------+--------------------+-----------------+ |firstname| lastname|gender| location|subscription_type| +---------+----------+------+--------------------+-----------------+ | Annalyse|Montgomery| F| Killeen-Temple, TX| free| | Dylann| Thomas| M| Anchorage, AK| paid| | Liam| Watts| M|New York-Newark-J...| paid| | Tess| Townsend| F|Nashville-Davidso...| free| | Margaux| Smith| F|Atlanta-Sandy Spr...| free| | Alan| Morse| M|Chicago-Napervill...| paid| |Gabriella| Shelton| F|San Jose-Sunnyval...| free| | Elijah| Williams| M|Detroit-Warren-De...| paid| | Margaux| Smith| F|Atlanta-Sandy Spr...| free| | Tess| Townsend| F|Nashville-Davidso...| free| | Alan| Morse| M|Chicago-Napervill...| paid| | Liam| Watts| M|New York-Newark-J...| paid| | Liam| Watts| M|New York-Newark-J...| paid| | Dylann| Thomas| M| Anchorage, AK| paid| | Alan| Morse| M|Chicago-Napervill...| paid| | Elijah| Williams| M|Detroit-Warren-De...| paid| | Margaux| Smith| F|Atlanta-Sandy Spr...| free| | Alan| Morse| M|Chicago-Napervill...| paid| | Dylann| Thomas| M| Anchorage, AK| paid| | Margaux| Smith| F|Atlanta-Sandy Spr...| free| +---------+----------+------+--------------------+-----------------+
Azure SQL Data Warehouse へのデータの読み込みLoad data into Azure SQL Data Warehouse
このセクションでは、変換したデータを Azure SQL Data Warehouse にアップロードします。In this section, you upload the transformed data into Azure SQL Data Warehouse. Azure Databricks 用の Azure SQL Data Warehouse コネクタを使用して、データフレームを SQL データ ウェアハウスのテーブルとして直接アップロードします。You use the Azure SQL Data Warehouse connector for Azure Databricks to directly upload a dataframe as a table in a SQL data warehouse.
前述のように、SQL Data Warehouse コネクタによって、Azure Blob Storage が一時ストレージとして使用され、Azure Databricks と Azure SQL Data Warehouse との間でデータがアップロードされます。As mentioned earlier, the SQL Data Warehouse connector uses Azure Blob storage as temporary storage to upload data between Azure Databricks and Azure SQL Data Warehouse. それにはまず、そのストレージ アカウントに接続するための構成を指定します。So, you start by providing the configuration to connect to the storage account. このアカウントは、この記事の前提条件としてあらかじめ作成しておく必要があります。You must already have already created the account as part of the prerequisites for this article.
Azure Databricks から Azure Storage アカウントにアクセスするための構成を指定します。Provide the configuration to access the Azure Storage account from Azure Databricks.
val blobStorage = "<blob-storage-account-name>.blob.core.windows.net" val blobContainer = "<blob-container-name>" val blobAccessKey = "<access-key>"
Azure Databricks と Azure SQL Data Warehouse の間でデータを移動するときに使用する一時フォルダーを指定します。Specify a temporary folder to use while moving data between Azure Databricks and Azure SQL Data Warehouse.
val tempDir = "wasbs://" + blobContainer + "@" + blobStorage +"/tempDirs"
次のスニペットを実行して、Azure Blob Storage のアクセス キーを構成に格納します。Run the following snippet to store Azure Blob storage access keys in the configuration. このアクションにより、アクセス キーをプレーンテキストのままノートブックに保持せずに済みます。This action ensures that you don't have to keep the access key in the notebook in plain text.
val acntInfo = "fs.azure.account.key."+ blobStorage sc.hadoopConfiguration.set(acntInfo, blobAccessKey)
Azure SQL Data Warehouse インスタンスに接続するための値を指定します。Provide the values to connect to the Azure SQL Data Warehouse instance. SQL データ ウェアハウスは、前提条件としてあらかじめ作成しておく必要があります。You must have created a SQL data warehouse as a prerequisite. dwServer の完全修飾サーバー名を使用します。Use the fully qualified server name for dwServer. たとえば、「
<servername>.database.windows.net
」のように入力します。For example,<servername>.database.windows.net
.//SQL Data Warehouse related settings val dwDatabase = "<database-name>" val dwServer = "<database-server-name>" val dwUser = "<user-name>" val dwPass = "<password>" val dwJdbcPort = "1433" val dwJdbcExtraOptions = "encrypt=true;trustServerCertificate=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;" val sqlDwUrl = "jdbc:sqlserver://" + dwServer + ":" + dwJdbcPort + ";database=" + dwDatabase + ";user=" + dwUser+";password=" + dwPass + ";$dwJdbcExtraOptions" val sqlDwUrlSmall = "jdbc:sqlserver://" + dwServer + ":" + dwJdbcPort + ";database=" + dwDatabase + ";user=" + dwUser+";password=" + dwPass
次のスニペットを実行して、変換済みのデータフレーム (renamedColumnsDf) をテーブルとして SQL データ ウェアハウスに読み込みます。 このスニペットは、SQL データベースに SampleTable というテーブルを作成します。This snippet creates a table called SampleTable in the SQL database.
spark.conf.set( "spark.sql.parquet.writeLegacyFormat", "true") renamedColumnsDF.write.format("com.databricks.spark.sqldw").option("url", sqlDwUrlSmall).option("dbtable", "SampleTable") .option( "forward_spark_azure_storage_credentials","True").option("tempdir", tempDir).mode("overwrite").save()
注意
このサンプルでは
forward_spark_azure_storage_credentials
フラグを使用します。これにより、SQL Data Warehouse は、アクセス キーを使用して BLOB ストレージからのデータにアクセスします。This sample uses theforward_spark_azure_storage_credentials
flag, which causes SQL Data Warehouse to access data from blob storage using an Access Key. これは、サポートされている唯一の認証方法です。This is the only supported method of authentication.Azure Blob Storage が仮想ネットワークを選択するように制限されている場合、SQL Data Warehouse にはアクセス キーではなく、マネージド サービス ID が必要です。If your Azure Blob Storage is restricted to select virtual networks, SQL Data Warehouse requires Managed Service Identity instead of Access Keys. これにより、"This request is not authorized to perform this operation (この要求には、この操作を実行する権限がありません)" というエラーが発生します。This will cause the error "This request is not authorized to perform this operation."
SQL データベースに接続し、SampleTable という名前のデータベースが表示されることを確認します。Connect to the SQL database and verify that you see a database named SampleTable.
選択クエリを実行して、テーブルの内容を確認します。Run a select query to verify the contents of the table. このテーブルには、renamedColumnsDf データフレームと同じデータが存在しているはずです。The table should have the same data as the renamedColumnsDF dataframe.
リソースのクリーンアップClean up resources
チュートリアルが完了したら、クラスターを終了できます。After you finish the tutorial, you can terminate the cluster. Azure Databricks ワークスペースで、左側の [クラスター] を選択します。From the Azure Databricks workspace, select Clusters on the left. クラスターが終了するように、 [アクション] の下にある省略記号 (...) をポイントし、終了アイコンを選択します。For the cluster to terminate, under Actions, point to the ellipsis (...) and select the Terminate icon.
クラスター作成時に [Terminate after __ minutes of inactivity](アクティビティが __ 分ない場合は終了する) チェック ボックスをオンにした場合は、手動で終了しなくても、クラスターは自動的に停止します。If you don't manually terminate the cluster, it automatically stops, provided you selected the Terminate after __ minutes of inactivity check box when you created the cluster. このような場合、クラスターは、非アクティブな状態が一定の時間続くと自動的に停止します。In such a case, the cluster automatically stops if it's been inactive for the specified time.
次の手順Next steps
このチュートリアルでは、以下の内容を学習しました。In this tutorial, you learned how to:
- Azure Databricks サービスを作成するCreate an Azure Databricks service
- Azure Databricks で Spark クラスターを作成するCreate a Spark cluster in Azure Databricks
- Azure Databricks でノートブックを作成するCreate a notebook in Azure Databricks
- Data Lake Storage Gen2 アカウントからデータを抽出するExtract data from a Data Lake Storage Gen2 account
- Azure Databricks でデータを変換するTransform data in Azure Databricks
- Azure SQL Data Warehouse へのデータの読み込みLoad data into Azure SQL Data Warehouse
次のチュートリアルに進み、Azure Event Hubs を使ってリアルタイム データを Azure Databricks にストリーミングする方法について見てみましょう。Advance to the next tutorial to learn about streaming real-time data into Azure Databricks using Azure Event Hubs.
フィードバック
フィードバックを読み込んでいます...