zoukankan      html  css  js  c++  java
  • hbase 实战项目

    首先 根据 hadoop 搭建 + hbase 搭建把 环境弄好

    由于 hbase 依赖于 hdfs ,所以 需要 进入 hadoop --》sbin  下 启动 start-dfs.sh , start-yarn.sh
    然后 进 hbase --> 下 启动 start-hbase.sh  如果 后面 运行失败 报 找不到 zookeeper 八成 你需要进 hbase-bin-> stop-hbase 然后 重run  start-hbase.sh
    

    这里列举下 hbase shell 的常用操作

    #最有用命令 help
    help 'status'
    #建表
    create 'FileTable','fileInfo','saveInfo'
    #列出有哪些表
    list
    #描述表信息
    desc 'FileTable'
    #统计 表
    count 'FileTable'
    #添加列簇
    alter 'FileTable','cf'
    # 删除列簇 ,一定要注意大小写
    alter 'FileTable',{NAME=>'cf',METHOD=>'delete'}
    
    #插入数据
    put 'FileTable','rowkey1','fileInfo:name','file1.txt'
    0 row(s) in 3.9840 seconds
    put 'FileTable','rowkey1','fileInfo:type','txt'
    0 row(s) in 0.0410 seconds
    put 'FileTable','rowkey1','fileInfo:size','1024'
    0 row(s) in 0.1100 seconds
    put 'FileTable','rowkey1','saveInfo:path','/home/pics'
    0 row(s) in 0.1320 seconds
    put 'FileTable' , 'rowkey1','saveInfo:creator','tom'
    0 row(s) in 0.1430 seconds
    #查询表总行数
    hbase(main):016:0> count 'FileTable'
    1 row(s) in 0.8500 seconds
    # get 查询
    get 'FileTable','rowkey1'
    get 'FileTable','rowkey1','fileInfo'
    
    #delete
    
    #deleteall
    
    #scan
    
    
    #删除表之前必须先禁用表Drop the named table. Table must first be disabled:
    disable 'FileTable'
    
    is_enabled
    is_disabled
    
    drop 'FileTable'
    
    

    查询所有列簇

    查询指定列簇

    HBase 连接类

    package com.ghc.hbase.api;
    
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.Connection;
    import org.apache.hadoop.hbase.client.ConnectionFactory;
    import org.apache.hadoop.hbase.client.Table;
    
    import java.io.IOException;
    
    public class HBaseConn {
        private static final HBaseConn INSTANCE = new HBaseConn();
        private static Configuration configuration;
        private static Connection connection;
    
        private HBaseConn(){
            try{
                if(configuration == null){
                    configuration = HBaseConfiguration.create();
                    configuration.set("hbase.zookeeper.quorum","192.168.32.129:2181");
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        private Connection getConnection(){
            if(connection == null || connection.isClosed()){
                try{
                    connection = ConnectionFactory.createConnection(configuration);
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
            return connection;
        }
    
        public static Connection getHBaseConnection(){
            return INSTANCE.getConnection();
        }
    
        public static Table getTable(String tableName) throws IOException{
            return INSTANCE.getConnection().getTable(TableName.valueOf(tableName));
        }
    
        public static void closeConn(){
            if(connection != null){
                try{
                    connection.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
    

    junit 测试一波连接类

    import com.ghc.hbase.api.HBaseConn;
    import org.apache.hadoop.hbase.client.Connection;
    import org.apache.hadoop.hbase.client.Table;
    import org.junit.Test;
    
    import java.io.IOException;
    
    public class HBaseConnTest {
        @Test
        public void getConnTest(){
            Connection conn = HBaseConn.getHBaseConnection();
            System.out.println(conn.isClosed());
            HBaseConn.closeConn();
            System.out.println(conn.isClosed());
        }
        @Test
        public void getTableTest(){
            Table table = null;
            try{table = HBaseConn.getTable("FileTable");
                System.out.println(table.getName());
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
        }
    }
    
    

    hbase 增删操作类

    package com.ghc.hbase.api;
    
    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.filter.FilterList;
    import org.apache.hadoop.hbase.util.Bytes;
    
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;
    
    public class HBaseUtil {
    
        public static Boolean createTable(String tableName,String[] cfs){
            try(HBaseAdmin admin = (HBaseAdmin)HBaseConn.getHBaseConnection().getAdmin()){
                    if(admin.tableExists(tableName)) {
                        return false;
                    }
                    // 不存在 则创建
                    HTableDescriptor  hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
                    Arrays.stream(cfs).forEach(cf->{
                        HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
                        hColumnDescriptor.setMaxVersions(1);
                        hTableDescriptor.addFamily(hColumnDescriptor);
                    });
                    admin.createTable(hTableDescriptor);
                }catch(Exception e){
                    e.printStackTrace();
                }
                return true;
            }
        public static Boolean putRow(String tableName, String rowKey, String cfName,String qualifier,String data){
            try(Table table = HBaseConn.getTable(tableName)){
                Put put = new Put(Bytes.toBytes(rowKey));
                put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(qualifier), Bytes.toBytes(data));
                table.put(put);
            }catch (IOException ioe){
                ioe.printStackTrace();
            }
            return true;
        }
    
        public static Boolean putRows(String tableName,List<Put> puts){
            try(Table table = HBaseConn.getTable(tableName)){
                table.put(puts);
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
            return true;
        }
    
        public static Result getRow(String tableName, String rowKey){
            try(Table table = HBaseConn.getTable(tableName)){
                Get get = new Get(Bytes.toBytes(rowKey));
                return table.get(get);
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
            return null;
        }
    
        public static Result getRow(String tableName, String rowKey, FilterList filterList){
            try(Table table = HBaseConn.getTable(tableName)){
                Get get = new Get(Bytes.toBytes(rowKey));
                get.setFilter(filterList);
                return table.get(get);
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
            return null;
        }
    
        public static ResultScanner getScanner(String tableName){
            try(Table table = HBaseConn.getTable(tableName)){
                Scan scan = new Scan();
                scan.setCaching(1000);
                return table.getScanner(scan);
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
            return null;
        }
    
        public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey){
            try(Table table = HBaseConn.getTable(tableName)){
                Scan  scan = new Scan();
                scan.setStartRow(Bytes.toBytes(startRowKey));
                scan.setStopRow(Bytes.toBytes(endRowKey));
                scan.setCaching(1000);
                return table.getScanner(scan);
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
            return null;
        }
    
        // 使用过滤器
        public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey, FilterList filterList){
            try(Table table = HBaseConn.getTable(tableName)){
                Scan scan = new Scan();
                scan.setStartRow(Bytes.toBytes(startRowKey));
                scan.setStopRow(Bytes.toBytes(endRowKey));
                scan.setCaching(1000);
                scan.setFilter(filterList);
                return table.getScanner(scan);
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
            return null;
        }
    
        // 删除
        public static Boolean deleteRow(String tableName,String rowKey){
            try(Table table = HBaseConn.getTable(tableName)){
                Delete delete = new Delete(Bytes.toBytes(rowKey));
                table.delete(delete);
                return true;
            }catch(IOException ioe){ioe.printStackTrace();}
            return null;
        }
    
        // 删除 列簇 用 admin
        public static Boolean deleteColumnFamily(String tableName,String cfName){
            try(HBaseAdmin admin = (HBaseAdmin)HBaseConn.getHBaseConnection().getAdmin()){
                admin.deleteColumn(tableName,cfName);
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
            return true;
        }
        //删除 某列 用 table
        public static Boolean deleteQualifier(String tableName, String rowKey, String cfName,String qualifier){
            try(Table table = HBaseConn.getTable(tableName)){
                Delete delete = new Delete(Bytes.toBytes(rowKey));
                delete.addColumn(Bytes.toBytes(cfName),Bytes.toBytes(qualifier));
                table.delete(delete);
                return true;
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
            return null;
        }
    }
    
    
    如果有来生,一个人去远行,看不同的风景,感受生命的活力。。。
  • 相关阅读:
    ES6关于Promise的用法
    JS进阶篇--JS数组reduce()方法详解及高级技巧
    JavaScript常用数组操作方法,包含ES6方法
    揭密 Vue 的双向绑定
    JavaScript(E5,6) 正则学习总结学习,可看可不看!
    利用scons构建project
    cuda核函数再调用核函数,多层并行
    使用微信JSSDK实现图片上传
    android 自己定义水平和圆形progressbar 仅仅定义一些style就能够
    [LeetCode] 035. Search Insert Position (Medium) (C++)
  • 原文地址:https://www.cnblogs.com/Frank99/p/9981993.html
Copyright © 2011-2022 走看看