使用 Node.js 的 Azure SDK 管理 Azure 資料湖分析Manage Azure Data Lake Analytics using Azure SDK for Node.js

本文說明如何使用以 Azure SDK for Node.js 所撰寫的應用程式,來管理 Azure Data Lake Analytics 帳戶、資料來源、使用者和作業。This article describes how to manage Azure Data Lake Analytics accounts, data sources, users, and jobs using an app written using the Azure SDK for Node.js.

支援的版本如下:The following versions are supported:

  • Node.js 版本:0.10.0 或更新版本Node.js version: 0.10.0 or higher
  • 帳戶的 REST API 版本:2015-10-01-previewREST API version for Account: 2015-10-01-preview
  • 目錄的 REST API 版本:2015-10-01-previewREST API version for Catalog: 2015-10-01-preview
  • 作業的 REST API 版本:2016-03-20-previewREST API version for Job: 2016-03-20-preview

功能Features

  • 帳戶管理:建立、取得、列出、更新及刪除。Account management: create, get, list, update, and delete.
  • 作業管理︰提交、取得、列出及取消。Job management: submit, get, list, and cancel.
  • 目錄管理:取得及列出。Catalog management: get and list.

如何安裝How to Install

npm install azure-arm-datalake-analytics

使用 Azure Active Directory 進行驗證Authenticate using Azure Active Directory

var msrestAzure = require('ms-rest-azure');
//user authentication
var credentials = new msRestAzure.UserTokenCredentials('your-client-id', 'your-domain', 'your-username', 'your-password', 'your-redirect-uri');
//service principal authentication
var credentials = new msRestAzure.ApplicationTokenCredentials('your-client-id', 'your-domain', 'your-secret');

建立 Data Lake Analytics 用戶端Create the Data Lake Analytics client

var adlaManagement = require("azure-arm-datalake-analytics");
var accountClient = new adlaManagement.DataLakeAnalyticsAccountClient(credentials, 'your-subscription-id');
var jobClient = new adlaManagement.DataLakeAnalyticsJobClient(credentials, 'azuredatalakeanalytics.net');
var catalogClient = new adlaManagement.DataLakeAnalyticsCatalogClient(credentials, 'azuredatalakeanalytics.net');

建立 Data Lake Analytics 帳戶Create a Data Lake Analytics account

var util = require('util');
var resourceGroupName = 'testrg';
var accountName = 'testadlaacct';
var location = 'eastus2';

// A Data Lake Store account must already have been created to create
// a Data Lake Analytics account. See the Data Lake Store readme for
// information on doing so. For now, we assume one exists already.
var datalakeStoreAccountName = 'existingadlsaccount';

// account object to create
var accountToCreate = {
  tags: {
    testtag1: 'testvalue1',
    testtag2: 'testvalue2'
  },
  name: accountName,
  location: location,
  properties: {
    defaultDataLakeStoreAccount: datalakeStoreAccountName,
    dataLakeStoreAccounts: [
      {
        name: datalakeStoreAccountName
      }
    ]
  }
};

client.account.create(resourceGroupName, accountName, accountToCreate, function (err, result, request, response) {
  if (err) {
    console.log(err);
    /*err has reference to the actual request and response, so you can see what was sent and received on the wire.
      The structure of err looks like this:
      err: {
        code: 'Error Code',
        message: 'Error Message',
        body: 'The response body if any',
        request: reference to a stripped version of http request
        response: reference to a stripped version of the response
      }
    */
  } else {
    console.log('result is: ' + util.inspect(result, {depth: null}));
  }
});

取得作業清單Get a list of jobs

var util = require('util');
var accountName = 'testadlaacct';
jobClient.job.list(accountName, function (err, result, request, response) {
  if (err) {
    console.log(err);
  } else {
    console.log('result is: ' + util.inspect(result, {depth: null}));
  }
});

取得 Data Lake Analytics 目錄中的資料庫清單Get a list of databases in the Data Lake Analytics Catalog

var util = require('util');
var accountName = 'testadlaacct';
catalogClient.catalog.listDatabases(accountName, function (err, result, request, response) {
  if (err) {
    console.log(err);
  } else {
    console.log('result is: ' + util.inspect(result, {depth: null}));
  }
});

另請參閱See also