Hbase是够建在HDFS之上的半结构化的分布式存储系统,

Hbase是够建在HDFS之上的半结构化的分布式存储系统,具有HDFS的所有优点,同时也有自己的亮点,支持更快速的随机读写以及更灵活的Scan操作,而在HDFS上这一点我们是远远做不到的,因为HDFS仅支持Append追加操作,而且也不具备随机读写一条数据的功能,实际上HDFS扫描的范围按Block来算的,所以从某个角度来言,Hbase利用Schemal的方式做到了这一点。

一般情况下,我们使用Linux的shell命令,就可以非常轻松的操作Hbase,例如一些建表,建列簇,插值,显示所有表,统计数量等等,但有时为了提高灵活性,我们也需要使用编程语言来操作Hbase,当然Hbase通过Thrift接口提供了对大多数主流编程语言的支持,例如C++,PHP,Python,Ruby等等,那么本篇,散仙给出的例子是基于Java原生的API操作Hbase,相比其他的一些编程语言,使用Java操作Hbase,会更加高效一些,因为Hbase本身就是使用Java语言编写的。

下面,散仙给出源码,以供参考:

Java代码 复制代码  收藏代码
  1. package com.hbase;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.apache.hadoop.conf.Configuration;  
  7. import org.apache.hadoop.hbase.HBaseConfiguration;  
  8. import org.apache.hadoop.hbase.HColumnDescriptor;  
  9. import org.apache.hadoop.hbase.HTableDescriptor;  
  10. import org.apache.hadoop.hbase.KeyValue;  
  11. import org.apache.hadoop.hbase.client.Delete;  
  12. import org.apache.hadoop.hbase.client.Get;  
  13. import org.apache.hadoop.hbase.client.HBaseAdmin;  
  14. import org.apache.hadoop.hbase.client.HTable;  
  15. import org.apache.hadoop.hbase.client.Put;  
  16. import org.apache.hadoop.hbase.client.Result;  
  17. import org.apache.hadoop.hbase.client.ResultScanner;  
  18. import org.apache.hadoop.hbase.client.Scan;  
  19. import org.apache.hadoop.hbase.util.Bytes;  
  20.   
  21. /** 
  22.  * @author 三劫散仙 
  23.  *  
  24.  * **/  
  25. public class Test {  
  26.       
  27.     static Configuration conf=null;  
  28.     static{  
  29.           
  30.           conf=HBaseConfiguration.create();//hbase的配置信息  
  31.           conf.set("hbase.zookeeper.quorum""10.2.143.5");  //zookeeper的地址  
  32.           
  33.     }  
  34.       
  35.     public static void main(String[] args)throws Exception {  
  36.           
  37.         Test t=new Test();  
  38.         //t.createTable("temp", new String[]{"name","age"});  
  39.      //t.insertRow("temp", "2", "age", "myage", "100");  
  40.     // t.getOneDataByRowKey("temp", "2");  
  41.         t.showAll("temp");  
  42.        
  43.     }  
  44.       
  45.     /*** 
  46.      * 创建一张表 
  47.      * 并指定列簇 
  48.      * */  
  49.     public void createTable(String tableName,String cols[])throws Exception{  
  50.      HBaseAdmin admin=new HBaseAdmin(conf);//客户端管理工具类  
  51.     if(admin.tableExists(tableName)){  
  52.         System.out.println("此表已经存在.......");  
  53.     }else{  
  54.         HTableDescriptor table=new HTableDescriptor(tableName);  
  55.         for(String c:cols){  
  56.             HColumnDescriptor col=new HColumnDescriptor(c);//列簇名  
  57.             table.addFamily(col);//添加到此表中  
  58.         }  
  59.           
  60.      admin.createTable(table);//创建一个表  
  61.      admin.close();  
  62.      System.out.println("创建表成功!");  
  63.     }  
  64.     }  
  65.       
  66.     /** 
  67.      * 添加数据, 
  68.      * 建议使用批量添加 
  69.      * @param tableName 表名 
  70.      * @param row  行号 
  71.      * @param columnFamily 列簇 
  72.      * @param column   列 
  73.      * @param value   具体的值 
  74.      *  
  75.      * **/  
  76.     public  void insertRow(String tableName, String row,    
  77.             String columnFamily, String column, String value) throws Exception {    
  78.         HTable table = new HTable(conf, tableName);    
  79.         Put put = new Put(Bytes.toBytes(row));    
  80.         // 参数出分别:列族、列、值    
  81.         put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),    
  82.                 Bytes.toBytes(value));   
  83.          
  84.         table.put(put);    
  85.         table.close();//关闭  
  86.         System.out.println("插入一条数据成功!");  
  87.     }      
  88.       
  89.     /** 
  90.      * 删除一条数据 
  91.      * @param tableName 表名 
  92.      * @param row  rowkey 
  93.      * **/  
  94.     public void deleteByRow(String tableName,String rowkey)throws Exception{  
  95.         HTable h=new HTable(conf, tableName);  
  96.         Delete d=new Delete(Bytes.toBytes(rowkey));  
  97.         h.delete(d);//删除一条数据  
  98.         h.close();  
  99.     }  
  100.       
  101.     /** 
  102.      * 删除多条数据 
  103.      * @param tableName 表名 
  104.      * @param row  rowkey 
  105.      * **/  
  106.     public void deleteByRow(String tableName,String rowkey[])throws Exception{  
  107.         HTable h=new HTable(conf, tableName);  
  108.        
  109.         List<Delete> list=new ArrayList<Delete>();  
  110.         for(String k:rowkey){  
  111.             Delete d=new Delete(Bytes.toBytes(k));  
  112.             list.add(d);  
  113.         }  
  114.         h.delete(list);//删除  
  115.         h.close();//释放资源  
  116.     }  
  117.       
  118.     /** 
  119.      * 得到一条数据 
  120.      *  
  121.      * @param tableName 表名 
  122.      * @param rowkey 行号 
  123.      * ***/  
  124.     public void getOneDataByRowKey(String tableName,String rowkey)throws Exception{  
  125.         HTable h=new HTable(conf, tableName);  
  126.           
  127.         Get g=new Get(Bytes.toBytes(rowkey));  
  128.         Result r=h.get(g);  
  129.         for(KeyValue k:r.raw()){  
  130.               
  131.             System.out.println("行号:  "+Bytes.toStringBinary(k.getRow()));  
  132.             System.out.println("时间戳:  "+k.getTimestamp());  
  133.             System.out.println("列簇:  "+Bytes.toStringBinary(k.getFamily()));  
  134.             System.out.println("列:  "+Bytes.toStringBinary(k.getQualifier()));  
  135.             //if(Bytes.toStringBinary(k.getQualifier()).equals("myage")){  
  136.             //  System.out.println("值:  "+Bytes.toInt(k.getValue()));  
  137.             //}else{  
  138.             String ss=  Bytes.toString(k.getValue());  
  139.             System.out.println("值:  "+ss);  
  140.             //}  
  141.               
  142.                
  143.               
  144.         }  
  145.         h.close();  
  146.           
  147.           
  148.     }  
  149.       
  150.     /** 
  151.      * 扫描所有数据或特定数据 
  152.      * @param tableName 
  153.      * **/  
  154.     public void showAll(String tableName)throws Exception{  
  155.           
  156. HTable h=new HTable(conf, tableName);  
  157.           
  158.          Scan scan=new Scan();  
  159.          //扫描特定区间  
  160.          //Scan scan=new Scan(Bytes.toBytes("开始行号"),Bytes.toBytes("结束行号"));  
  161.          ResultScanner scanner=h.getScanner(scan);  
  162.          for(Result r:scanner){  
  163.              System.out.println("==================================");  
  164.         for(KeyValue k:r.raw()){  
  165.               
  166.             System.out.println("行号:  "+Bytes.toStringBinary(k.getRow()));  
  167.             System.out.println("时间戳:  "+k.getTimestamp());  
  168.             System.out.println("列簇:  "+Bytes.toStringBinary(k.getFamily()));  
  169.             System.out.println("列:  "+Bytes.toStringBinary(k.getQualifier()));  
  170.             //if(Bytes.toStringBinary(k.getQualifier()).equals("myage")){  
  171.             //  System.out.println("值:  "+Bytes.toInt(k.getValue()));  
  172.             //}else{  
  173.             String ss=  Bytes.toString(k.getValue());  
  174.             System.out.println("值:  "+ss);  
  175.             //}  
  176.               
  177.                
  178.               
  179.         }  
  180.          }  
  181.         h.close();  
  182.           
  183.     }  
  184.   
  185. }  
package com.hbase;

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.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;

/**
 * @author 三劫散仙
 * 
 * **/
public class Test {
	
	static Configuration conf=null;
	static{
		
		  conf=HBaseConfiguration.create();//hbase的配置信息
		  conf.set("hbase.zookeeper.quorum", "10.2.143.5");  //zookeeper的地址
		
	}
	
	public static void main(String[] args)throws Exception {
		
		Test t=new Test();
		//t.createTable("temp", new String[]{"name","age"});
	 //t.insertRow("temp", "2", "age", "myage", "100");
	// t.getOneDataByRowKey("temp", "2");
		t.showAll("temp");
	 
	}
	
	/***
	 * 创建一张表
	 * 并指定列簇
	 * */
	public void createTable(String tableName,String cols[])throws Exception{
	 HBaseAdmin admin=new HBaseAdmin(conf);//客户端管理工具类
	if(admin.tableExists(tableName)){
		System.out.println("此表已经存在.......");
	}else{
		HTableDescriptor table=new HTableDescriptor(tableName);
		for(String c:cols){
			HColumnDescriptor col=new HColumnDescriptor(c);//列簇名
			table.addFamily(col);//添加到此表中
		}
		
	 admin.createTable(table);//创建一个表
	 admin.close();
	 System.out.println("创建表成功!");
	}
	}
	
	/**
	 * 添加数据,
	 * 建议使用批量添加
	 * @param tableName 表名
	 * @param row  行号
	 * @param columnFamily 列簇
	 * @param column   列
	 * @param value   具体的值
	 * 
	 * **/
    public  void insertRow(String tableName, String row,  
            String columnFamily, String column, String value) throws Exception {  
        HTable table = new HTable(conf, tableName);  
        Put put = new Put(Bytes.toBytes(row));  
        // 参数出分别:列族、列、值  
        put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),  
                Bytes.toBytes(value)); 
       
        table.put(put);  
        table.close();//关闭
        System.out.println("插入一条数据成功!");
    }    
    
    /**
     * 删除一条数据
     * @param tableName 表名
     * @param row  rowkey
     * **/
    public void deleteByRow(String tableName,String rowkey)throws Exception{
    	HTable h=new HTable(conf, tableName);
    	Delete d=new Delete(Bytes.toBytes(rowkey));
    	h.delete(d);//删除一条数据
    	h.close();
    }
    
    /**
     * 删除多条数据
     * @param tableName 表名
     * @param row  rowkey
     * **/
    public void deleteByRow(String tableName,String rowkey[])throws Exception{
    	HTable h=new HTable(conf, tableName);
     
    	List<Delete> list=new ArrayList<Delete>();
    	for(String k:rowkey){
    		Delete d=new Delete(Bytes.toBytes(k));
    		list.add(d);
    	}
    	h.delete(list);//删除
    	h.close();//释放资源
    }
    
    /**
     * 得到一条数据
     * 
     * @param tableName 表名
     * @param rowkey 行号
     * ***/
    public void getOneDataByRowKey(String tableName,String rowkey)throws Exception{
    	HTable h=new HTable(conf, tableName);
    	
    	Get g=new Get(Bytes.toBytes(rowkey));
    	Result r=h.get(g);
    	for(KeyValue k:r.raw()){
    		
    		System.out.println("行号:  "+Bytes.toStringBinary(k.getRow()));
    		System.out.println("时间戳:  "+k.getTimestamp());
    		System.out.println("列簇:  "+Bytes.toStringBinary(k.getFamily()));
    		System.out.println("列:  "+Bytes.toStringBinary(k.getQualifier()));
    		//if(Bytes.toStringBinary(k.getQualifier()).equals("myage")){
    		//	System.out.println("值:  "+Bytes.toInt(k.getValue()));
    		//}else{
    		String ss=	Bytes.toString(k.getValue());
    		System.out.println("值:  "+ss);
    		//}
    		
    		 
    		
    	}
    	h.close();
    	
    	
    }
    
    /**
     * 扫描所有数据或特定数据
     * @param tableName
     * **/
    public void showAll(String tableName)throws Exception{
    	
HTable h=new HTable(conf, tableName);
    	
    	 Scan scan=new Scan();
    	 //扫描特定区间
    	 //Scan scan=new Scan(Bytes.toBytes("开始行号"),Bytes.toBytes("结束行号"));
    	 ResultScanner scanner=h.getScanner(scan);
    	 for(Result r:scanner){
    		 System.out.println("==================================");
    	for(KeyValue k:r.raw()){
    		
    		System.out.println("行号:  "+Bytes.toStringBinary(k.getRow()));
    		System.out.println("时间戳:  "+k.getTimestamp());
    		System.out.println("列簇:  "+Bytes.toStringBinary(k.getFamily()));
    		System.out.println("列:  "+Bytes.toStringBinary(k.getQualifier()));
    		//if(Bytes.toStringBinary(k.getQualifier()).equals("myage")){
    		//	System.out.println("值:  "+Bytes.toInt(k.getValue()));
    		//}else{
    		String ss=	Bytes.toString(k.getValue());
    		System.out.println("值:  "+ss);
    		//}
    		
    		 
    		
    	}
    	 }
    	h.close();
    	
    }

}


显示所有数据的打印输出如下:

Java代码 复制代码  收藏代码
  1. ==================================  
  2. 行号:  1  
  3. 时间戳:  1385597699287  
  4. 列簇:  name  
  5. 列:  myname  
  6. 值:  秦东亮  
  7. ==================================  
  8. 行号:  2  
  9. 时间戳:  1385598393306  
  10. 列簇:  age  
  11. 列:  myage  
  12. 值:  100  
  13. 行号:  2  
  14. 时间戳:  1385597723900  
  15. 列簇:  name  
  16. 列:  myname  
  17. 值:  三劫散仙  
==================================
行号:  1
时间戳:  1385597699287
列簇:  name
列:  myname
值:  秦东亮
==================================
行号:  2
时间戳:  1385598393306
列簇:  age
列:  myage
值:  100
行号:  2
时间戳:  1385597723900
列簇:  name
列:  myname
值:  三劫散仙




由此,可以看出Hbase的对外的API提供接口,是非常简单易用的

猜你喜欢

转载自weitao1026.iteye.com/blog/2268095