全部产品
云市场

Spark访问

更新时间:2019-10-15 10:20:13

访问准备

HBase增强版支持从Spark访问,用户只需要加入alihbase-connector的依赖即可,最新版本详见JAVA SDK安装

  1. <dependency>
  2. <groupId>com.alibaba.hbase</groupId>
  3. <artifactId>alihbase-connector</artifactId>
  4. <version>{version}</version>
  5. </dependency>

获取访问地址

参见连接集群,使用地址中Java API访问地址,默认端口为30020,如果是公网访问,请使用公网域名

获取用户名和密码

参见连接集群,默认的用户名为root,密码为root。或者在集群管理页面中关闭ACL功能后,无需再提供用户名密码

添加HBase增强版访问配置

方式一:配置文件

hbase-site.xml 中增加下列配置项:

  1. <configuration>
  2. <property>
  3. <name>hbase.client.connection.impl</name>
  4. <value>com.alibaba.hbase.client.AliHBaseUEConnection</value>
  5. </property>
  6. <!--
  7. 集群的连接地址(注意公网地址和VPC内网地址)
  8. -->
  9. <property>
  10. <name>hbase.client.endpoint</name>
  11. <value>{HOST:PORT}</value>
  12. </property>
  13. <!--
  14. 设置用户名密码,默认root:root,可根据实际情况调整
  15. -->
  16. <property>
  17. <name>hbase.client.username</name>
  18. <value>root</value>
  19. </property>
  20. <property>
  21. <name>hbase.client.password</name>
  22. <value>root</value>
  23. </property>
  24. </configuration>

方式二:代码

通过代码在Configuration中添加参数:

  1. // 新建一个Configuration
  2. Configuration conf = HBaseConfiguration.create();
  3. // 将HBase底层Connection实现替换成HBase增强版专用的AliHBaseUEConnection
  4. conf.set("hbase.client.connection.impl", AliHBaseUEConnection.class.getName());
  5. // 集群的连接地址(注意公网地址和VPC内网地址)
  6. conf.set("hbase.client.endpoint", "HOST:PORT");
  7. // 设置用户名密码,默认root:root,可根据实际情况调整
  8. conf.set("hbase.client.username", "root")
  9. conf.set("hbase.client.password", "root")

注: 访问HBase增强版不需要配置zookeeper的地址参数(hbase.zookeeper.quorum)

Spark访问示例

  1. test(" test the spark sql count result") {
  2. //1. 添加hbase ue访问配置
  3. var conf = HBaseConfiguration.create
  4. conf.set("hbase.client.connection.impl", "com.alibaba.hbase.client.AliHBaseUEConnection")
  5. conf.set("hbase.client.endpoint", "127.0.0.1")
  6. conf.set("hbase.client.username", "test_user")
  7. conf.set("hbase.client.password", "password")
  8. //2. 创建表
  9. val hbaseTableName = "testTable"
  10. val cf = "f"
  11. val column1 = cf + ":a"
  12. val column2 = cf + ":b"
  13. var rowsCount: Int = -1
  14. var namespace = "spark_test"
  15. val admin = ConnectionFactory.createConnection(conf).getAdmin()
  16. val tableName = TableName.valueOf(namespace, hbaseTableName)
  17. val htd = new HTableDescriptor(tableName)
  18. htd.addFamily(new HColumnDescriptor(cf))
  19. admin.createTable(htd)
  20. //3. 插入测试数据
  21. val rng = new Random()
  22. val k: Array[Byte] = new Array[Byte](3)
  23. val famAndQf = KeyValue.parseColumn(Bytes.toBytes(column))
  24. val puts = new util.ArrayList[Put]()
  25. var i = 0
  26. for (b1 <- ('a' to 'z')) {
  27. for (b2 <- ('a' to 'z')) {
  28. for (b3 <- ('a' to 'z')) {
  29. if(i < 10) {
  30. k(0) = b1.toByte
  31. k(1) = b2.toByte
  32. k(2) = b3.toByte
  33. val put = new Put(k)
  34. put.addColumn(famAndQf(0), famAndQf(1), ("value_" + b1 + b2 + b3).getBytes())
  35. puts.add(put)
  36. i = i + 1
  37. }
  38. }
  39. }
  40. }
  41. val conn = ConnectionFactory.createConnection(conf)
  42. val table = conn.getTable(tableName)
  43. table.put(puts)
  44. //4. 创建spark表
  45. val sparkTableName = "spark_hbase"
  46. val createCmd = s"""CREATE TABLE ${sparkTableName} USING org.apache.hadoop.hbase.spark
  47. | OPTIONS ('catalog'=
  48. | '{"table":{"namespace":"$${hbaseTableName}", "name":"${hbaseTableName}"},"rowkey":"rowkey",
  49. | "columns":{
  50. | "col0":{"cf":"rowkey", "col":"rowkey", "type":"string"},
  51. | "col1":{"cf":"cf1", "col":"a", "type":"string"},
  52. | "col2":{"cf":"cf1", "col":"b", "type":"String"}}}'
  53. | )""".stripMargin
  54. println(" createCmd: \n" + createCmd + " rows : " + rowsCount)
  55. sparkSession.sql(createCmd)
  56. //5. 执行count sql
  57. val result = sparkSession.sql("select count(*) from " + sparkTableName)
  58. val sparkCounts = result.collect().apply(0).getLong(0)
  59. println(" sparkCounts : " + sparkCounts)