zoukankan      html  css  js  c++  java
  • Hbase的常用shell命令+Java操作

    1 Hbase常用shell命令

    1.1 进入命令行

    hbase shell

    1.2 常用命令

    help                           

    查看命令的使用描述 help '命令名'
    whoami 身份(root、user) whoami
    version 返回hbase版本信息 version
    status 返回hbase集群的状态信息 status
    table_help 查看如何操作表 table_help
    create 创建表 create '表名', '列族名1', '列族名2', '列族名N'
    alter 修改列族 添加列族:alter '表名', NAME=>'列族名'
    删除列族:alter '表名', {NAME=> '列族名', METHOD=> 'delete'}
    describe 显示表相关的详细信息 describe '表名'
    list 列出hbase中存在的所有表 list
    exists 测试表是否存在 exists '表名'
    put 添加或修改的表的值 put '表名', '行键', '列族名', '列值'
    put '表名', '行键', '列族名:列名', '列值'
    scan 通过对表的扫描来获取对用的值 scan '表名'
    扫描某个列族:scan '表名',{COLUMN=>'列族名',FORMATTER =>'toString'}
    扫描某个列族的某个列:scan '表名', {COLUMN=>'列族名:列名'}
    查询同一个列族的多个列: scan '表名', {COLUMNS => [ '列族名1:列名1', '列族名1:列名2' …]}
    get 获取行或单元(cell)的值 get '表名', '行键'
    get '表名', '行键', '列族名'
    count 统计表中行的数量 count '表名'
    incr 增加指定表行或列的值 incr '表名', '行键', '列族:列名', 步长值
    get_counter 获取计数器 get_counter '表名', '行键', '列族:列名'
    delete 删除指定对象的值(可以为表,行,列对应的值,另外也可以指定时间戳的值) 删除列族的某个列: delete 表名', '行键', '列族名:列名'
    deleteall 删除指定行的所有元素值 deleteall '表名', '行键'
    truncate 重新创建指定表(清空表) truncate '表名'
    enable 使表有效 enable '表名'
    is_enabled 是否启用 is_enabled '表名'
    disable 使表无效(删除表之前先禁用) disable '表名'
    is_disabled 是否无效 is_disabled '表名'
    drop 删除表 drop的表必须是disable的
    disable '表名'
    drop '表名'
    shutdown 关闭hbase集群(与exit不同)  
    tools 列出hbase所支持的工具  
    exit 退出hbase shell

    2.1 源代码2 Hbase的API操作

    package test;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.Cell;
    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.List;
    
    /**
     * @author mooojl
     * @Date 2020-10-18-8:51
     */
    public class Test {
        // connection是一个重量级的对象,不能频繁去创建connection
        //connection是线程安全的
        private static Connection connection;
        private static Admin admin;
        public static void main(String[] args) throws IOException {
            getConn();
            //创建表:stusent,列蔟:"Student","Course","SC"
            //createTable("stusent",new String[]{"Student","Course","SC"});
            //添加行
            //①Student
            //addRow("student","001","Student",new String[]{"S_No","S_Name","S_Sex","S_Age"},new String[]{"2015001","Zhangsan","male","23"});
            //addRow("student","002","Student",new String[]{"S_No","S_Name","S_Sex","S_Age"},new String[]{"2015003","Mary","female","22"});
            //②Course
            //addRow("student","001","Course",new String[]{"C_No","C_Name","C_Credit"},new String[]{"123001","Math","2.0"});
            //addRow("student","002","Course",new String[]{"C_No","C_Name","C_Credit"},new String[]{"123003","English","3.0"});
            //③SC
            //addRow("student","001","SC",new String[]{"SC_Sno","SC_Cno","SC_Score"},new String[]{"2015001","123001","86"});
            //addRow("student","002","SC",new String[]{"SC_Sno","SC_Cno","SC_Score"},new String[]{"2015003","123003","69"});
    
            //删除
            //deleteRow("student","001","Student","S_No");
    
            //get获取数据
            //getData("student","002","Student","S_No");
            
            //修改数据
            modifyData("student", "001", "SC","SC_Score", "100");
            closeConn();
        }
        public static void getConn() throws IOException {
            //1、使用HBaseConfiguration.create()创建Hbase配置
            Configuration configuration = HBaseConfiguration.create();
            //2、 ConnectionFactory.createConnection()创建Hbase连接
            connection = ConnectionFactory.createConnection(configuration);
            System.out.println(connection);
            //3、要创建表,需要基于Hbase连接获取admin管理对象
            admin = connection.getAdmin();
        }
        public static void closeConn() throws IOException {
            //4、使用admin.close. connection.close关闭连接
            if(admin != null){
                admin.close();
            }
            if(connection!=null){
                connection.close();
            }
        }
        /**
         * 建表。HBase的表中会有一个系统默认的属性作为主键,主键无需自行创建,默认为put命令操作中表名后第一个数据,因此此处无需创建id列
         * @param mytableName 表名
         * @param colFamily 列族名
         */
        public static void createTable(String mytableName,String[] colFamily) throws IOException {
            TableName tableName = TableName.valueOf(mytableName);
            //1、判断表是否存在
            if (admin.tableExists(tableName)){
                System.out.println("表存在");
                return;
            }else {
                //构建表
                //2、使用TableDescriptorBuilder.newBuilder构建表描述构造器
                //TableDescriptorBuilder:表描述器,描述这个表有几个列蔟、其他属性
                TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
                //3、使用ColumnFamilyDescriptorBuilder.newBuilder()构建列蔟描述构造器
                //创建列蔟也需要有列蔟的描述器,需要用一个构建器来构建ColumnFamilyDescriptor
                //经常会使用到一个工具类:Bytes (hbase包下的Bytes工具类)
                //这个工具类可以将字符串、long.double类型转换成byte[]数组
                //也可以将byte[]数组转换为指定类型
                for (String str : colFamily) {
                    ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str));
                    //4、构建列蔟描述,构建表描述
                    ColumnFamilyDescriptor cfdes = columnFamilyDescriptorBuilder.build();
                    //建立表和列蔟的关联
                    tableDescriptorBuilder.setColumnFamily(cfdes);
                }
                TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
                //5、创建表
                admin.createTable(tableDescriptor);
            }
            closeConn();
        }
        /**
         * 向某一行的某一列插入数据
         * @param tableName 表名
         * @param rowKey 行键
         * @param colFamily 列族名
         * @param col 列名(如果其列族下没有子列,此参数可为空)
         * @param val 值
         */
        public static void addRow(String tableName, String rowKey,String colFamily,String[] col,String[] val) throws IOException {
            TableName tablename = TableName.valueOf(tableName);
            //1 使用Hbase连接获取Htable
            Table table = connection.getTable(tablename);
            //2 构建ROWKEY、列蔟名、列名
            //3 构建Put对象(对应put命令)
            Put put = new Put(Bytes.toBytes(rowKey));
            for (int i=0;i<col.length;i++){
                //4 添加列、值
                put.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col[i]),Bytes.toBytes(val[i]));
            }
            //5 使用Htable表对象执行put操作
            table.put(put);
            //6 关闭Htable表对象
            //HTable是一个轻量级的对象,可以经常创建
            //HTable它是一个非线程安全的API
            table.close();
        }
        /**
         * 删除数据
         * @param tableName 表名
         * @param rowKey 行键
         * @param colFamily 列族名
         * @param col 列名
         *
         */
        public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException {
            TableName tablename = TableName.valueOf(tableName);
            Table table = connection.getTable(tablename);
            Delete delete = new Delete(Bytes.toBytes(rowKey));
            //Delete delete = new Delete(rowKey.getBytes());
            //删除指定列族的所有数据
            delete.addFamily(colFamily.getBytes());
            //删除指定列的数据
            //delete.addColumn(colFamily.getBytes(), col.getBytes());
            table.delete(delete);
            table.close();
        }
        /**
         * 删除指定表
         * @param tableName 表名
         */
        public static void deleteTable(String tableName) throws IOException {
            TableName tablename = TableName.valueOf(tableName);
            if (admin.tableExists(tablename)){
                //2、如果存在,则禁用表
                admin.disableTable(tablename);
                //3、删除
                admin.deleteTable(tablename);
            }
        }
    
        /**
         * 根据行键rowkey查找数据
         * @param tableName 表名
         * @param rowKey    行键
         * @param colFamily 列族名
         * @param col       列名
         */
        public static void getData(String tableName, String rowKey, String colFamily, String col) throws IOException {
            TableName tablename = TableName.valueOf(tableName);
            // 1.获取HTable
            Table table = connection.getTable(tablename);
            //2.使用rowkey构建Get对象
            Get get = new Get(Bytes.toBytes(rowKey));
            //get.addFamily(Bytes.toBytes(colFamily));
            get.addColumn(colFamily.getBytes(),col.getBytes());
            //3.执行get请求
            Result result = table.get(get);
            //4.获取所有单元格
            List<Cell> cellList = result.listCells();
            byte[] rowkey = result.getRow();
            System.out.println("rowkey"+Bytes.toString(rowkey));;
            //6.迭代单元格列表
            for (Cell cell : cellList) {
                //将字节数组转换为字符串
                //获取列蔟名称
                String cf = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
                //获取列名称
                String columnName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
                //获取值
                String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                System.out.println(cf +":" +columnName+"->"+value);
            }
            table.close();
        }
    
        //修改表
        public static void modifyData(String tableName,String rowKey,String colFamily,String column,String val) throws IOException {
            TableName tablename = TableName.valueOf(tableName);
            Table table = connection.getTable(tablename);
            long ts = 0;
            Put put = new Put(Bytes.toBytes(rowKey));
            Scan scan = new Scan();
            ResultScanner resultScanner = table.getScanner(scan);
            for (Result r : resultScanner) {
                for (Cell cell : r.getColumnCells(rowKey.getBytes(), column.getBytes())) {
                    ts = cell.getTimestamp();
                }
            }
            put.addColumn(rowKey.getBytes(),column.getBytes(),ts,val.getBytes());
            table.put(put);
            table.close();
        }
    }
  • 相关阅读:
    The Quad
    将OrCAD Capture CIS的设计文件(.dsn)导入到PADS Logic VX.2.3
    OrCAD Capture CIS 16.6 将版本16.6的设计文件另存为版本16.2的设计文件
    Eclipse IDE 添加jar包到Java工程中
    PADS Logic VX.2.3 修改软件界面语言
    切换Allegro PCB Editor
    Allegro PCB Design GXL (legacy) 将brd文件另存为低版本文件
    Allegro PCB Design GXL (legacy) 设置自动保存brd文件
    Could not create an acl object: Role '16'
    windows 下apache开启FastCGI
  • 原文地址:https://www.cnblogs.com/MoooJL/p/13837786.html
Copyright © 2011-2022 走看看