java操作hbase

来源:互联网 发布:直播行业现状知乎 编辑:程序博客网 时间:2024/05/17 22:44
package hbase;import java.io.IOException;  import java.util.ArrayList;  import java.util.HashMap;import java.util.List;  import java.util.Map;   import org.apache.commons.lang.StringUtils;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.Result;  import org.apache.hadoop.hbase.client.ResultScanner;  import org.apache.hadoop.hbase.client.Scan;  import org.apache.hadoop.hbase.client.Put;  import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;import org.apache.hadoop.hbase.filter.Filter;import org.apache.hadoop.hbase.filter.FilterList;import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;import org.apache.hadoop.hbase.util.Bytes; public class HbaseBaseDao {private static Configuration conf = null;             static {          Configuration HBASE_CONFIG = new Configuration();          HBASE_CONFIG.set("hbase.zookeeper.quorum", "192.168.2.99");          HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");          conf = HBaseConfiguration.create(HBASE_CONFIG);      }              /**      * 创建表     */      public static HbaseResult creatTable(String tableName, String[] familys){    HbaseResult hRes = new HbaseResult();    try{    HBaseAdmin admin = new HBaseAdmin(conf);              if (admin.tableExists(tableName)) {            hRes.setResult(false);            hRes.setMessage("table already exists!");                System.out.println("table already exists!");                return hRes;            } else {                  HTableDescriptor tableDesc = new HTableDescriptor(tableName);                  for(int i=0; i<familys.length; i++){                      tableDesc.addFamily(new HColumnDescriptor(familys[i]));                  }                  admin.createTable(tableDesc);                 hRes.setResult(true);                System.out.println("create table " + tableName + " ok.");              }       }catch(Exception e){    hRes.setResult(false);        hRes.setMessage("exception!");    e.printStackTrace();    }    return hRes;    }            /**      * 删除表     */      public static HbaseResult deleteTable(String tableName) throws Exception {    HbaseResult hRes = new HbaseResult();       try {             HBaseAdmin admin = new HBaseAdmin(conf);             admin.disableTable(tableName);             admin.deleteTable(tableName);            hRes.setResult(true);           System.out.println("delete table " + tableName + " ok.");         } catch (Exception e) {       hRes.setResult(false);          hRes.setMessage("exception!");           e.printStackTrace();         }         return hRes;    }             /**      * 更新记录     */      public static HbaseResult updateRecord (String tableName, String rowKey, String family, String qualifier, String value)throws Exception{    HbaseResult hRes = new HbaseResult();    //check    if(StringUtils.isEmpty(tableName)||StringUtils.isEmpty(rowKey)||StringUtils.isEmpty(family)||StringUtils.isEmpty(qualifier)||StringUtils.isEmpty(value)){    hRes.setResult(false);         hRes.setMessage("tableName,rowKey,family,qualifier,value all is required!");         return hRes;    }        try {              HTable table = new HTable(conf, tableName);              Put put = new Put(Bytes.toBytes(rowKey));              put.add(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value));              table.put(put);              hRes.setResult(true);            System.out.println("update recored " + rowKey + " to table " + tableName +" ok.");          } catch (Exception e) {        hRes.setResult(false);         hRes.setMessage("exception!");            e.printStackTrace();          }          return hRes;    }                  /**        * 插入记录     * @param tableName        */        public static HbaseResult addRowRecord(String tableName,String rowKey,String family,String[]qualifiers,String[] values) throws Exception{    HbaseResult hRes = new HbaseResult();    //check    if(StringUtils.isEmpty(tableName)||StringUtils.isEmpty(rowKey)||StringUtils.isEmpty(family)||null==qualifiers||qualifiers.length==0||null==values||values.length==0){    hRes.setResult(false);         hRes.setMessage("tableName,rowKey,family,qualifiers,values all is required!");         return hRes;    }    //check    if(qualifiers.length!=values.length){    hRes.setResult(false);         hRes.setMessage("�ֶ���Ƶ��������ֶ�ֵ��������һ��!");         return hRes;    }        try {              HTable table = new HTable(conf, tableName);              Put put = new Put(Bytes.toBytes(rowKey));             for(int i=0;i<qualifiers.length;i++){            put.add(Bytes.toBytes(family),Bytes.toBytes(qualifiers[i]),Bytes.toBytes(values[i]));             }                           table.put(put);              hRes.setResult(true);            System.out.println("insert recored " + rowKey + " to table " + tableName +" ok.");          } catch (Exception e) {        hRes.setResult(false);         hRes.setMessage("exception!");            e.printStackTrace();          }        return hRes;    }               /**      * 删除记录      */      @SuppressWarnings("rawtypes")    public static HbaseResult delRecord (String tableName, String rowKey) throws IOException{    HbaseResult hRes = new HbaseResult();    //check    if(StringUtils.isEmpty(tableName)||StringUtils.isEmpty(rowKey)){    hRes.setResult(false);         hRes.setMessage("tableName,rowKey all is required!");         return hRes;    }    try{    HTable table = new HTable(conf, tableName);  List list = new ArrayList();          Delete del = new Delete(Bytes.toBytes(rowKey));          list.add(del);          table.delete(list);        hRes.setResult(true);        System.out.println("del recored " + rowKey + " ok.");    }catch(Exception e){    hRes.setResult(false);         hRes.setMessage("exception!");    e.printStackTrace();    }                return hRes;    }             /**      * 根据rowkey获取一条记录     */      public static Result getOneRecord (String tableName, String rowKey) throws Exception{    Result rs = null;    //check    if(StringUtils.isEmpty(tableName)||StringUtils.isEmpty(rowKey)){    return  rs;    }    try{    HTable table = new HTable(conf, tableName);  //            Get get = new Get(rowKey.getBytes());// ���rowkey��ѯ            Get get = new Get(Bytes.toBytes(rowKey));// ���rowkey��ѯ            Result r = table.get(get);                 System.out.println("rowkey:" + new String(r.getRow()));                 for (KeyValue keyValue : r.raw()) {                     System.out.println("Family��" + new String(keyValue.getFamily())                     +";Qualifier:" + new String(keyValue.getQualifier())                     + ";Vaule:" + new String(keyValue.getValue()));                 }      }catch(Exception e){    e.printStackTrace();    }    return rs;    }              /**      * 查询表中所有记录     */      public static ResultScanner getAllRecord (String tableName) throws Exception{    ResultScanner rs=null;    //check    if(StringUtils.isEmpty(tableName)){    return rs;    }        try{               HTable table = new HTable(conf, tableName);               Scan s = new Scan();               rs = table.getScanner(s);               for (Result r : rs) {                      System.out.println("rowkey:" + new String(r.getRow()));                      for (KeyValue keyValue : r.raw()) {                       System.out.println("family:" + new String(keyValue.getFamily())                          +";Qualifier:"+new String(keyValue.getQualifier())                       + ";Value:" + new String(keyValue.getValue()));                      }               }          } catch (Exception e){              e.printStackTrace();          } finally{        rs.close();// always close the ResultScanner!        }         return rs;    }              /**        * 单条件查询,如根据编号查询      * @param tableName        */        public static ResultScanner QueryByFilter(String tableName,String family,String qualifier,String value) {    ResultScanner rs = null;    //check    if(StringUtils.isEmpty(tableName)||StringUtils.isEmpty(family)||StringUtils.isEmpty(qualifier)||StringUtils.isEmpty(value)){    return rs;    }        try {        HTable table = new HTable(conf, tableName);              SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(family),                       Bytes.toBytes(qualifier),                       CompareOp.EQUAL,Bytes.toBytes(value));              filter.setFilterIfMissing(true);                            Scan s = new Scan();                 s.setFilter(filter);                 rs = table.getScanner(s);                 for (Result r : rs) {                     System.out.println("rowkey:" + new String(r.getRow()));                     for (KeyValue keyValue : r.raw()) {                         System.out.println("Family��" + new String(keyValue.getFamily())                         +";Qualifier:" + new String(keyValue.getQualifier())                         + ";Vaule:" + new String(keyValue.getValue()));                     }                 }          } catch (Exception e) {                 e.printStackTrace();             }finally{        rs.close();        }         return rs;        }               /**   * 多条件查询    * @param tableName   * @param families   * @param qualifiers   * @param values   * @param startRow   * @param stopRow   * @return   */    public static ResultScanner QueryByMultiFilter(String tableName,String[] families,String[] qualifiers,String[] values,String startRow,String stopRow) {         ResultScanner rs = null;            try {                 HTable table = new HTable(conf, tableName);                 List<Filter> filters = new ArrayList<Filter>();                     for(int index=0;index<families.length;index++){                  SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(families[index]),                       Bytes.toBytes(qualifiers[index]),                       CompareOp.EQUAL,Bytes.toBytes(values[index]));                  filters.add(filter);              }                            FilterList filterList = new FilterList(filters);                     Scan scan = new Scan();            if(StringUtils.isNotEmpty(stopRow)&&StringUtils.isNotEmpty(stopRow)){             scan.setStartRow(Bytes.toBytes(startRow));                 scan.setStopRow(Bytes.toBytes(stopRow));            }            if(null!=values&&null!=qualifiers){            scan.setFilter(filterList);            }                             rs = table.getScanner(scan);                 for (Result r : rs) {                     System.out.println("---rowkey:" + new String(r.getRow()));                     for (KeyValue keyValue : r.raw()) {                         System.out.println("Family��" + new String(keyValue.getFamily())                         +";Qualifier:" + new String(keyValue.getQualifier())                         + ";Vaule:" + new String(keyValue.getValue()));                     }                 System.out.println("------------------------------------------------");            }                             } catch (Exception e) {                 e.printStackTrace();             }finally{        rs.close();         }         return rs;    }              public static void  main (String [] agrs) {          try {              String tableName = "order";              String[] families = {"f","f"};            String family = "f";                        //create table//            HbaseBaseDao.creatTable(tableName, families);                                     //add record//        String[] qu = {"orderNo","customerNo","customerName","tel","email","hotelNo","hotelName","roomTypeNo","roomTypeName","orderTime","checkInTime","checkOutTime","roomNum","roomDayNum","totalMoney","feeMoney","payMoney","orderStatus","notes"};//        String[] val = {"W201409031636473416","8899105014","赵琦","15995460002","00202@163.com","H0004","苏州车坊店","R0001","豪华单人房","2014-09-16 16:36:47","2014-09-14 18:00:00","2014-09-15 12:00:00","1","1","288.00","20.00","268.00","1","能上网"};//        HbaseBaseDao.addRowRecord(tableName, "889910501420140916W201409031636473416", "f", qu, val);                           //get all record//            HbaseBaseDao.getAllRecord(tableName); //                             //get record//            HbaseBaseDao.getOneRecord(tableName, "889910508320140903W201409031636473482");              //            HbaseBaseDao.QueryByFilter(tableName, family, "customerName", "����");//            String dateStart = "00000000";//            String dateEnd = "99999999";//            String orderStart = "W000000000000000000";//            String orderEnd = "W999999999999999999";//            String[] qualifiers = {"roomTypeNo","hotelNo"}; //            String[] values = {"R0001","H0004"};//            String startRow = "889910501420140903W201409031636473482";//            String stopRow =  "889910501420140923W201409031636473482"; //            HbaseBaseDao.QueryByMultiFilter(tableName, families, qualifiers, values,startRow,stopRow);//               //               //            System.out.println("===========del one record========");  //            HbaseBaseDao.delRecord(tableName, "W2014090308070002");              HbaseBaseDao.getAllRecord("test");  //               //            System.out.println("===========show all record========");  //            HbaseBaseDao.getAllRecord(tableName);  //                    //            HbaseBaseDao.deleteTable(tableName);                                } catch (Exception e) {              e.printStackTrace();          }      }  }

参考资料:

http://blog.csdn.net/wangjiannuaa/article/details/7572500

http://blog.csdn.net/hxpjava1/article/details/19035691
0 0
原创粉丝点击