• 统计表的行数。


    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();
        }
    
    }
  • 相关阅读:
    洛谷 P5595 【XR-4】歌唱比赛
    洛谷 P5594 【XR-4】模拟赛
    洛谷 AT1350 深さ優先探索
    洛谷 P1449 后缀表达式
    VIJOS-P1064 迎春舞会之数字舞蹈
    洛谷 P3998 [SHOI2013]发微博
    CF306C White, Black and White Again
    CF261E Maxim and Calculator
    JLOI 2014 松鼠的新家
    HNOI 2009 有趣的数列
  • 原文地址:https://www.cnblogs.com/MiraculousB/p/13958123.html
Copyright © 2020-2023  润新知