HBase编程实战

  1. 编程实现以下指定功能,并用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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值