Hbase 实现增删改查操作

Linux下实现增删改查

准备条件

首先在Linux系统大搭建好三台虚拟机,配置好Hadoop,zookeeper以及hbase
如果只是用hbase shell 实现表格操作,可以忽略03链接
01链接: Hadoop集群配置
02 链接hbase安装
03 链接:Linux hbase java api配置

在Linux 实现增删改查

首先要启动Hadoop和hbase
hadoop01终端启动 start-all.sh
三台虚拟机同时启动 zkServer.sh start
hadoop01 启动 start-hbase.sh

用 hbase shell 命令实现操作

首先进入hbase数据库

hbase shell

创建student表增加name , score 列

create 'student','name',score'

增加学生晓清,并加入id为01

put 'student','01','score:Math:','100'//增加晓清的数学成绩
put 'student','01','score:English:','100'// 增加晓清的英语成绩

统计表行数

count 'student'

查看表数据

scan 'student'

条件查询

put 'student','01','score'

删除指定数据

deleteall 'student','01'

清空数据

truncate 'student'

删除表 , 删除之前要 disable

disable 'student'
drop 'student'

查看表是否存在

exists 'student'

Linux 下 用 Java api 实现 hbase 增删改查

按照03链接在虚拟机下安装好Java api , 然后输入以下代码

package HBaseExample;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
 
import java.io.IOException;
public class ExampleForHBase {
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
    public static void main(String[] args)throws IOException{
        init();
        createTable("student",new String[]{"score"});
        insertData("student","zhangsan","score","English","69");
        insertData("student","zhangsan","score","Math","86");
        insertData("student","zhangsan","score","Computer","77");
        getData("student", "zhangsan", "score","Math");
        close();
    }
    
   //创建连接
    public static void init(){
        configuration  = HBaseConfiguration.create();
        configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
        try{
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
            System.out.println("连接成功");
        }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();
        }
    }
 
    //创建表
    public static void createTable(String myTableName,String[] colFamily) throws IOException {
        TableName tableName = TableName.valueOf(myTableName);
        //判断是否已经存在
        if(admin.tableExists(tableName)){
            System.out.println("talbe is exists!");
        	admin. disableTableAsync(TableName.valueOf(myTableName));
        	admin.deleteTable(TableName.valueOf(myTableName));
        }
        else{
        	   TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
               for(String str:colFamily){
                   ColumnFamilyDescriptor family = 
    ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();
                   tableDescriptor.setColumnFamily(family);
               admin.createTable(tableDescriptor.build());	
               }
        } 
    }
    
    //插入数据 
    public static void insertData(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
        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();
        System.out.println("插入成功");
    }
    
    //查询 
    public static void getData(String tableName,String rowKey,String colFamily, String col)throws  IOException{
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(rowKey.getBytes());
        get.addColumn(colFamily.getBytes(),col.getBytes());
        Result result = table.get(get);
        System.out.println(new String(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes())));
        table.close(); 
    }
    
    // 删除表
    public static void delData(String tableName) throws IOException{
    	Table table = connection.getTable(TableName.valueOf(tableName));
    	admin. disableTableAsync(TableName.valueOf(tableName));
    	admin.deleteTable(TableName.valueOf(tableName));
    	System.out.println("删除成功");
    }
}

在windows 下实现增删改查

同样是需要先配置Hadoop 和hbase 然后配置Java api
具体配置windows Java api 看这里@配置Windows 下 hbase java api

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值