Updated on 2022-06-01 GMT+08:00

Compiling and Running an Application When a Client Is Installed

HBase applications can run in a Linux environment where an HBase client is installed. After application code development is complete, you can upload a JAR file to the Linux environment to run applications.

Prerequisites

  • You have installed an HBase client.
  • You have installed a JDK in the Linux environment. The version of the JDK must be consistent with that of the JDK used by Eclipse to export the JAR file.
  • If the host where the client is installed is not a node in the cluster, the mapping between the host name and the IP address must be set in the hosts file on the node where the client locates. The host names and IP addresses must be mapped one by one.

Procedure

  1. Modify the sample code.

    1. Currently, there are three types of HBase APIs in the sample code: common API, HFS API (no longer supported in MRS 1.9.x), and REST API. When commissioning different APIs to operate HBase, you can comment out the invoking of other APIs. In this example, common APIs are used to operate HBase and the main method contains only the following code snippet.
      public static void main(String[] args) {
        try {
          init();
          login();
        } catch (IOException e) {
            LOG.error("Failed to login because ", e);
            return;
        }
        // getDefaultConfiguration();
        conf = HBaseConfiguration.create();
        // test hbase API
        HBaseExample oneSample;
        try {
          oneSample = new HBaseExample(conf);
          oneSample.test();
        } catch (Exception e) {
          LOG.error("Failed to test HBase because ", e);
        }
        LOG.info("-----------finish HBase-------------------");
      }
    2. When you call HFS APIs (no longer supported in MRS 1.9.x) and RESTful APIs, you need to copy the inputfile.txt and hbaseclient.properties files in the src\main\resources directory in the sample project to the HBase/hbase/conf directory of the client (for example, the client directory is /opt/client), and modify the hbaseclient.properties file. userKeytabName and krb5ConfName indicate the file addresses obtained in 2. If you need to modify rest.server.info when using REST APIs, make it correspond to ip:port (default port: 21309) of the rest server.
      rest.server.info=10.10.10.10:21309
      user.name=hbaseuser
      userKeytabName=user.keytab
      krb5ConfName=krb5.conf

      The HFSSample sample project is removed from MRS 1.9.x. Pay attention to the current cluster version.

  2. Run the mvn package command to generate a JAR file, for example, hbase-examples-mrs-2.0.jar. Obtain the JAR file from the target directory in the project directory, and upload it to the /opt/client/HBase/hbase/lib directory.
  3. Run the JAR file.

    1. Before running the JAR file on the Linux client, run the following command to switch to the client directory as the user that is used for installation:

      cd $BIGDATA_CLIENT_HOME/HBase/hbase

      $BIGDATA_CLIENT_HOME indicates the client installation directory.

    2. Run the following command:

      source $BIGDATA_CLIENT_HOME/bigdata_env

      After the multi-instance function is enabled, you also need to run the following command to switch to the client of the specified service instance before developing applications for the HBase service instance, for example, HBase2: source /opt/client/HBase2/component_env.

    3. Copy the JAR package generated in 2 and the krb5.conf and user.keytab files obtained in Preparing a Development User to the HBase/hbase/conf directory in the client running environment, for example, /opt/client/HBase/hbase/conf. Create the hbaseclient.properties file in the /opt/client/HBase/hbase/conf directory if the file does not exist. In the file, user.name corresponds to the new user hbaseuser, and the values of userKeytabName and krb5ConfName correspond to the authentication-related file names obtained in Preparing a Development User (skip this step if Kerberos authentication is not enabled for the cluster).
      user.name=hbaseuser
      userKeytabName=user.keytab
      krb5ConfName=krb5.conf
    4. Run the following command to execute the JAR package:

      hbase com.huawei.bigdata.hbase.examples.TestMain /opt/client/HBase/hbase/conf

      com.huawei.bigdata.hbase.examples.TestMain is used as an example. Use the actual code instead.

      /opt/client/HBase/hbase/conf corresponds to the path of files such as user.keytab and krb5.conf mentioned above.

      For MRS 1.9.2 or later, run the hbase com.huawei.bigdata.hbase.examples.TestMain /opt/client/HBase/hbase/conf command.