1.首先导入HBase Java API所需的所有jar包

导入以下两个目录中的所有 jar 包
/usr/local/hbase/lib/.jar
/usr/local/hbase/lib/client-facing-thirdparty/
.jar
注意!!此处不再需要导入Hadoop安装目录下的JAR包,这样可以避免由于Hadoop和HBase的版本冲突而引起的错误。否则运行HBase程序时将会报很多不兼容的错误。

2.测试代码运行

在运行代码前必须先启动 hadoop 和 hbase

  1. import org.apache.hadoop.conf.Configuration;
  2. import org.apache.hadoop.hbase.*;
  3. import org.apache.hadoop.hbase.client.*;
  4. import org.apache.hadoop.hbase.util.Bytes;
  5. import java.io.IOException;
  6. import com.thoughtworks.paranamer.JavadocParanamer;
  7. public class ExampleForHBase {
  8. public static Configuration configuration; //管理 HBase 的配置信息
  9. public static Connection connection; //管理 HBase连接
  10. public static Admin admin; //管理 HBase数据库的表信息
  11. public static void main(String[] args)throws IOException{
  12. init(); //建立连接
  13. createTable("student",new String[]{"score"}); //建表
  14. insertData("student","zhangsan","score","English","69"); //插入单元格数据
  15. insertData("student","zhangsan","score","Math","86"); //同上
  16. insertData("student","zhangsan","score","Computer","77"); //同上
  17. getData("student", "zhangsan", "score","English"); //浏览单元格数据
  18. close(); //关闭连接
  19. }
  20. //建立连接
  21. public static void init(){
  22. configuration = HBaseConfiguration.create();
  23. configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase"); //指定HBase数据存储的根目录,这里底层使用hdfs作为存储
  24. try{
  25. connection = ConnectionFactory.createConnection(configuration);
  26. admin = connection.getAdmin();
  27. }catch (IOException e){
  28. e.printStackTrace();
  29. }
  30. }
  31. public static void close(){
  32. try{
  33. if(admin != null){
  34. admin.close();
  35. }
  36. if(null != connection){
  37. connection.close();
  38. }
  39. }catch (IOException e){
  40. e.printStackTrace();
  41. }
  42. }
  43. /**
  44. * 创建表
  45. * @param myTableName 表名
  46. * @param colFamily 列族数组
  47. * @throws IOException
  48. */
  49. public static void createTable(String myTableName,String[] colFamily) throws IOException {
  50. TableName tableName = TableName.valueOf(myTableName);
  51. if(admin.tableExists(tableName)){
  52. System.out.println("talbe is exists!");
  53. }else {
  54. TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
  55. for(String str:colFamily){
  56. ColumnFamilyDescriptor family =
  57. ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();
  58. tableDescriptor.setColumnFamily(family);
  59. }
  60. admin.createTable(tableDescriptor.build());
  61. }
  62. }
  63. /**
  64. * 添加数据
  65. * @param tableName 表名
  66. * @param rowKey 行键
  67. * @param colFamily 列族
  68. * @param col 列限定符(列族中的指定列名)
  69. * @param val 数据
  70. * @throws IOException
  71. */
  72. public static void insertData(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
  73. Table table = connection.getTable(TableName.valueOf(tableName));
  74. Put put = new Put(rowKey.getBytes());
  75. put.addColumn(colFamily.getBytes(),col.getBytes(), val.getBytes());
  76. table.put(put);
  77. table.close();
  78. }
  79. /**
  80. * 获取某单元格数据
  81. * @param tableName 表名
  82. * @param rowKey 行键
  83. * @param colFamily 列族
  84. * @param col 列限定符
  85. * @throws IOException
  86. */
  87. public static void getData(String tableName,String rowKey,String colFamily, String col)throws IOException{
  88. Table table = connection.getTable(TableName.valueOf(tableName));
  89. Get get = new Get(rowKey.getBytes());
  90. get.addColumn(colFamily.getBytes(),col.getBytes());
  91. Result result = table.get(get);
  92. System.out.println(new String(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes())));
  93. table.close();
  94. }
  95. }

成功运行后,会在输出中看到 69 的字样
image.png
使用scan命令,查看新建的student表中的数据
image.png