Updated on 2024-10-23 GMT+08:00

Development Plan

Scenarios

Assume that table1 of HBase stores a user's consumption amount on the current day and table2 stores the user's history consumption amount.

In table1, "key=1" and "cf:cid=100" indicate that the consumption amount of user 1 in the current day is CNY 100.

In table2, "key=1" and "cf:cid=1000" indicate that the history comsuption amount of user 1 is CNY 1,000.

The Spark application shall achieve the following function:

To determine a user's total consumption amount, add their current day's consumption of CNY 100 to their history consumption amount of CNY 1,000, based on their username.

In table2, user 1 (key=1) has a total consumption amount of CNY 1,100 (cf:cid=1100).

Preparing Data

Use the Spark-Beeline tool to create table1 and table2 (Spark table and HBase table, respectively), and insert data by HBase.

  1. Ensure that JDBCServer is started. On the Spark2x client, perform the following operations using the Spark-Beeline command tool:
  2. Use the Spark-Beeline tool to create Spark table1:

    create table table1

    (

    key string,

    cid string

    )

    using org.apache.spark.sql.hbase.HBaseSource

    options(

    hbaseTableName "table1",

    keyCols "key",

    colsMapping "cid=cf.cid");

  3. Run the following command on HBase to insert data to table1:

    put 'table1', '1', 'cf:cid', '100'

  4. Use the Spark-Beeline tool to create Spark table2:

    create table table2

    (

    key string,

    cid string

    )

    using org.apache.spark.sql.hbase.HBaseSource

    options(

    hbaseTableName "table2",

    keyCols "key",

    colsMapping "cid=cf.cid");

  5. Run the following command on HBase to insert data to table 2:

    put 'table2', '1', 'cf:cid', '1000'

Development Guidelines

  1. Query the data in table1.
  2. Query the data in table2 using the key value of table1.
  3. Add the queried data.
  4. Write the result of the previous step to table2.

Preparations

For clusters with the security mode enabled, the Spark Core sample code needs to read two files (user.keytab and krb5.conf). The user.keytab and krb5.conf files are authentication files in the security mode. Download the authentication credentials of the user principal on the FusionInsight Manager page. The user in the sample code is sparkuser, change the value to the prepared development user name.

Packaging the Project

  1. Upload the user.keytab and krb5.conf files to the server where the client is located.
  1. Use the Maven tool provided by IDEA to package the project and generate the JAR file. For details, see Writing and Running the Spark Program in the Linux Environment.
    • Before compilation and packaging, change the paths of the user.keytab and krb5.conf files in the sample code to the actual paths on the client server. Example: /opt/female/user.keytab and /opt/female/krb5.conf.
    • Before running the sample project, set the spark.yarn.security.credentials.hbase.enabled configuration item to true in the spark-defaults.conf configuration file of Spark client. (The default value is false. Changing the value to true does not affect existing services.) If you want to uninstall the HBase service, change the value back to false first.
  2. Upload the JAR file to any directory (for example, /opt/female/) on the server where the Spark client is located.

Running Tasks

Go to the Spark client directory and run the following commands to invoke the bin/spark-submit script to run the code (The class name and file name must be the same as those in the actual code. The following is only an example.):

  • Run Java or Scala sample code.

    bin/spark-submit --conf spark.yarn.user.classpath.first=true --class com.huawei.bigdata.spark.examples.SparkHbasetoHbase --master yarn --deploy-mode client /opt/female/SparkHbasetoHbase-1.0.jar

  • Run the Python sample project.
    • PySpark does not provide HBase APIs. In this example, Python is used to invoke Java code to implement required operations. Use Maven to pack the provided Java code into a JAR file and place it in the same directory. When running the Python program, use --jars to load the JAR file to classpath.
    • The Python sample code does not provide authentication information. Configure --keytab and --principal to specify authentication information.

    bin/spark-submit --master yarn --deploy-mode client --keytab /opt/FIclient/user.keytab --principal sparkuser --conf spark.yarn.user.classpath.first=true --jars /opt/female/SparkHbasetoHbasePythonExample/SparkHbasetoHbase-1.0.jar,/opt/female/protobuf-java-2.5.0.jar /opt/female/SparkHbasetoHbasePythonExample/SparkHbasetoHbasePythonExample.py