java操作hbase基础

public class HbaseDemo {
	private static Configuration conf =null;
	static{
		conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "192.168.0.252");
		conf.set("hbase.zookeeper.property.clientPort", "2181");
	}
	
	//建表
	public static void createTable(String tableName){
		HBaseAdmin admin=null;
		try {
			admin = new HBaseAdmin(conf);
			if(admin.tableExists(tableName)){
				admin.disableTable(tableName);
				admin.deleteTable(tableName);
				System.out.println("delete exixt table");
			}
			HTableDescriptor table = new HTableDescriptor(tableName);
			table.addFamily(new HColumnDescriptor(Bytes.toBytes("info")));
			table.addFamily(new HColumnDescriptor(Bytes.toBytes("other")));
			admin.createTable(table);
			System.out.println("create table success");
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
				try {
					if(admin!=null){
						admin.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
	
	//删除表
	public static void dropTable(String tableName){
		HBaseAdmin admin=null;
		try {
			 admin = new HBaseAdmin(conf);
			 if(admin.tableExists(tableName)){
				 admin.disableTable(tableName);
				 admin.deleteTable(tableName);
				 System.out.println("delete table success");
			 }else{
				 System.out.println("table not exist");
			 }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
	}
	//添加数据
	public static void addData(String tableName,String row){
		HTable table=null;
		try {
			 table = new HTable(conf, tableName);
			 Put put = new Put(Bytes.toBytes(row));
			 put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("java"));
			 put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("20"));
			 put.add(Bytes.toBytes("other"), Bytes.toBytes("other"), Bytes.toBytes("123"));
			 table.put(put);
			 table.flushCommits();
			 System.out.println("add data success");
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(table!=null){
					table.close();
				}			
			} 
			catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	
	}

	//得到单条数据
	public static void getDate(String tableName,String row){
		HTable table = null;
		try {
			table=new HTable(conf, tableName);
			Get get = new Get(Bytes.toBytes(row));
			Result result = table.get(get);
			for(Cell cell:result.rawCells()){
				System.out.print(new String(CellUtil.cloneRow(cell))+",");
				System.out.print(new String(CellUtil.cloneFamily(cell))+",");
				System.out.print(new String(CellUtil.cloneQualifier(cell))+",");
				System.out.print(new String(CellUtil.cloneValue(cell)));
				System.out.println();
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(table!=null){
					table.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	//得到表所有数据
	public static void getAllData(String tableName){
		HTable table = null;
		try {
			table = new HTable(conf, tableName);
			Scan scan = new Scan();
			ResultScanner rs = table.getScanner(scan);
			for(Result r:rs){
				for(Cell cell:r.rawCells()){
					System.out.print(new String(CellUtil.cloneRow(cell))+",");
					System.out.print(new String(CellUtil.cloneFamily(cell))+",");
					System.out.print(new String(CellUtil.cloneQualifier(cell))+",");
					System.out.print(new String(CellUtil.cloneValue(cell)));
					System.out.println();
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	//删除一条数据
	public static void deleteData(String tableName,String row){
		HTable table = null;
		 try {
			table = new HTable(conf, tableName);
			Delete delete = new Delete(Bytes.toBytes(row));
			table.delete(delete);
			System.out.println("delete success");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
//		createTable("hbaseDemo");
//		addData("hbaseDemo", "row1");
//		addData("hbaseDemo", "row2");
//		addData("hbaseDemo", "row3");
//		getAllData("hbaseDemo");
//		getDate("hbaseDemo", "row1");
//		getDate("hbaseDemo", "row2");
//		getDate("hbaseDemo", "row3");
//		deleteData("hbaseDemo", "row1");
//		getAllData("hbaseDemo");
//		dropTable("hbaseDemo");
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值