Hbase新的api的增删改查的工具类

写了个Hbase新的api的增删改查的工具类

Java代码
  1. package com.dhgate.hbase.test;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  1. import org.apache.hadoop.conf.Configuration;  
  2. import org.apache.hadoop.hbase.Cell;  
  3. import org.apache.hadoop.hbase.CellUtil;  
  4. import org.apache.hadoop.hbase.HBaseConfiguration;  
  5. import org.apache.hadoop.hbase.HColumnDescriptor;  
  6. import org.apache.hadoop.hbase.HTableDescriptor;  
  7. import org.apache.hadoop.hbase.TableName;  
  8. import org.apache.hadoop.hbase.client.Delete;  
  9. import org.apache.hadoop.hbase.client.Get;  
  10. import org.apache.hadoop.hbase.client.HBaseAdmin;  
  11. import org.apache.hadoop.hbase.client.HTable;  
  12. import org.apache.hadoop.hbase.client.Put;  
  13. import org.apache.hadoop.hbase.client.Result;  
  14. import org.apache.hadoop.hbase.client.ResultScanner;  
  15. import org.apache.hadoop.hbase.client.Scan;  
  16. import org.apache.hadoop.hbase.filter.PageFilter;  
  17. import org.apache.hadoop.hbase.filter.PrefixFilter;  
  18. import org.apache.hadoop.hbase.util.Bytes;  
  19.   
  20. /** 
  21.  * 基于新的API 
  22.  * Hbase0.96版本 
  23.  * 写的工具类 
  24.  *  
  25.  * @author qindongliang 
  26.  * 大数据技术交流群: 376932160 
  27.  *  
  28.  * **/  
  29. public class HbaseCommons {  
  30.   
  31.       
  32.     static Configuration conf=HBaseConfiguration.create();  
  33.     static String tableName="";  
  34.    
  35.       
  36.       
  37.       
  38.     public static void main(String[] args)throws Exception {  
  39.           
  40.         //String tableName="test";  
  41.         //createTable(tableName, null);  
  42.           
  43.     }  
  44.       
  45.       
  46.     /** 
  47.      * 批量添加数据 
  48.      * @param tableName 标名字 
  49.      * @param rows rowkey行健的集合 
  50.      * 本方法仅作示例,其他的内容需要看自己义务改变 
  51.      *  
  52.      * **/  
  53.     public static void insertList(String tableName,String rows[])throws Exception{  
  54.         HTable table=new HTable(conf, tableName);  
  55.         List<Put> list=new ArrayList<Put>();  
  56.         for(String r:rows){  
  57.             Put p=new Put(Bytes.toBytes(r));  
  58.            //此处示例添加其他信息  
  59.             //p.add(Bytes.toBytes("family"),Bytes.toBytes("column"), 1000, Bytes.toBytes("value"));  
  60.             list.add(p);  
  61.         }  
  62.         table.put(list);//批量添加  
  63.         table.close();//释放资源  
  64.     }  
  65.       
  66.     /** 
  67.      * 创建一个表 
  68.      * @param tableName 表名字 
  69.      * @param columnFamilys 列簇 
  70.      *  
  71.      * **/  
  72.     public static void createTable(String tableName,String[] columnFamilys)throws Exception{  
  73.         //admin 对象  
  74.         HBaseAdmin admin=new HBaseAdmin(conf);  
  75.         if(admin.tableExists(tableName)){  
  76.             System.out.println("此表,已存在!");  
  77.         }else{  
  78.             //旧的写法  
  79.             //HTableDescriptor tableDesc=new HTableDescriptor(tableName);  
  80.             //新的api  
  81.             HTableDescriptor tableDesc=new HTableDescriptor(TableName.valueOf(tableName));  
  82.               
  83.             for(String columnFamily:columnFamilys){  
  84.                 tableDesc.addFamily(new HColumnDescriptor(columnFamily));  
  85.             }  
  86.               
  87.             admin.createTable(tableDesc);  
  88.             System.out.println("建表成功!");  
  89.               
  90.         }  
  91.         admin.close();//关闭释放资源  
  92.           
  93.     }  
  94.       
  95.     /** 
  96.      * 删除一个表 
  97.      * @param tableName 删除的表名 
  98.      * */  
  99.     public static void deleteTable(String tableName)throws Exception{  
  100.         HBaseAdmin admin=new HBaseAdmin(conf);  
  101.         if(admin.tableExists(tableName)){  
  102.             admin.disableTable(tableName);//禁用表  
  103.             admin.deleteTable(tableName);//删除表  
  104.             System.out.println("删除表成功!");  
  105.         }else{  
  106.             System.out.println("删除的表不存在!");  
  107.         }  
  108.         admin.close();  
  109.     }  
  110.       
  111.     /** 
  112.      * 插入一条数据 
  113.      * @param tableName 表明 
  114.      * @param columnFamily 列簇 
  115.      * @param column      列 
  116.      * @param value       值 
  117.      *  
  118.      * ***/  
  119.     public static void insertOneRow(String tableName,String rowkey,String columnFamily,String column,String value)throws Exception{  
  120.           
  121.         HTable table=new HTable(conf, tableName);  
  122.         Put put=new Put(Bytes.toBytes(rowkey));  
  123.         put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));  
  124.         table.put(put);//放入表  
  125.         table.close();//释放资源  
  126.           
  127.     }  
  128.       
  129.     /** 
  130.      * 删除一条数据 
  131.      * @param tableName 表名 
  132.      * @param row  rowkey行键 
  133.      *  
  134.      * */  
  135.     public static void deleteOneRow(String tableName,String row)throws Exception{  
  136.           
  137.         HTable table=new HTable(conf, tableName);  
  138.         Delete delete=new Delete(Bytes.toBytes(row));  
  139.         table.delete(delete);  
  140.         table.close();  
  141.     }  
  142.       
  143.       
  144.     /** 
  145.      * 删除多条数据 
  146.      * @param tableName 表名 
  147.      * @param rows 行健集合 
  148.      *  
  149.      * **/  
  150.     public static void deleteList(String tableName,String rows[])throws Exception{  
  151.         HTable table=new HTable(conf, tableName);  
  152.         List<Delete> list=new ArrayList<Delete>();  
  153.         for(String row:rows){  
  154.             Delete del=new Delete(Bytes.toBytes(row));  
  155.             list.add(del);  
  156.         }  
  157.         table.delete(list);  
  158.         table.close();//释放资源  
  159.           
  160.     }  
  161.       
  162.       
  163.     /** 
  164.      * 获取一条数据,根据rowkey 
  165.      * @param  tableName 表名 
  166.      * @param  row 行健 
  167.      *  
  168.      * **/  
  169.     public static void getOneRow(String tableName,String row)throws Exception{  
  170.         HTable table=new HTable(conf, tableName);  
  171.         Get get=new Get(Bytes.toBytes(row));  
  172.         Result result=table.get(get);  
  173.         printRecoder(result);//打印记录  
  174.         table.close();//释放资源  
  175.     }  
  176.       
  177.     /** 
  178.      * 查看某个表下的所有数据 
  179.      *  
  180.      * @param tableName 表名 
  181.      * */  
  182.     public static void showAll(String tableName)throws Exception{  
  183.         HTable table=new HTable(conf, tableName);  
  184.         Scan scan=new Scan();  
  185.         ResultScanner rs=table.getScanner(scan);  
  186.         for(Result r:rs){  
  187.             printRecoder(r);//打印记录  
  188.         }  
  189.         table.close();//释放资源  
  190.     }  
  191.       
  192.       
  193.     /** 
  194.      * 查看某个表下的所有数据 
  195.      *  
  196.      * @param tableName 表名 
  197.      * @param rowKey  行健 
  198.      * */  
  199.     public static void ScanPrefixByRowKey(String tableName,String rowKey)throws Exception{  
  200.         HTable table=new HTable(conf, tableName);  
  201.         Scan scan=new Scan();  
  202.         scan.setFilter(new PrefixFilter(Bytes.toBytes(rowKey)));  
  203.         ResultScanner rs=table.getScanner(scan);  
  204.         for(Result r:rs){  
  205.             printRecoder(r);//打印记录  
  206.         }  
  207.         table.close();//释放资源  
  208.     }  
  209.       
  210.       
  211.     /** 
  212.      * 查看某个表下的所有数据 
  213.      *  
  214.      * @param tableName 表名 
  215.      * @param rowKey 行健扫描 
  216.      * @param limit  限制返回数据量 
  217.      * */  
  218.     public static void ScanPrefixByRowKeyAndLimit(String tableName,String rowKey,long limit)throws Exception{  
  219.         HTable table=new HTable(conf, tableName);  
  220.         Scan scan=new Scan();  
  221.         scan.setFilter(new PrefixFilter(Bytes.toBytes(rowKey)));  
  222.         scan.setFilter(new  PageFilter(limit));  
  223.         ResultScanner rs=table.getScanner(scan);  
  224.         for(Result r:rs){  
  225.             printRecoder(r);//打印记录  
  226.         }  
  227.         table.close();//释放资源  
  228.     }  
  229.       
  230.       
  231.     /** 
  232.      * 根据rowkey扫描一段范围 
  233.      * @param tableName 表名 
  234.      * @param startRow 开始的行健 
  235.      * @param stopRow  结束的行健 
  236.      * **/  
  237.     public  void scanByStartAndStopRow(String tableName,String startRow,String stopRow)throws Exception{  
  238.         HTable table=new HTable(conf, tableName);  
  239.         Scan scan=new Scan();  
  240.         scan.setStartRow(Bytes.toBytes(startRow));  
  241.         scan.setStopRow(Bytes.toBytes(stopRow));  
  242.         ResultScanner rs=table.getScanner(scan);  
  243.         for(Result r:rs){  
  244.             printRecoder(r);  
  245.         }  
  246.         table.close();//释放资源  
  247.           
  248.           
  249.     }  
  250.     /** 
  251.      * 扫描整个表里面具体的某个字段的值 
  252.      * @param tableName  表名 
  253.      * @param columnFalimy 列簇 
  254.      * @param column 列 
  255.      * **/  
  256.     public static  void getValueDetail(String tableName,String columnFalimy,String column)throws Exception{  
  257.       
  258.         HTable table=new HTable(conf, tableName);  
  259.         Scan scan=new Scan();  
  260.         ResultScanner rs=table.getScanner(scan);  
  261.         for(Result r:rs){  
  262.             System.out.println("值: " +new String(r.getValue(Bytes.toBytes(columnFalimy), Bytes.toBytes(column))));  
  263.         }  
  264.         table.close();//释放资源  
  265.           
  266.     }  
  267.       
  268.       
  269.       
  270.       
  271.       
  272.       
  273.       
  274.       
  275.     /** 
  276.      * 打印一条记录的详情 
  277.      *  
  278.      * */  
  279.     public  static void printRecoder(Result result)throws Exception{  
  280.         for(Cell cell:result.rawCells()){  
  281.             System.out.print("行健: "+new String(CellUtil.cloneRow(cell)));  
  282.             System.out.print("列簇: "+new String(CellUtil.cloneFamily(cell)));  
  283.             System.out.print(" 列: "+new String(CellUtil.cloneQualifier(cell)));  
  284.             System.out.print(" 值: "+new String(CellUtil.cloneValue(cell)));  
  285.             System.out.println("时间戳: "+cell.getTimestamp());      
  286.         }  
  287.     }  
  288.       
  289.       
  290.       
  291. }

猜你喜欢

转载自weitao1026.iteye.com/blog/2268090
今日推荐