zoukankan      html  css  js  c++  java
  • HBase API操作

    |的ascII最大
    ctrl+shift+t查找类  ctrl+p显示提示

    HBase API操作

    依赖的jar包

    <dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.3.1</version>
        </dependency>
    
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.3.1</version>
        </dependency>
    
    </dependencies>
    View Code
    public class TestHbase {
        //1.构建Configuration, Connection, Admin
        //Configuration 持有了zk的信息,进而hbase集群的信息可以间接获得
        public static Configuration conf;
        //Connection  hbase连接  借助配置信息 获得连接
        public static Connection connection;
        public static Admin admin;
        static{  //为静态属性初始化,或者说辅助类初始化
            conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", "hadoop101,hadoop102,hadoop103");
            try {
                connection = ConnectionFactory.createConnection(conf);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //admin
            try {
                admin = connection.getAdmin();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    //1.创建库
        public static void createNS(String namespace) throws IOException {
            //①构建  ns的描述器  声明库名
            NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(namespace).build();
            //②创建库
            try{
                admin.createNamespace(namespaceDescriptor);
            }catch (NamespaceExistException e){
                System.out.println("该库已经存在!");
            }
            //③关资源
            admin.close();
        }
        //2.判断表是否存在
        public static boolean isExists(String tableName) throws IOException {
            boolean exists = admin.tableExists(TableName.valueOf(tableName));
            System.out.println("exits:" + exists);
            admin.close();
            return exists;
        }
        //3.创建表
        public static void createTable(String tableName, String... info) throws IOException {
            //①HTableDescriptor
            HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
            //②添加columnFamily 列族
            for (String cf : info) {
                hTableDescriptor.addFamily(new HColumnDescriptor(cf));
            }
            //③建表
            admin.createTable(hTableDescriptor);
            //④释放资源
            admin.close();
        }
        public static void deleteTable(String tableName) throws IOException {
            //禁用并删除表
            admin.disableTable(TableName.valueOf(tableName));
            admin.deleteTable(TableName.valueOf(tableName));
            admin.close();
        }
        //5.插入数据  put 'student','1001','cf1:name','kris'
        public static void insertData(String tableName, String rowkey, String column, String value) throws IOException {
            //①获取table
            Table table = connection.getTable(TableName.valueOf(tableName));
            //②获得put
            Put put = new Put(Bytes.toBytes(rowkey));//把String类型转成bytes类型
            put.addColumn(Bytes.toBytes(column.split(":")[0]), Bytes.toBytes(column.split(":")[1]),
                    Bytes.toBytes(value));
            table.put(put); //③添加数据
            table.close();//④释放资源
        }
    //6.删除数据
        public static void deleteData(String tableName, String... rowkey) throws IOException {
            Table table = connection.getTable(TableName.valueOf(tableName));
            for (String rk : rowkey) {
                Delete del = new Delete(Bytes.toBytes(rk));//获得delete对象,其中持有要删除行的rowkey
                table.delete(del);
            }
            table.close();
        }
    //7.查询
        public static void  queryAll(String tableName) throws IOException {
            Table table = connection.getTable(TableName.valueOf(tableName));
            Scan scan = new Scan();
            ResultScanner results = table.getScanner(scan);
            for (Result result : results) { //result对应一行数据
                Cell[] cells = result.rawCells(); //获取一行的所有cells
                for (Cell cell : cells) {
                    String rowkey = Bytes.toString(CellUtil.cloneRow(cell));//
                    String family = Bytes.toString(CellUtil.cloneFamily(cell));
                    String column = Bytes.toString(CellUtil.cloneQualifier(cell));
                    String value = Bytes.toString(CellUtil.cloneValue(cell));
                    System.out.println("rowkey:" + rowkey + "	" + family + ":" + column
                    +"	" + value);
                }
            }
        }
    //8.查询单行
        public static void getRow(String tableName, String rowkey) throws IOException {
            Table table = connection.getTable(TableName.valueOf(tableName));
            Get get = new Get(Bytes.toBytes(rowkey));
            get.addColumn(Bytes.toBytes("cf2"), Bytes.toBytes("name"));
            //get.addFamily(Bytes.toBytes("cf1")); //如果不追加列族,则查询所有列族
            Result result = table.get(get);
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                System.out.println("查询单行");
                String row = Bytes.toString(CellUtil.cloneRow(cell));
                String family = Bytes.toString(CellUtil.cloneFamily(cell));
                String column = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println("row:" + row +"	" + family + ":" + column + "	" + value);
            }
        }

     

  • 相关阅读:
    C# Bitmap类型与Byte[]类型相互转化
    博客园添加个人Github链接
    C# Exception has been thrown by the target of an invocation(调用的目标已抛出异常) 解决办法
    C# 使用Renci.SshNet连接SSH远程服务器
    Oracle 查询当前数据库版本信息
    Oracle ORA-12569: TNS:包校验和失败
    Oracle Rollup()函数
    Oracle 字符串补零
    DataGridView 表格排序后颜色丢失
    DataGridView 实现最后一列的宽度自适应
  • 原文地址:https://www.cnblogs.com/shengyang17/p/10491788.html
Copyright © 2011-2022 走看看