0


【hdfs】【hbase】【大数据技术基础】实践二 HBase Java API编程

*实践二 HBase Java API编程*

为什么可以写命令还要编写程序?自动化批量处理?

    尽管我们可以通过HBase的shell命令行工具进行数据操作,但在实际的生产环境中,为了提高效率和实现自动化处理,我们通常需要编写程序来与HBase进行交互。

    本实例使用Eclipse编写java程序,来对HBase数据库进行增删改查等操作,Eclipse可以在Ubuntu软件中心搜索下载并安装。

第一步:启动hadoop,启动hbase

    在开始之前,确保你已经安装了Hadoop和HBase,并且它们正在运行。你可以通过以下命令启动它们:
  1. cd /usr/local/hadoop
  2. ./sbin/start-dfs.sh
  3. cd /usr/local/hbase
  4. ./bin/start-hbase.sh

HBase 命令要在HBase shell中使用

启动 HBase shell: 在命令行中输入以下命令来启动 HBase shell:

hbase shell
    如果 HBase 服务启动成功,你将看到 HBase shell 的提示符,通常是一个光标后面跟着 
hbase>

HBase命令使用:使用scan ‘Score’,示例:

:因为我们表还没被创建,报错Unknown table Score是正常的。

报错了可能因为少了:ssh localhost

这个命令可不能少!使用SSH登录本地主机,确保HBase Shell可以正常访问。

第二步,新建Java Project——>新建Class

第三步:在工程中导入外部jar包:

首先导入前面的HDFS API相应类包(实践一中导入的)。
然后导入hbase安装目录中的lib文件中的所有jar****包。

下列代码包含了数据库创建、增删改查等一系列操作,需要大家根据需要逐步开放注释,进行分阶段运行。
这里给出一个编程实例,,以下是源代码:

  1. import org.apache.hadoop.conf.Configuration;

  2. import org.apache.hadoop.hbase.*;

  3. import org.apache.hadoop.hbase.client.*;

  4. import java.io.IOException;

  5. public class ExampleForHbase{

  6. public static Configuration configuration;
    
  7. public static Connection connection;
    
  8. public static Admin admin;
    
  9. //主函数中的语句请逐句执行,只需删除其前的//即可,如:执行insertRow时**请将其他语句注释**
    
  10. public static void main(String[] args)throws IOException{
    
  11.     //创建一个表,表名为Score,列族为sname,course
    
  12.     createTable("Score",new String[]{"sname","course"});
    
  13.     //在Score表中插入一条数据,其行键为95001,sname为Mary(因为sname列族下没有子列所以第四个参数为空)
    
  14.     //等价命令:put 'Score','95001','sname','Mary'
    
  15.     //insertRow("Score", "95001", "sname", "", "Mary");
    
  16.     //在Score表中插入一条数据,其行键为95001,course:Math为88(course为列族,Math为course下的子列)
    
  17.     //等价命令:put 'Score','95001','score:Math','88'
    
  18.     //insertRow("Score", "95001", "course", "Math", "88");
    
  19.     //在Score表中插入一条数据,其行键为95001,course:English为85(course为列族,English为course下的子列)
    
  20.     //等价命令:put 'Score','95001','score:English','85'
    
  21.     //insertRow("Score", "95001", "course", "English", "85");
    
  22.     //1、删除Score表中指定列数据,其行键为95001,列族为course,列为Math
    
  23.     //执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码取消注释注释,将删除制定列族的代码注释
    
  24.     //等价命令:delete 'Score','95001','score:Math'
    
  25.     //deleteRow("Score", "95001", "course", "Math");
    
  26.     //2、删除Score表中指定列族数据,其行键为95001,列族为course(95001的Math和English的值都会被删除)
    
  27.     //执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码注释,将删除制定列族的代码取消注释
    
  28.     //等价命令:delete 'Score','95001','score'
    
  29.     //deleteRow("Score", "95001", "course", "");
    
  30.     //3、删除Score表中指定行数据,其行键为95001
    
  31.     //执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码注释,以及将删除制定列族的代码注释
    
  32.     //等价命令:deleteall 'Score','95001'
    
  33.     //deleteRow("Score", "95001", "", "");
    
  34.     //查询Score表中,行键为95001,列族为course,列为Math的值
    
  35.     //getData("Score", "95001", "course", "Math");
    
  36.     //查询Score表中,行键为95001,列族为sname的值(因为sname列族下没有子列所以第四个参数为空)
    
  37.     //getData("Score", "95001", "sname", "");
    
  38.     //删除Score表
    
  39.     //deleteTable("Score");
    
  40. }
    
  41. //建立连接
    
  42. public static void init(){
    
  43.     configuration  = HBaseConfiguration.create();
    
  44.     configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
    
  45.     try{
    
  46.         connection= ConnectionFactory.createConnection(configuration);
    
  47.         admin = connection.getAdmin();
    
  48.     }catch (IOException e){
    
  49.         e.printStackTrace();
    
  50.     }
    
  51. }
    
  52. //关闭连接
    
  53. public static void close(){
    
  54.     try{
    
  55.         if(admin != null){
    
  56.             admin.close();
    
  57.         }
    
  58.         if(null != connection){
    
  59.             connection.close();
    
  60.         }
    
  61.     }catch (IOException e){
    
  62.         e.printStackTrace();
    
  63.     }
    
  64. }
    
  65. /**
    
  66.  * 建表。HBase的表中会有一个系统默认的属性作为主键,主键无需自行创建,默认为put命令操作中表名后第一个数据,因此此处无需创建id列
    
  67.  * @param myTableName 表名
    
  68.  * @param colFamily 列族名
    
  69.  * @throws IOException
    
  70.  */
    
  71. public static void createTable(String myTableName,String[] colFamily) throws IOException {
    
  72.     init();
    
  73.     TableName tableName = TableName.valueOf(myTableName);
    
  74.     if(admin.tableExists(tableName)){
    
  75.         System.out.println("talbe is exists!");
    
  76.     }else {
    
  77.         HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
    
  78.         for(String str:colFamily){
    
  79.             HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
    
  80.             hTableDescriptor.addFamily(hColumnDescriptor);
    
  81.         }
    
  82.         admin.createTable(hTableDescriptor);
    
  83.         System.out.println("create table success");
    
  84.     }
    
  85.     close();
    
  86. }
    
  87. /**
    
  88.  * 删除指定表
    
  89.  * @param tableName 表名
    
  90.  * @throws IOException
    
  91.  */
    
  92. public static void deleteTable(String tableName) throws IOException {
    
  93.     init();
    
  94.     TableName tn = TableName.valueOf(tableName);
    
  95.     if (admin.tableExists(tn)) {
    
  96.         admin.disableTable(tn);
    
  97.         admin.deleteTable(tn);
    
  98.     }
    
  99.     close();
    
  100. }
    
  101. /**
    
  102.  * 查看已有表
    
  103.  * @throws IOException
    
  104.  */
    
  105. public static void listTables() throws IOException {
    
  106.     init();
    
  107.     HTableDescriptor hTableDescriptors[] = admin.listTables();
    
  108.     for(HTableDescriptor hTableDescriptor :hTableDescriptors){
    
  109.         System.out.println(hTableDescriptor.getNameAsString());
    
  110.     }
    
  111.     close();
    
  112. }
    
  113. /**
    
  114.  * 向某一行的某一列插入数据
    
  115.  * @param tableName 表名
    
  116.  * @param rowKey 行键
    
  117.  * @param colFamily 列族名
    
  118.  * @param col 列名(如果其列族下没有子列,此参数可为空)
    
  119.  * @param val 值
    
  120.  * @throws IOException
    
  121.  */
    
  122. public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
    
  123.     init();
    
  124.     Table table = connection.getTable(TableName.valueOf(tableName));
    
  125.     Put put = new Put(rowKey.getBytes());
    
  126.     put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
    
  127.     table.put(put);
    
  128.     table.close();
    
  129.     close();
    
  130. }
    
  131. /**
    
  132.  * 删除数据
    
  133.  * @param tableName 表名
    
  134.  * @param rowKey 行键
    
  135.  * @param colFamily 列族名
    
  136.  * @param col 列名
    
  137.  * @throws IOException
    
  138.  */
    
  139. public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException {
    
  140.     init();
    
  141.     Table table = connection.getTable(TableName.valueOf(tableName));
    
  142.     Delete delete = new Delete(rowKey.getBytes());
    
  143.     //删除指定列族的所有数据
    
  144.     //delete.addFamily(colFamily.getBytes());
    
  145.     //删除指定列的数据
    
  146.     //delete.addColumn(colFamily.getBytes(), col.getBytes());
    

这里应该是:

if (col != null && !col.isEmpty()) {

    // 如果有 col 参数传入,则删除指定列

    delete.addColumn(colFamily.getBytes(), col.getBytes());

} else if(colFamily != null && !colFamily.isEmpty()) {

    // 否则,删除指定列族的所有数据

    delete.addFamily(colFamily.getBytes());

}
  1.     table.delete(delete);
    
  2.     table.close();
    
  3.     close();
    
  4. }
    
  5. /**
    
  6.  * 根据行键rowkey查找数据
    
  7.  * @param tableName 表名
    
  8.  * @param rowKey 行键
    
  9.  * @param colFamily 列族名
    
  10.  * @param col 列名
    
  11.  * @throws IOException
    
  12.  */
    
  13. public static void getData(String tableName,String rowKey,String colFamily,String col)throws  IOException{
    
  14.     init();
    
  15.     Table table = connection.getTable(TableName.valueOf(tableName));
    
  16.     Get get = new Get(rowKey.getBytes());
    
  17.     get.addColumn(colFamily.getBytes(),col.getBytes());
    
  18.     Result result = table.get(get);
    
  19.     showCell(result);
    
  20.     table.close();
    
  21.     close();
    
  22. }
    
  23. /**
    
  24.  * 格式化输出
    
  25.  * @param result
    
  26.  */
    
  27. public static void showCell(Result result){
    
  28.     Cell[] cells = result.rawCells();
    
  29.     for(Cell cell:cells){
    
  30.         System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
    
  31.         System.out.println("Timetamp:"+cell.getTimestamp()+" ");
    
  32.         System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
    
  33.         System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
    
  34.         System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
    
  35.     }
    
  36. }
    
  37. }

第四步:执行与验证

    每次执行完,都可以****回到********shell********界面查看是否执行成功****,如:执行完插入数据后,在shell界面中执行scan 'Score'。截图如下:

建表:

建表成功~

添加数据:

删除指定列:

删除指定列族:

删除整行数据:

查询Score表中,行键为95001,列族为course,列为Math的值:

查询Score表中,行键为95001,列族为sname的值(因为sname列族下没有子列所以第四个参数为空):

【遇到的问题】:为什么打印了内存地址而不是值?

【解决方法】:把+" "删除就好了?

标签: hadoop 大数据 hdfs

本文转载自: https://blog.csdn.net/m0_73972962/article/details/143184932
版权归原作者 Kika写代码 所有, 如有侵权,请联系我们删除。

“【hdfs】【hbase】【大数据技术基础】实践二 HBase Java API编程”的评论:

还没有评论