HBase数据操作

作者: 大龄程序员在帝都 | 来源:发表于2017-05-16 15:37 被阅读244次

    1、 HBase shell操作

    在命令行中执行hbase shell就进入了hbase shell操作模式,可以通过命令进行一些操作

    创建表

    #创建一张表log 包含 log_id log_content 和 remark三个列簇
    create 'log' , 'log_id' , 'log_content' ,'remark'
    

    查看表的描述信息

    describe 'log'
    
    image.png

    列出所有的表

    list
    

    删除表

    #首先对表进行禁用
    disable 'log'
    #对表进行删除
    drop 'log'
    

    添加记录

    put 'log' , 'log001' , 'log_content:add','generate a new record' 
    put 'log' , 'log001' , 'log_content:update','update a record' 
    put 'log' , 'log001' , 'log_content:delete','delete a record' 
    
    put 'log' , 'log002' , 'log_content:add','add a record' 
    put 'log' , 'log002' , 'log_content:add','add a record' 
    put 'log' , 'log002' , 'log_content:delete','delete a record' 
    
    put 'log' , 'log001' , 'remark:recordId','200053' 
    put 'log' , 'log002' , 'remark:recordId','633948'
    
    

    查看记录

    scan 'log'
    
    image.png

    获取其中一条记录

    get 'log','log001'
    
    image.png

    获取指定Id的一个列族

    get 'log' ,'log001','remark'
    
    image.png

    获取指定Id的一个列族的一个指定列

    get 'log','log001','log_content:delete'
    
    image.png

    更新记录

    put 'log','log001','log_content:add','add a record for update'
    

    如下更新以后为:

    image.png

    删除记录
    删除一条记录

    delete 'log','log001','log_content:add'
    
    如图删除以后没有数据了

    删除整行记录

    deleteall 'log','log001'
    

    如图删除以后没有了对应的数据

    image.png

    统计行数

    count 'log'
    

    清空表数据

    truncate 'log'
    

    通过Java API操作HBase

    具体可以参考Hbase的API
    引入的pom.xml

    <dependencies>
    
            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-client</artifactId>
                <version>1.3.0</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase</artifactId>
                <version>1.2.5</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-hdfs</artifactId>
                <version>2.7.3</version>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
    
        </dependencies>
    

    Java 操作HBase

    package demo;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.HColumnDescriptor;
    import org.apache.hadoop.hbase.HTableDescriptor;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;
    
    /**
     * Description
     * Created by qiupeng.wang on 17/5/16.
     */
    public class HBaseDemo {
        private static Configuration  conf = HBaseConfiguration.create();
        static {
            //加载配置和hbase配置保持一致
            conf.set("hbase.rootdir","hdfs://106.xxx.xxx.213:9000/hbase");
            conf.set("hbase.zookeeper.quorum","106.xxx.xxx.213,106.xxx.xxx.203,106.xxx.xxx.162");
        }
    
        public static void main(String[] args) throws Exception {
            //createTable("users","username");
            //deleteTable("users");
            //insertRecord("users","001","username","firstName","wang");
            //insertRecord("users","001","username","lastName","qpeng");
            //getRecord("users","001");
            scan("users");
        }
    
    
        /**
         * 创建表
         * @param tableNmae
         * @param columnFamily
         * @throws Exception
         */
        public static void createTable(String tableNmae,String columnFamily) throws  Exception{
            Connection connection = ConnectionFactory.createConnection(conf);
            Admin admin = connection.getAdmin();
            TableName tableNameObject = TableName.valueOf(tableNmae);
    
            if (admin.tableExists(tableNameObject)) {
                System.out.println("Table exists");
                System.exit(0);
              }else{
                HTableDescriptor tableDescriptor = new HTableDescriptor(tableNameObject);
                tableDescriptor.addFamily(new HColumnDescriptor(columnFamily));
                admin.createTable(tableDescriptor);
                System.out.println("create table success");
             }
              admin.close();
              connection.close();
            }
    
        /**
         * 删除表
         * @param tableName
         * @throws Exception
         */
        public static void deleteTable(String tableName)throws  Exception{
            Connection connection = ConnectionFactory.createConnection(conf);
            Admin  admin = connection.getAdmin();
            TableName tableObject = TableName.valueOf(tableName);
            admin.disableTable(tableObject);
            admin.deleteTable(tableObject);
            System.out.println("delete " + tableName + "success ");
        }
    
        /**
         * 插入记录
         * @param tableName
         * @param rowKey
         * @param columnFamily
         * @param column
         * @param value
         * @throws Exception
         */
        public static void insertRecord(String tableName,String rowKey,String columnFamily,String column,String value) throws Exception{
            Connection connection  = ConnectionFactory.createConnection(conf);
            Table table = connection.getTable(TableName.valueOf(tableName));
            Put put = new Put(Bytes.toBytes(rowKey));
            put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value));
            table.put(put);
            table.close();
            connection.close();
            System.out.println("insert success");
        }
    
        /**
         * 获取记录信息
         * @param tableName
         * @param rowKey
         * @throws Exception
         */
        public static void getRecord(String tableName,String rowKey) throws  Exception{
            Connection connection  = ConnectionFactory.createConnection(conf);
            Table table = connection.getTable(TableName.valueOf(tableName));
            Get  get = new Get(Bytes.toBytes(rowKey));
            Result result = table.get(get);
            System.out.println("get result : " + result);
        }
    
    
        public static void  scan(String tableName) throws  Exception{
            Connection connection  = ConnectionFactory.createConnection(conf);
            Table table = connection.getTable(TableName.valueOf(tableName));
            Scan  scan = new Scan();
            ResultScanner  scanner = table.getScanner(scan);
            for (Result result : scanner){
                System.out.println("scan result :  " + result);
            }
        }
    
    }
    
    
    

    hbase操作
    参考引入包
    HBase官方文档盛大数据仓库团队-颜开译

    相关文章

      网友评论

        本文标题:HBase数据操作

        本文链接:https://www.haomeiwen.com/subject/trdlxxtx.html