Hbase常用操作命令及JavaAPI

文章目录

 

一、Hbase常用操作命令

1. 进入hbase

./hbase shell

  • 参数说明
    • 进入到hbase目录并进入hbase

2. 浏览所有表

list

  • 参数说明
    • 查看表列表

3. 查看表结构

describe ‘BizvaneV2.VipSearch’

  • 参数说明
    • describe 动作命令,BizvaneV2.VipSearch 为要查看表结构的表名

4. 创建表

create ‘UserInfo’, { NAME => ‘info’, REPLICATION_SCOPE => ‘1’ }

  • 参数说明
    • create 动作命令,UserInfo 为要创建的表名
    • NAME 列族名,info 为列族名称
    • REPLICATION_SCOPE 是否复制,0为不复制,1为复制

5. 修改表结构

disable ‘UserInfo’
alter ‘UserInfo’, {NAME => ‘extendInfo’, REPLICATION_SCOPE => ‘1’}
describe ‘UserInfo’

  • 参数说明
    • 修改表结构要使表不启用状态
    • disable 动作命令,使要修改结构的表无效,UserInfo 为表名
    • 修改命令
      • alter 动作命令
      • UserInfo 要修改的表名称
      • {NAME => ‘extendInfo’, REPLICATION_SCOPE => ‘1’} 要修改的结构体
    • describe 动作命令,查看表结构描述,验证是否修改成功

6. 添加数据

put ‘UserInfo’,‘row_1’,‘info:firstname’,‘liu’

  • 参数说明
    • 添加命令
      • put 动作命令,
      • UserInfo 要添加数据的表名称
      • row_1 数据行
      • info:firstname 列及列名字
      • liu 要添加的值

7. 查看数据

scan ‘UserInfo’

  • 参数说明
    • scan 动作命令
      • 要查看表的名称

8. 修改数据

put ‘UserInfo’,‘row_1’,‘info:firstname’,‘liu_copy’

  • 参数说明
    • 添加命令
      • put 动作命令,
      • UserInfo 要修改数据的表名称
      • row_1 数据行
      • info:firstname 列及列名字
      • liu 新数据值

9. 删除数据

delete ‘UserInfo’,‘row_1’,'info:firstname’参数说明

  • 删除命令
    • delete 动作命令,
    • UserInfo 要修改数据的表名称
    • row_1 数据行
    • info:firstname 列及列名字

10. 删除表

disable ‘UserInfo’
drop ‘UserInfo’

  • 参数说明
  • 修改表结构要使表不启用状态
    • 删除命令
      • disable 使表不启用,UserInfo 要不启用的表名称
      • drop 删除命令 UserInfo 要删除的表名称

11. 帮助

help

  • 参数说明
  • help 帮助命令,可以查看Hbase提供的命令清单

二、Hbase Java操作API

public class HbaseDemo {
 
    private static Admin admin;
 
    public static void main(String[] args){
        try {
              createTable("user_table", new String[] { "information", "contact" });
              User user = new User("001", "xiaoming", "123456", "man", "20", "13355550021", "1232821@csdn.com");
              insertData("user_table", user);
              User user2 = new User("002", "xiaohong", "654321", "female", "18", "18757912212", "214214@csdn.com");
              insertData("user_table", user2);
              List<User> list = getAllData("user_table");
              System.out.println("--------------------插入两条数据后--------------------");
              for (User user3 : list){
                 System.out.println(user3.toString());
              }
              System.out.println("--------------------获取原始数据-----------------------");
              getNoDealData("user_table");
              System.out.println("--------------------根据rowKey查询--------------------");
              User user4 = getDataByRowKey("user_table", "user-001");
              System.out.println(user4.toString());
              System.out.println("--------------------获取指定单条数据-------------------");
              String user_phone = getCellData("user_table", "user-001", "contact", "phone");
              System.out.println(user_phone);
              User user5 = new User("test-003", "xiaoguang", "789012", "man", "22", "12312132214", "856832@csdn.com");
              insertData("user_table", user5);
              List<User> list2 = getAllData("user_table");
              System.out.println("--------------------插入测试数据后--------------------");
              for (User user6 : list2){
                  System.out.println(user6.toString());
              }
              deleteByRowKey("user_table", "user-test-003");
              List<User> list3 = getAllData("user_table");
              System.out.println("--------------------删除测试数据后--------------------");
              for (User user7 : list3){
                  System.out.println(user7.toString());
              }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    //连接集群
    public static Connection initHbase() throws IOException {
 
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        configuration.set("hbase.zookeeper.quorum", "127.0.0.1");
        //集群配置↓
        //configuration.set("hbase.zookeeper.quorum", "101.236.39.141,101.236.46.114,101.236.46.113");
        configuration.set("hbase.master", "127.0.0.1:60000");
        Connection connection = ConnectionFactory.createConnection(configuration);
        return connection;
    }
 
    //创建表
    public static void createTable(String tableNmae, String[] cols) throws IOException {
 
        TableName tableName = TableName.valueOf(tableNmae);
        admin = initHbase().getAdmin();//由连接得到客户端
        if (admin.tableExists(tableName)) {
            System.out.println("表已存在!");
        } else {
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            for (String col : cols) {
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col);
                hTableDescriptor.addFamily(hColumnDescriptor);
            }
            admin.createTable(hTableDescriptor);
        }
    }
 
    //插入数据
    public static void insertData(String tableName, User user) throws IOException {
        TableName tablename = TableName.valueOf(tableName);
        Put put = new Put(("user-" + user.getId()).getBytes());
        //参数:1.列族名  2.列名  3.值
        put.addColumn("information".getBytes(), "username".getBytes(), user.getUsername().getBytes()) ;
        put.addColumn("information".getBytes(), "age".getBytes(), user.getAge().getBytes()) ;
        put.addColumn("information".getBytes(), "gender".getBytes(), user.getGender().getBytes()) ;
        put.addColumn("contact".getBytes(), "phone".getBytes(), user.getPhone().getBytes());
        put.addColumn("contact".getBytes(), "email".getBytes(), user.getEmail().getBytes());
        //HTable table = new HTable(initHbase().getConfiguration(),tablename);已弃用
        Table table = initHbase().getTable(tablename);
        table.put(put);
    }
 
    //获取原始数据
    public static void getNoDealData(String tableName){
        try {
            Table table= initHbase().getTable(TableName.valueOf(tableName));
            Scan scan = new Scan();
            ResultScanner resutScanner = table.getScanner(scan);
            for(Result result: resutScanner){
                System.out.println("scan:  " + result);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    //根据rowKey进行查询
    public static User getDataByRowKey(String tableName, String rowKey) throws IOException {
 
        Table table = initHbase().getTable(TableName.valueOf(tableName));
        Get get = new Get(rowKey.getBytes());
        User user = new User();
        user.setId(rowKey);
        //先判断是否有此条数据
        if(!get.isCheckExistenceOnly()){
            Result result = table.get(get);
            for (Cell cell : result.rawCells()){
                String colName = Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
                String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                if(colName.equals("username")){
                    user.setUsername(value);
                }
                if(colName.equals("age")){
                    user.setAge(value);
                }
                if (colName.equals("gender")){
                    user.setGender(value);
                }
                if (colName.equals("phone")){
                    user.setPhone(value);
                }
                if (colName.equals("email")){
                    user.setEmail(value);
                }
            }
        }
        return user;
    }
 
    //查询指定单cell内容
    public static String getCellData(String tableName, String rowKey, String family, String col){
 
        try {
            Table table = initHbase().getTable(TableName.valueOf(tableName));
            String result = null;
            Get get = new Get(rowKey.getBytes());
            if(!get.isCheckExistenceOnly()){
                get.addColumn(Bytes.toBytes(family),Bytes.toBytes(col));
                Result res = table.get(get);
                byte[] resByte = res.getValue(Bytes.toBytes(family), Bytes.toBytes(col));
                return result = Bytes.toString(resByte);
            }else{
                return result = "查询结果不存在";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "出现异常";
    }
 
    //查询指定表名中所有的数据
    public static List<User> getAllData(String tableName){
 
        Table table = null;
        List<User> list = new ArrayList<User>();
        try {
            table = initHbase().getTable(TableName.valueOf(tableName));
            ResultScanner results = table.getScanner(new Scan());
            User user = null;
            for (Result result : results){
                String id = new String(result.getRow());
                System.out.println("用户名:" + new String(result.getRow()));
                user = new User();
                for(Cell cell : result.rawCells()){
                       String row = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
                       //String family =  Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength());
                       String colName = Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
                       String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                       user.setId(row);
                       if(colName.equals("username")){
                            user.setUsername(value);
                       }
                       if(colName.equals("age")){
                            user.setAge(value);
                       }
                       if (colName.equals("gender")){
                           user.setGender(value);
                       }
                       if (colName.equals("phone")){
                           user.setPhone(value);
                       }
                       if (colName.equals("email")){
                           user.setEmail(value);
                       }
                }
                list.add(user);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
 
    //删除指定cell数据
    public static void deleteByRowKey(String tableName, String rowKey) throws IOException {
 
        Table table = initHbase().getTable(TableName.valueOf(tableName));
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        //删除指定列
        //delete.addColumns(Bytes.toBytes("contact"), Bytes.toBytes("email"));
        table.delete(delete);
    }
 
    //删除表
    public static void deleteTable(String tableName){
 
        try {
            TableName tablename = TableName.valueOf(tableName);
            admin = initHbase().getAdmin();
            admin.disableTable(tablename);
            admin.deleteTable(tablename);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
}
  • 操作的实体类
public class User {
 
    private String id;
    private String username;
    private String password;
    private String gender;
    private String age;
    private String phone;
    private String email;
 
    public User(String id, String username, String password, String gender, String age, String phone, String email) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.gender = gender;
        this.age = age;
        this.phone = phone;
        this.email = email;
    }
 
    public User(){
 
    }
 
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public String getGender() {
        return gender;
    }
 
    public void setGender(String gender) {
        this.gender = gender;
    }
 
    public String getAge() {
        return age;
    }
 
    public void setAge(String age) {
        this.age = age;
    }
 
    public String getPhone() {
        return phone;
    }
 
    public void setPhone(String phone) {
        this.phone = phone;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }
 
    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", gender='" + gender + '\'' +
                ", age='" + age + '\'' +
                ", phone='" + phone + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值