- 编程实现以下指定功能,并用Hadoop提供的HBase Shell命令完成相同任务:
(1) 列出HBase所有的表的相关信息,例如表名;
public static void main(String[] args) throws IOException{
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
Connection connection = ConnectionFactory.createConnection(configuration);
Admin admin = connection.getAdmin();
HTableDescriptor[] tables = admin.listTables();
for(HTableDescriptor s:tables){
System.out.println(s.getNameAsString());
}
if(admin != null)
admin.close();
if(connection!=null)
connection.close();
}
(2) 在终端打印出指定的表的所有记录数据;
public static void main(String[] args) throws IOException{
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
Connection connection = ConnectionFactory.createConnection(configuration);
Admin admin = connection.getAdmin();
TableName table = TableName.valueOf("student");
if(!admin.tableExists(table)){
System.out.println("table is not exists");
}else{
Table t = connection.getTable(table);
Scan scan = new Scan();
ResultScanner rs = t.getScanner(scan);
for(Result s :rs){
for(Cell cell:s.rawCells()){
System.out.print("row name:"+new String(CellUtil.cloneRow(cell)));
System.out.print("column zoo :" +new String(CellUtil.cloneFamily(cell)));
System.out.print("column :"+new String(CellUtil.cloneQualifier(cell)));
System.out.print("value :"+new String(CellUtil.cloneValue(cell)));
System.out.print("time :"+cell.getTimestamp());
System.out.println();
}
}
}
if(admin != null)
admin.close();
if(connection!=null)
connection.close();
}
(3) 向已经创建好的表添加和删除指定的列族或列;
public static void main(String[] args) throws IOException{
Configuration configuration = new Configuration();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
Connection connection = ConnectionFactory.createConnection(configuration);
TableName tablename = TableName.valueOf("student");
Table table = connection.getTable(tablename);
//add data
Put put = new Put("2018004".getBytes());
put.addColumn("information".getBytes(), "math".getBytes(), "90".getBytes());
table.put(put);
System.out.println("sucessfully input");
//delete data
Delete delete = new Delete("20