访问准备

使用HBase API的用户,支持通过Spark访问Lindorm,用户只需要使用阿里云发布的HBase客户端,具体使用方式请参见HBase Java SDK 安装&升级

添加Lindorm访问配置

  • 方式一:通过配置文件添加访问配置
    在配置文件hbase-site.xml中增加下列配置项:
    <configuration>
          <!--
        集群的连接地址,在控制台页面的数据库连接界面获得(注意公网地址和VPC内网地址)
        -->
        <property>
            <name>hbase.zookeeper.quorum</name>
            <value>ld-xxxx-proxy-hbaseue.hbaseue.xxx.rds.aliyuncs.com:30020</value>
        </property>
        <!--    
       设置用户名密码,默认root:root,可根据实际情况调整
        -->
        <property>
            <name>hbase.client.username</name>
            <value>root</value>
        </property>
        <property>
            <name>hbase.client.password</name>
            <value>root</value>
        </property>
        <!--
        如果您直接依赖了阿里云hbase客户端,则无需配置connection.impl参数,如果您依赖了alihbase-connector,则需要配置此参数
        -->
        <!--property>
            <name>hbase.client.connection.impl</name>
           <value>org.apache.hadoop.hbase.client.AliHBaseUEClusterConnection</value>
        </property-->
    </configuration>
  • 方式二:通过代码在Configuration中添加参数
    // 新建一个Configuration
    Configuration conf = HBaseConfiguration.create();
    // 集群的连接地址,在控制台页面的数据库连接界面获得(注意公网地址和VPC内网地址)
    conf.set("hbase.zookeeper.quorum", "ld-xxxx-proxy-hbaseue.hbaseue.xxx.rds.aliyuncs.com:30020");
    // 设置用户名密码,默认root:root,可根据实际情况调整
    conf.set("hbase.client.username", "root")
    conf.set("hbase.client.password", "root")
    // 如果您直接依赖了阿里云hbase客户端,则无需配置connection.impl参数,如果您依赖了alihbase-connector,则需要配置此参数
    //conf.set("hbase.client.connection.impl", AliHBaseUEClusterConnection.class.getName());

Spark访问示例

添加hbase ue访问配置。
test(" test the spark sql count result") {
//1. 添加hbase ue访问配置
var conf = HBaseConfiguration.create
conf.set("hbase.zookeeper.quorum", "ld-xxxx-proxy-hbaseue.hbaseue.xxx.rds.aliyuncs.com:30020")
conf.set("hbase.client.username", "test_user")  
conf.set("hbase.client.password", "password")

//2. 创建表
val hbaseTableName = "testTable"
val cf = "f"  
           val column1 = cf + ":a"  
val column2 = cf + ":b"
var rowsCount: Int = -1
var namespace = "spark_test"  
val admin = ConnectionFactory.createConnection(conf).getAdmin() 
val tableName = TableName.valueOf(namespace, hbaseTableName) 
val htd = new HTableDescriptor(tableName)  
htd.addFamily(new HColumnDescriptor(cf)) 
admin.createTable(htd)  

//3. 插入测试数据  
val rng = new Random()  
23. val k: Array[Byte] = new Array[Byte](3)  
24. val famAndQf = KeyValue.parseColumn(Bytes.toBytes(column)) 
25. val puts = new util.ArrayList[Put]() 
26. var i = 0  
27. for (b1 <- ('a' to 'z')) { 
28.     for (b2 <- ('a' to 'z')) {  
29.       for (b3 <- ('a' to 'z')) {       
30.          if(i < 10) {           
31.             k(0) = b1.toByte          
32.             k(1) = b2.toByte          
33.             k(2) = b3.toByte       
34.             val put = new Put(k)        
35.             put.addColumn(famAndQf(0), famAndQf(1), ("value_" + b1 + b2 + b3).getBytes())        
36.             puts.add(put)         
37.             i = i + 1       
38.           }       
39.        }     
40.       }  
41. }  
42. val conn = ConnectionFactory.createConnection(conf) 
43. val table = conn.getTable(tableName) 
44. table.put(puts) 
45.
46.
47. //4. 创建spark表 
48. val sparkTableName = "spark_hbase" 
49. val createCmd = s"""CREATE TABLE ${sparkTableName} USING org.apache.hadoop.hbase.spark   
50.                        |    OPTIONS ('catalog'=               
51.                        |    '{"table":{"namespace":"$${hbaseTableName}",                   "name":"${hbaseTableName}"},"rowkey":"rowkey",            
52.                        |    "columns":{               
53.                        |    "col0":{"cf":"rowkey", "col":"rowkey", "type":"string"},               
54.                        |    "col1":{"cf":"cf1", "col":"a", "type":"string"},                  
55.                        |    "col2":{"cf":"cf1", "col":"b", "type":"String"}}}'            
56.                        |    )""".stripMargin  
57. 
58. println(" createCmd: \n" + createCmd + " rows : " + rowsCount) 
59. sparkSession.sql(createCmd) 
60.
61. //5. 执行count sql 
62. val result = sparkSession.sql("select count(*) from " + sparkTableName) 
63. val sparkCounts = result.collect().apply(0).getLong(0) 
64. println(" sparkCounts : " + sparkCounts)