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(); } }