前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hadoop——HBase配置、shell编程和api编程

Hadoop——HBase配置、shell编程和api编程

作者头像
不愿意做鱼的小鲸鱼
发布2022-09-24 09:59:05
3480
发布2022-09-24 09:59:05
举报
文章被收录于专栏:web全栈web全栈

HBase的相关资料和配置文档

点击下载 链接:https://pan.baidu.com/s/17r-mfTTYwrgLFh50xDVEvA 提取码:h25r

HBase基本操作命令

  1. 启动HBase服务进程 cd /usr/local/hbase bin/start-hbase.sh
Hadoop——HBase配置、shell编程和api编程-左眼会陪右眼哭の博客
Hadoop——HBase配置、shell编程和api编程-左眼会陪右眼哭の博客
  1. HBase的shell编程 hbase shell
Hadoop——HBase配置、shell编程和api编程-左眼会陪右眼哭の博客
Hadoop——HBase配置、shell编程和api编程-左眼会陪右眼哭の博客
  1. 创建表student,列族1为stu_id,列族2为stu_name。 create ‘student’,’stu_id’,’stu_name’
  2. 查看所有的表 list
  3. 查看表结构 describe ‘student’
  4. 使用alter修改表的结构,增加一个列族more。 alter 'student','more'
  5. 为student表添加了学号为95001,名字为LiYing的一行数据,其行键为95001。 put 'student','95001',' stu_name ','LiYing'
  6. 为95001行下的course列族的math列添加了一个数据。 put 'student','95001','course:math','80'
  7. delete用于删除一个数据,是put的反向操作;deleteall操作用于删除一行数据。

* 删除了student表中95001行下的stu_name delete 'student','95001',’ stu_name’ * 删除了student表中的95001行的全部数据。 deleteall 'student','95001' 10. get命令,用于查看表的某一个单元格数据;scan命令用于查看某个表的全部数据。 * 返回的是‘student’表‘95001’行的数据 get 'student','95001' * 返回的是‘student’表的全部数据 scan 'student' 11. 删除表有两步,第一步先让该表不可用,第二步删除表

代码语言:javascript
复制
disable 'student'
drop 'student'
  1. 退出HBase数据库表操作 exit

HBase的Java API编程实例

详细的配置请看文档

Hadoop——HBase配置、shell编程和api编程-左眼会陪右眼哭の博客
Hadoop——HBase配置、shell编程和api编程-左眼会陪右眼哭の博客

基本代码如下

代码语言:javascript
复制
package hbaseTest;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class HbaseTest{
    // hadoop配置
    public static Configuration configuration;
    // hbase客户端连接
    public static Connection connection;
    //获取管理员权限
    public static Admin admin;

    //主函数中的语句请逐句执行,只需删除其前的//即可,如:执行insertRow时请将其他语句注释
    public static void main(String[] args)throws Exception{
        //创建一个表,表名为Score,列族为sname,course
//        createTable("Score",new String[]{"sname","course","age","sex"});
        //查看已有的表
//        listTables();
        //查看表的所有记录
//      selectAll("Score");

        //在Score表中插入一条数据,其行键为95001,sname为Mary(因为sname列族下没有子列所以第四个参数为空)
        //等价命令:put 'Score','95001','sname','Mary'
//        insertRow("Score", "95001", "sname", "", "Mary");
        //在Score表中插入一条数据,其行键为95001,course:Math为88(course为列族,Math为course下的子列)
        //等价命令:put 'Score','95001','score:Math','88'
//        insertRow("Score", "95001", "course", "Math", "88");
        //在Score表中插入一条数据,其行键为95001,course:English为85(course为列族,English为course下的子列)
        //等价命令:put 'Score','95001','score:English','85'
//        insertRow("Score", "95001", "course", "English", "85");

        //1、删除Score表中指定列数据,其行键为95001,列族为course,列为Math
        //执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码取消注释注释,将删除制定列族的代码注释
        //等价命令:delete 'Score','95001','score:Math'
        //deleteRow("Score", "95001", "course", "Math");

        //2、删除Score表中指定列族数据,其行键为95001,列族为course(95001的Math和English的值都会被删除)
        //执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码注释,将删除制定列族的代码取消注释
        //等价命令:delete 'Score','95001','score'
        //deleteRow("Score", "95001", "course", "");

        //3、删除Score表中指定行数据,其行键为95001
        //执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码注释,以及将删除制定列族的代码注释
        //等价命令:deleteall 'Score','95001'
//        deleteRow("Score", "95001", "", "");

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

        //删除Score表
//        deleteTable("Score");
    }

    //建立连接
    public static void init(){
        configuration  = HBaseConfiguration.create();
        configuration.set("hbase.rootdir","hdfs://192.168.119.128:9000/hbase");
        configuration.set("hbase.zookeeper.quorum","192.168.119.128");  //hbase 服务地址
        configuration.set("hbase.zookeeper.property.clientPort","2181"); //端口号
        try{
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //关闭连接
    public static void close(){
        try{
            if(admin != null){
                admin.close();
            }
            if(null != connection){
                connection.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }


    /**
     * 建表。HBase的表中会有一个系统默认的属性作为主键,主键无需自行创建,默认为put命令操作中表名后第一个数据,因此此处无需创建id列
     * @param myTableName 表名
     * @param colFamily 列族名
     * @throws IOException
     */
    public static void createTable(String myTableName,String[] colFamily) throws IOException {

        init();
        TableName tableName = TableName.valueOf(myTableName);

        if(admin.tableExists(tableName)){
            System.out.println("talbe is exists!");
        }else {
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            for(String str:colFamily){
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
                hTableDescriptor.addFamily(hColumnDescriptor);
            }
            admin.createTable(hTableDescriptor);
            System.out.println("create table success");
        }
        close();
    }
    /**
     * 删除指定表
     * @param tableName 表名
     * @throws IOException
     */
    public static void deleteTable(String tableName) throws IOException {
        init();
        TableName tn = TableName.valueOf(tableName);
        if (admin.tableExists(tn)) {
            admin.disableTable(tn);
            admin.deleteTable(tn);
        }
        close();
    }

    /**
     * 查看已有表
     * @throws IOException
     */
    public static void listTables() throws IOException {
        init();
        HTableDescriptor hTableDescriptors[] = admin.listTables();
        for(HTableDescriptor hTableDescriptor :hTableDescriptors){
            System.out.println(hTableDescriptor.getNameAsString());
        }
        close();
    }

    /**
     * 向某一行的某一列插入数据
     * @param tableName 表名
     * @param rowKey 行键
     * @param colFamily 列族名
     * @param col 列名(如果其列族下没有子列,此参数可为空)
     * @param val 值
     * @throws IOException
     */
    public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(rowKey.getBytes());
        put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
        table.put(put);
        table.close();
        close();
    }

    /**
     * 删除数据
     * @param tableName 表名
     * @param rowKey 行键
     * @param colFamily 列族名
     * @param col 列名
     * @throws IOException
     */
    public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException {
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Delete delete = new Delete(rowKey.getBytes());
        //删除指定列族的所有数据
        //delete.addFamily(colFamily.getBytes());
        //删除指定列的数据
        delete.addColumn(colFamily.getBytes(), col.getBytes());

        table.delete(delete);
        table.close();
        close();
    }
    /**
     * 根据行键rowkey查找数据
     * @param tableName 表名
     * @param rowKey 行键
     * @param colFamily 列族名
     * @param col 列名
     * @throws IOException
     */
    public static void getData(String tableName,String rowKey,String colFamily,String col)throws  IOException{
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(rowKey.getBytes());
        get.addColumn(colFamily.getBytes(),col.getBytes());
        Result result = table.get(get);
        showCell(result);
        table.close();
        close();
    }
    /**
     * 格式化输出
     * @param result
     */
    public static void showCell(Result result){
        Cell[] cells = result.rawCells();
        for(Cell cell:cells){
            System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
            System.out.println("Timetamp:"+cell.getTimestamp()+" ");
            System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
            System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
            System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
        }
    }
   //查询表中所有记录
    @SuppressWarnings("deprecation")
    public static void selectAll(String tableName) throws Exception{
        Table hTable = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        ResultScanner resultScanner = null;
        try {
            resultScanner = hTable.getScanner(scan);
            for (Result result : resultScanner){
                for(KeyValue keyValue :result.raw()){
                    StringBuilder sb = new StringBuilder();
                    sb.append(Bytes.toString(keyValue.getRow())).append("\t");
                    sb.append(Bytes.toString(keyValue.getFamily())).append("\t");
                    sb.append(Bytes.toString(keyValue.getQualifier())).append("\t");
                    sb.append(keyValue.getTimestamp()).append("\t");
                    sb.append(Bytes.toString(keyValue.getValue())).append("\t");
                    System.out.println(sb.toString());
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            if (resultScanner != null)
            {
                 resultScanner.close();
            }
            System.out.println("selectAll end");
        }      

    }


}

测试创建一个表,插入数据,获取数据

Hadoop——HBase配置、shell编程和api编程-左眼会陪右眼哭の博客
Hadoop——HBase配置、shell编程和api编程-左眼会陪右眼哭の博客
Hadoop——HBase配置、shell编程和api编程-左眼会陪右眼哭の博客
Hadoop——HBase配置、shell编程和api编程-左眼会陪右眼哭の博客
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • HBase的相关资料和配置文档
  • HBase基本操作命令
  • HBase的Java API编程实例
相关产品与服务
TDSQL MySQL 版
TDSQL MySQL 版(TDSQL for MySQL)是腾讯打造的一款分布式数据库产品,具备强一致高可用、全球部署架构、分布式水平扩展、高性能、企业级安全等特性,同时提供智能 DBA、自动化运营、监控告警等配套设施,为客户提供完整的分布式数据库解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档