栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

hbase增删改查

hbase增删改查

package com.imooc.hbase;

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.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HbaseOp {
    public HbaseOp() {
    }

    public static void main(String[] args)  throws Exception{
        //获取配置
        Configuration conf= HbaseConfiguration.create();
        //指定Hbase使用zk的地址,多个用逗号隔开
        conf.set("hbase.zookeeper.quorum","bigdata01:2181,bigdata02:2181,bigdata03:2181");
        // 指定Hbase在hdfs上的根目录
        conf.set("hbase.rootdir","hdfs://bigdata01:9000/hbase");
        // 创建Hbase连接,负责Hbase中数据的一些增删改查DML操作
        Connection conn= ConnectionFactory.createConnection(conf);
//       添加数据
//        put(conn);
        //查询数据
//        get(conn);
        // 查询多版本的数据 conn getMoreVersion

//        getMoreVersion(conn);
//        删除数据
//        delete(conn);
//        获取管理权限,负责对Hbase中的表进行操作DDL
        Admin admin= conn.getAdmin();
        admin.close();
        conn.close();
// 创建表
//        createTable(admin);
//        删除表,先禁用表
//        deleteTable(admin);

    }

    private static void deleteTable(Admin admin) throws IOException {
        admin.disableTable(TableName.valueOf("test"));
        admin.deleteTable(TableName.valueOf("test"));
    }

    private static void createTable(Admin admin) throws IOException {
        ColumnFamilyDescriptor familyDesc1=ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info"))
                .setMaxVersions(3).build();
        ColumnFamilyDescriptor familyDesc2=ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("level"))
                .setMaxVersions(2).build();
        ArrayList familyList= new ArrayList();
        familyList.add(familyDesc1);
        familyList.add(familyDesc2);
        // 获取TableDescriptor对象
        TableDescriptor desc=TableDescriptorBuilder.newBuilder(TableName.valueOf("test"))
                .setColumnFamilies(familyList).build();
        admin.createTable(desc);
    }

    private static void delete(Connection conn) throws IOException {
        Table table= conn.getTable(TableName.valueOf("student"));
        Delete delete=new Delete(Bytes.toBytes("laowang"));
        table.delete(delete);
        table.close();
    }

    private static void getMoreVersion(Connection conn) throws IOException {
        Table table= conn.getTable(TableName.valueOf("student"));
        Get get =new Get(Bytes.toBytes("laowang"));
        get.readAllVersions();
        Result result=table.get(get);
        List columnCells=result.getColumnCells(Bytes.toBytes("info"),Bytes.toBytes("age"));
        for (Cell cell:columnCells){
            byte[] value_bytes=CellUtil.clonevalue(cell);
            long timestamp=cell.getTimestamp();
            System.out.println("值为:"+new String(value_bytes)+",时间戳:"+timestamp);

        }
        table.close();
    }

    private static void get(Connection conn) throws IOException {
        Table table= conn.getTable(TableName.valueOf("student"));
        Get get =new Get(Bytes.toBytes("laowang"));
        Result result=table.get(get);
        List cells=result.listCells();
        for (Cell cell:cells){
            byte[] famaily_bytes= CellUtil.cloneFamily(cell);
            byte[] column_bytes=CellUtil.cloneQualifier(cell);
            byte[] value_bytes=CellUtil.clonevalue(cell);
            System.out.println("列族:"+new String(famaily_bytes)+",列:"+new String(column_bytes)+",值:"+new String(value_bytes));

        }
        System.out.println("========================================================");

        byte[] age_bytes= result.getValue(Bytes.toBytes("info"),Bytes.toBytes("age"));
        System.out.println("age列的值"+new String(age_bytes));
        table.close();
    }

    private static void put(Connection conn) throws IOException {
        Table table= conn.getTable(TableName.valueOf("student"));
        Put put=new Put(Bytes.toBytes("laowang"));
        put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes("18"));
        put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("sex"),Bytes.toBytes("man"));
        put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("class"),Bytes.toBytes("A"));
        table.put(put);
        table.close();
    }


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/443539.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号