HBase的eclipse操作Java API

HBase的eclipse操作Java API

 

1、 创建一个表:

 

Console显示结果:

查看操作结果:

 

 

2、 添加一条数据:

 

 

主函数操作:

 

其中:xiaobaozi是表名,cf是表的一个列族,AWM是行健,laobing是一个列,huaju是列的具体值。

 

 

Console显示结果:

 

查看操作结果:

 

3、 查看一行记录:

 对应子方法:

 

 

主方法:


Console显示结果:

 

 

 

4、查看所有记录:

 

对应子方法:


 

主方法:

 

Console显示结果:

 

 

5、删除一行记录:

对应子方法:

 

主方法:

 

Console显示结果:

 

查看结果:

 

 

6、删除表:

对应子方法:

 

主方法:

 

Console显示结果:

 

 

查看结果:

 

 


完整代码:


package hbase;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
public class hbase{
	public static Configuration conf= HBaseConfiguration.create();
	//建表
	public static void creatTable(String tableName, String familys) throws Exception {       
		HBaseAdmin admin = new HBaseAdmin(conf);       
		if (admin.tableExists(tableName)) {       
			System.out.println("table already exists!");       
		} else {       
			@SuppressWarnings("deprecation")
			HTableDescriptor tableDesc = new HTableDescriptor(tableName);           
				tableDesc.addFamily(new HColumnDescriptor(familys));       
			admin.createTable(tableDesc);       
			System.out.println("create table " + tableName + " ok.");       
		}        
	}    	
	
	//添加列记录
	public static void addRecord (String tableName, String rowKey, String familys, String qualifier, String value)       
	            throws Exception{      
		HTable table = new HTable(conf, tableName);
	        try {       
	            Put put = new Put(Bytes.toBytes(rowKey));     
	            put.add(Bytes.toBytes(familys),Bytes.toBytes(qualifier),Bytes.toBytes(value));       
	            table.put(put);       
	            System.out.println("insert recored " + rowKey );       
	        } catch (IOException e) {       
	            e.printStackTrace();       
	        }finally{
	     table.close();
	        }
	    }   
	
	//查看一行记录   
	@SuppressWarnings("deprecation")
	public static void getRecord (String tableName, String rowKey)throws Exception{
		HTable table = new HTable(conf, tableName); 
		Get get=new Get(rowKey.getBytes());
		Result rs=table.get(get);
		for(KeyValue kv:rs.raw()){
			System.out.print(new String(kv.getRow()) + " " );       
			System.out.print(new String(kv.getFamily()) + ":" );     
			System.out.print(new String(kv.getQualifier()) + " " );  
			System.out.print(kv.getTimestamp() + " " );       
			System.out.println(new String(kv.getValue()));  
		}
	}
	
	//查看所有记录
	@SuppressWarnings("deprecation")
	public static void scanAllRecord (String tableName) {       
	    try{       
	         HTable table = new HTable(conf, tableName);       
	         Scan s = new Scan();       
	         ResultScanner ss = table.getScanner(s);       
	         for(Result r:ss){       
	             for(KeyValue kv : r.raw()){       
	                System.out.print(new String(kv.getRow()) + " ");       
	                System.out.print(new String(kv.getFamily()) + ":");       
	                System.out.print(new String(kv.getQualifier()) + " ");       
	                System.out.print(kv.getTimestamp() + " ");       
	                System.out.println(new String(kv.getValue()));       
	             }       
	         }       
	    } catch (IOException e){       
	        e.printStackTrace();       
	    }       
	} 
	
	// 删除整行记录
	@SuppressWarnings("unchecked")
	public static void deleteRecord (String tableName, String rowkey)throws Exception{
	HTable table = new HTable(conf,tableName);
	try {
		@SuppressWarnings("rawtypes")
		List list=new ArrayList();
		Delete del=new Delete(rowkey.getBytes());
		list.add(del);
		table.delete(list);
		System.out.println("delete recored"+rowkey);
		}catch (IOException e){
			e.printStackTrace();
		}finally{
			table.close();
		}
	}
	
	//删除表
	public static void deleteTable(String tableName)throws Exception
	{
		try{
				HBaseAdmin admin = new HBaseAdmin(conf); 
				admin.disableTable(tableName); 
				admin.deleteTable(tableName); 
				System.out.println("delete table " + tableName+"ok!" );  
	        } catch (MasterNotRunningException e) { 
	            e.printStackTrace(); 
	        } catch (ZooKeeperConnectionException e) { 
	            e.printStackTrace(); 
	        } catch (IOException e) { 
	            e.printStackTrace(); 
	        } 
	}	
	
	public static void main(String[] agrs){
		//表名
		String tableName="xiaobaozi";
		//列族
		String familys="cf";
		try{
			//hbase.creatTable(tableName,familys);
			//行键,列族,列,列值
			//hbase.addRecord(tableName, "AWM", familys, "laobing", "huaju");			
			//hbase.addRecord(tableName, "AK", familys, "huoqilin", "688");		
			//hbase.getRecord(tableName, "AWM");
			//hbase.scanAllRecord(tableName);
			hbase.deleteRecord(tableName, "AWM");
			//hbase.deleteTable(tableName);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值