SDK for Java

Updated at:

The Hologres software development kit (SDK) for Java lets you manage Hologres instances programmatically — query instance details, configure resources, and automate instance operations using the Hologres management API.

Prerequisites

Before you begin, make sure you have:

Install the SDK

Add the following Maven dependency to your project. To get the latest version, check the Alibaba Cloud SDK for Java release page.

If you use a build tool other than Maven, see other installation methods and Get started with Alibaba Cloud Darabonba SDK for Java.
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>hologram20220601</artifactId>
  <version>1.0.5</version>
</dependency>

Make your first API call

This section walks through importing the SDK, configuring credentials, creating a client, and calling the GetInstance operation to query instance details.

How it works

  1. Import the SDK packages.

  2. Configure credentials and an endpoint, then create a client.

  3. Create a request object and call the API operation.

  4. Handle the response or any errors.

Step 1: Import the SDK

import com.aliyun.tea.*;
import com.aliyun.hologram20220601.*;

Step 2: Configure credentials

Store your AccessKey ID and AccessKey secret as environment variables rather than hardcoding them in your source code. This prevents accidental exposure if the code is shared or leaked. For stronger security in production, use Security Token Service (STS) to issue short-lived credentials instead.

Config config = new Config()
    // Read the AccessKey ID from an environment variable.
    .setAccessKeyId(System.getenv("ALIBABA_CLOUD_USER"))
    // Read the AccessKey secret from an environment variable.
    .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_PASSWORD"));

Step 3: Set the endpoint

The endpoint follows this pattern:

hologram.<region-id>.aliyuncs.com

For example, for the China (Hangzhou) region:

config.endpoint = "hologram.cn-hangzhou.aliyuncs.com";

For a full list of region IDs and endpoints, see Hologres regions.

Step 4: Create a client

Client client = new Client(config);

Step 5: Call an API operation

Create a request object and call the operation. The following example calls GetInstance to query the details of an instance.

GetInstanceRequest request = new GetInstanceRequest();
GetInstanceResponse response = client.getInstance("your_instance_id", request);
System.out.println(JSON.toJSONString(response.getBody()));

Error handling

Catch TeaException for SDK-level errors (such as authentication failures or invalid parameters) and the generic Exception for unexpected errors. To look up error codes, see Hologres API operations.

try {
    GetInstanceResponse response = client.getInstance("your_instance_id", request);
    System.out.println(JSON.toJSONString(response.getBody()));
} catch (TeaException error) {
    com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
    TeaException error = new TeaException(_error.getMessage(), _error);
    com.aliyun.teautil.Common.assertAsString(error.message);
}

Complete example

The following runnable example queries instance details. It works with Hologres V1.0.3 or later.

package com.aliyun.sample;

import com.aliyun.hologram20220601.models.GetInstanceResponse;
import com.aliyun.hologram20220601.models.GetInstanceResponseBody;
import com.aliyun.tea.*;
import com.aliyun.hologram20220601.*;

public class Sample {

    /**
     * Initialize the client with your AccessKey ID and AccessKey secret.
     *
     * @param accessKeyId     AccessKey ID
     * @param accessKeySecret AccessKey secret
     * @return Client
     * @throws Exception
     */
    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // Required. Specify your AccessKey ID.
                .setAccessKeyId(accessKeyId)
                // Required. Specify your AccessKey secret.
                .setAccessKeySecret(accessKeySecret);
        // Endpoint format: hologram.<region-id>.aliyuncs.com
        config.endpoint = "hologram.cn-hangzhou.aliyuncs.com";
        return new Client(config);
    }

    public static void main(String[] args_) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);
        // Read credentials from environment variables.
        // To avoid leaking credentials if source code is shared, do not hardcode
        // the AccessKey pair. For production workloads, use STS for higher security.
        Client client = Sample.createClient(
                System.getenv("ALIBABA_CLOUD_USER"),
                System.getenv("ALIBABA_CLOUD_PASSWORD"));
        try {
            client.getInstance("instanceId");
        } catch (TeaException error) {
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

The following response is returned:

{
  "RequestId": "1E8EFA1E-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
  "Instance": {
    "SuspendReason": "",
    "ZoneId": "cn-hangzhou-h",
    "InstanceId": "hgpostcn-cn-xxxxxxxxxxxx",
    "InstanceChargeType": "PostPaid",
    "Memory": 64,
    "InstanceOwner": "123xxxxxxxxxxxx",
    "ColdStorage": 0,
    "Cpu": 16,
    "EnableHiveAccess": false,
    "InstanceName": "xxxx",
    "ComputeNodeCount": 1,
    "Endpoints": [
      {
        "Type": "Intranet",
        "Endpoint": "hgpostcn-cn-xxxxxxxxxxxx-cn-hangzhou-internal.hologres.aliyuncs.com:80",
        "Enabled": true
      },
      {
        "Type": "VPCSingleTunnel",
        "Endpoint": "hgpostcn-cn-xxxxxxxxxxxx-cn-hangzhou-vpc-st.hologres.aliyuncs.com:80",
        "VpcId": "vpc-xxxxxxxxxxxxxxxx",
        "VSwitchId": "vsw-xxxxxxxxxxxxxxxx",
        "Enabled": true,
        "VpcInstanceId": "hgpostcn-cn-xxxxxxxxxxxx-frontend-st"
      }
    ],
    "AutoRenewal": false,
    "Version": "r1.3.43",
    "InstanceStatus": "Running",
    "CommodityCode": "hologram_postpay_public_cn",
    "CreationTime": "2023-04-12T03:41:59.036Z",
    "InstanceType": "Standard",
    "Disk": 0,
    "Tags": [
      {
        "Value": "rg-xxxxxxxxxxxx",
        "Key": "acs:rm:rgId"
      }
    ]
  },
  "HttpStatusCode": 200,
  "Success": true
}

What's next