zoukankan      html  css  js  c++  java
  • 统计表的行数。

    import java.io.IOException;
    
    
    public class E_getTableRowSum {
        public static Configuration configuration;
        public static Connection connection;
        public static Admin admin;
        
        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            getTableRows("student");
        }
        //建立连接
        public static void init(){
            configuration  = HBaseConfiguration.create();
            configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
            try{
                connection = ConnectionFactory.createConnection(configuration);
                admin = connection.getAdmin();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        //关闭连接
        public static void close(){
            try{
                if(admin != null){
                    admin.close();
                }
                if(null != connection){
                    connection.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        /*
         * E_
         */
        public static void getTableRows(String tablename) throws IOException {
            init();
            HTableDescriptor hTableDescriptors[] = admin.listTables();
            System.out.println("tablename:"+tablename);
            Table table = connection.getTable(TableName.valueOf(tablename));
            //创建一个空的Scan实例
            Scan scan1 = new Scan();
            //在行上获取遍历器
            ResultScanner scanner1 = table.getScanner(scan1);
            int count_rows=0;
            for (Result res : scanner1) {
                System.out.println(res);
                Cell[] cells = res.rawCells();
                for(Cell cell:cells){
                    count_rows+=1;
                }
            }
            //关闭释放资源
            System.out.println("count_rows:"+count_rows);
            scanner1.close();
            table.close();
            close();
        }
    
    }
  • 相关阅读:
    &【12】Python 函数
    &【11】Python 流程控制
    &【09】Python 数据类型之dictionary
    &【07】Python 数据类型之元组
    &【08】Python 数据类型之set
    &【06】Python 数据类型之list
    &【05】Python 彻底搞懂分片操作
    &【04】Python 数据结构之序列
    SpringBoot-HelloWorld(三)
    SpringBoot-了解微服务(二)
  • 原文地址:https://www.cnblogs.com/MiraculousB/p/13958123.html
Copyright © 2011-2022 走看看