IBM Cloud Docs
Using the Node.js SDK

Using the Node.js SDK

The IBM Analytics Engine Node.js SDK allows you to interact programmatically with the IBM Analytics Engine service API for serverless instances.

Installing the SDK

You can install the IBM Analytics Engine Node.js SDK using the Node package manager (npm).

Type the following command into a command line:

npm install iaesdk

You can find the source code in GitHub. See ibm-iae-node-sdk. The iaesdk library provides complete access to the IBM Analytics Engine API.

To run the Node.js SDK, you need Node 4.x+. You need to provide the service endpoints and the API key when you create a IBM Analytics Engine service resource or a low-level client.

The service instance ID is also referred to as a instance GUID. You can retrieve the service instance ID when you create service credentials or through the CLI. See Retrieving service endpoints.

Code samples using iaesdk

Getting started with the Node.js SDK after you have installed it, involves sourcing credentials to the IBM Analytics Engine service, invoking the service and then issuing different cluster commands as shown in the following sample code snippets.

In addition to the sample code snippets in this topic, you can work with Node.js code samples from the IBM Analytics Engine V3 API reference.

The following code samples show how to:

  • Authenticate to the IBM Analytics Engine service and create a service client:

    const IbmAnalyticsEngineApiV3 = require('iaesdk/ibm-analytics-engine-api/v3');
    const { IamAuthenticator } = require('iaesdk/auth');
    
    const IAM_API_KEY = "{apikey}" // eg "W00YiRnLW4a3fTjMB-odB-2ySfTrFBIQQWanc--P3byk"
    const IAE_ENDPOINT_URL = "{url}" // Current list available at https://cloud.ibm.com/apidocs/ibm-analytics-engine#service-endpoints
    const API_AUTH_URL = "{api auth url}" // "https://iam.cloud.ibm.com/identity/token"
    const DEFAULT_SERVICE_NAME = 'ibm_analytics_engine_api'
    
    let options = {};
    
    // Create an IAM authenticator.
    options.authenticator = new IamAuthenticator({
      apikey: IAM_API_KEY,
      url: API_AUTH_URL
    });
    
    options.serviceUrl = IAE_ENDPOINT_URL;
    options.serviceName = DEFAULT_SERVICE_NAME;
    
    // Construct the service client.
    const ibmAnalyticsEngineApiService = new IbmAnalyticsEngineApiV3.newInstance(options);
    

    Key values:

    • serviceUrl: public endpoint to the IBM Analytics Engine instance. See Service endpoints.
    • apikey: API key generated when creating the service credentials. Write access is required for creation and deletion tasks.
  • Retrieve the details of a single instance:

    getInstance(params)
    

    Example request:

    const params = {
      instanceId: 'e64c907a-e82f-46fd-addc-ccfafbd28b09',
    };
    ibmAnalyticsEngineApiService.getInstance(params)
    .then((res) => {
      console.log(JSON.stringify(res.result, null, 2));
    }).catch((err) => {
      console.warn(err);
    });
    
  • Deploy a Spark application on a given serverless Spark instance:

    createApplication(params)
    

    Example request:

    // ApplicationRequestApplicationDetails
    const applicationRequestApplicationDetailsModel = {
      application: '/opt/ibm/spark/examples/src/main/python/wordcount.py',
      arguments: ['/opt/ibm/spark/examples/src/main/resources/people.txt'],
    };
    
    ibmAnalyticsEngineApiService.createApplication({
      instanceId: 'e64c907a-e82f-46fd-addc-ccfafbd28b09',
      applicationDetails: applicationRequestApplicationDetailsModel,
    }).then((res) => {
      console.log(JSON.stringify(res.result, null, 2));
    }).catch((err) => {
      console.warn(err);
    });
    
  • Retrieve all Spark applications run on a given instance:

    listApplications(params)
    

    Example request:

    ibmAnalyticsEngineApiService.listApplications({
      instanceId: 'e64c907a-e82f-46fd-addc-ccfafbd28b09',
    }).then((res) => {
      console.log(JSON.stringify(res.result, null, 2));
    }).catch((err) => {
      console.warn(err);
    });
    
  • Retrieve the details of a given Spark application:

    getApplication(params)
    

    Example request:

    ibmAnalyticsEngineApiService.getApplication({
      instanceId: 'e64c907a-e82f-46fd-addc-ccfafbd28b09',
      applicationId: 'db933645-0b68-4dcb-80d8-7b71a6c8e542',
    }).then((res) => {
      console.log(JSON.stringify(res.result, null, 2));
    }).catch((err) => {
      console.warn(err);
    });
    
  • Return the state of the application indentified by the app_ididentifier:

    getApplicationState(params)
    

    Example request:

    ibmAnalyticsEngineApiService.getApplicationState({
      instanceId: 'e64c907a-e82f-46fd-addc-ccfafbd28b09',
      applicationId: 'db933645-0b68-4dcb-80d8-7b71a6c8e542',
    }).then((res) => {
      console.log(JSON.stringify(res.result, null, 2));
    }).catch((err) => {
      console.warn(err);
    });
    
  • Stop a running application identified by the app_id identifier. This is an idempotent operation. Performs no action if the requested application is already stopped or completed.

    deleteApplication(params)
    

    Example request:

    ibmAnalyticsEngineApiService.deleteApplication({
      instanceId: 'e64c907a-e82f-46fd-addc-ccfafbd28b09',
      applicationId: 'db933645-0b68-4dcb-80d8-7b71a6c8e542',
    }).then((res) => {
      console.log(JSON.stringify(res.result, null, 2));
    }).catch((err) => {
      console.warn(err);
    });