HBase的增删改

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 HBaseUtil {

//创建链接对象
public static Connection connection=null;

static {
    try {

        //创建链接
        //创建配置对象
        Configuration conf = HBaseConfiguration.create();
        //设置参数和分机
        conf.set("hbase.zookeeper.quorum", "spark2", "spark3");
        //设置端口号
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        //赋值给对象
        connection = ConnectionFactory.createConnection(conf);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//创建表
public static void createTable(String tablename,String... families) throws IOException {
    //操作表结构,调用admin
    Admin admin = connection.getAdmin();
    //创建表的描述器
    HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tablename));
    //循环添加列族,如果有多个的话
    for (String family : families) {
        //创建列族的描述器
        HColumnDescriptor columndesc = new HColumnDescriptor(family);
        //向表的描述器中添加列族
        desc.addFamily(columndesc);

    }
    //使用admin对象添加表
    admin.createTable(desc);
    System.out.println("创建成功");
    //关闭对象
    admin.close();

}

//添加数据
public static void putCell(String tablename,String rowKey,String family,String column,String value) throws IOException {
    //操作表内部数据,调用gettable
    Table table = connection.getTable(TableName.valueOf(tablename));
    try {
        //创建put对象,把rowkey传进去
        Put put = new Put(Bytes.toBytes(rowKey));
        //把其他参数也传进去
        put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value));
        //像表中添加数据
        table.put(put);
    }
    finally {
        table.close();
    }

}

//查询表中所有数据
public static void getall(String tablename) throws IOException {
    Table table = connection.getTable(TableName.valueOf(tablename));
    //创建scan对象
    Scan scan = new Scan();
    //获取扫描器
    ResultScanner scanner = table.getScanner(scan);
    for (Result result : scanner) {
        //获取所有的列并循环
        for (Cell cell : result.rawCells()) {
            String rowStr = Bytes.toString(CellUtil.cloneRow(cell));
            String familyStr = Bytes.toString(CellUtil.cloneFamily(cell));
            String columnStr = Bytes.toString(CellUtil.cloneQualifier(cell));
            String valueStr = Bytes.toString(CellUtil.cloneValue(cell));
            System.out.println(rowStr+"-"+familyStr+"-"+columnStr+"-"+valueStr);
        }
    }
    scanner.close();
    table.close();
}

//查询出user_info表中行键值为“rk002”的数据。
public static void getrow(String tablename,String rowKey) throws IOException {
    Table table = connection.getTable(TableName.valueOf(tablename));
    //调用get对象,传入要查询的rowkey
    Get get = new Get(Bytes.toBytes(rowKey));
    Result result = table.get(get);
    //遍历多个列
    for (Cell cell : result.rawCells()) {
        //列族
        String familyStr = Bytes.toString(CellUtil.cloneFamily(cell));
        //列
        String columnStr = Bytes.toString(CellUtil.cloneQualifier(cell));
        //值
        String valueStr = Bytes.toString(CellUtil.cloneValue(cell));
        System.out.println(familyStr+"-"+columnStr+"-"+valueStr);
    }
    table.close();

}

//删除user_info表中行键为“rk002”的数据
public static void dropRow(String tablename,String rowkey) throws IOException {
    Table table = connection.getTable(TableName.valueOf(tablename));

        Delete delete = new Delete(Bytes.toBytes(rowkey));
        table.delete(delete);
        table.close();


}

//删除表
public static void droptable(String tablename) throws IOException {
    //操作表结构,调用admin对象
    Admin admin = connection.getAdmin();
    //disable取消授权
    if (!admin.tableExists(TableName.valueOf(tablename))){
        System.out.println("表不存在");
    }
    admin.disableTable(TableName.valueOf(tablename));
    admin.deleteTable(TableName.valueOf(tablename));
    //关闭对象
    admin.close();
}

public static void main(String[] args) throws IOException {

// createTable(“user_info”,“info”,“data”);
// putCell(“user_info”,“rk001”,“info”,“name”,“xiaoming”);
// putCell(“user_info”,“rk001”,“data”,“score”,“90”);
// putCell(“user_info”,“rk002”,“info”,“name”,“xiaohong”);
// putCell(“user_info”,“rk002”,“data”,“score”,“85”);
// putCell(“user_info”,“rk003”,“info”,“name”,“xiaohua”);
// putCell(“user_info”,“rk003”,“data”,“score”,“88”);
// getall(“user_info”);
// getrow(“user_info”,“rk002”);
// dropRow(“user_info”,“rk002”);
droptable(“user_info”);

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值