从今天开始学习hbase数据库,并每天记下自己的学习过程
****************************************************************************************************************************************************
做hbase的开发,肯定不会是通过shell操作和hbase数据库,------>>>java api
(1)创建全局的configration对象,作用是在获得hbase的配置信息,从而和hmaster通信什么的
private static Configuration conf =null; /** * 初始化配置 */ static { conf = HBaseConfiguration.create(); }
(2-1)创建表:
HBaseAdmin admin = new HBaseAdmin(conf); if (admin.tableExists(tableName)) { //TODO 如弹出提示框什么的 } else { HTableDescriptor tableDesc = new HTableDescriptor(tableName); for(int i=0; i<familys.length; i++){ tableDesc.addFamily(new HColumnDescriptor(familys[i])); } admin.createTable(tableDesc);
(2-2)删除表 :
HBaseAdmin admin = new HBaseAdmin(conf); admin.disableTable(tableName); admin.deleteTable(tableName);
(2-3)插入一条记录 :
public static void addRecord (String tableName, String rowKey, String family, String qualifier, String value) throws Exception{ //参数的含义:表名 主键 列族 列名 数据 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); System.out.println("insert recored " + rowKey + " to table " + tableName +" ok."); } catch (IOException e) { e.printStackTrace(); } }(2-4)删除一条记录 : 根据主键删除
public static void delRecord (String tableName, String rowKey) throws IOException{ HTable table = new HTable(conf, tableName); List list = new ArrayList(); Delete del = new Delete(rowKey.getBytes()); list.add(del); table.delete(list); System.out.println("del recored " + rowKey + " ok."); }(2-5)查找数据 :
(2-5-1)查询所有数据
HTablePool pool = new HTablePool(configuration, 1000); HTable table = (HTable) pool.getTable(tableName); ResultScanner rs = table.getScanner(new Scan()); for (Result r : rs) { System.out.println("获得到rowkey:" + new String(r.getRow())); for (KeyValue keyValue : r.raw()) { System.out.println("列:" + new String(keyValue.getFamily()) + "====值:" + new String(keyValue.getValue())); } }(2-5-2)根据主键查询
HTablePool pool = new HTablePool(configuration, 1000); HTable table = (HTable) pool.getTable(tableName); Get scan = new Get("abcdef".getBytes());// 根据rowkey查询 Result r = table.get(scan); System.out.println("获得到rowkey:" + new String(r.getRow())); for (KeyValue keyValue : r.raw()) { System.out.println("列:" + new String(keyValue.getFamily()) + "====值:" + new String(keyValue.getValue())); }
HTablePool pool = new HTablePool(configuration, 1000); HTable table = (HTable) pool.getTable(tableName); Filter filter = new SingleColumnValueFilter( Bytes.toBytes("column1"), null, CompareOp.EQUAL, Bytes.toBytes("aaa")); // 当列column1的值为aaa时进行查询 Scan s = new Scan(); s.setFilter(filter); ResultScanner rs = table.getScanner(s); for (Result r : rs) { System.out.println("获得到rowkey:" + new String(r.getRow())); for (KeyValue keyValue : r.raw()) { System.out.println("列:" + new String(keyValue.getFamily()) + "====值:" + new String(keyValue.getValue())); } }(2-5-4)多条件查询:方法同上,几个不同的filter实现
HTablePool pool = new HTablePool(configuration, 1000); HTable table = (HTable) pool.getTable(tableName); List<Filter> filters = new ArrayList<Filter>(); Filter filter1 = new SingleColumnValueFilter( Bytes.toBytes("column1"), null, CompareOp.EQUAL, Bytes.toBytes("aaa")); filters.add(filter1); Filter filter2 = new SingleColumnValueFilter( Bytes.toBytes("column2"), null, CompareOp.EQUAL, Bytes.toBytes("bbb")); filters.add(filter2); Filter filter3 = new SingleColumnValueFilter( Bytes.toBytes("column3"), null, CompareOp.EQUAL, Bytes.toBytes("ccc")); filters.add(filter3); FilterList filterList1 = new FilterList(filters); Scan scan = new Scan(); scan.setFilter(filterList1); ResultScanner rs = table.getScanner(scan); for (Result r : rs) { System.out.println("获得到rowkey:" + new String(r.getRow())); for (KeyValue keyValue : r.raw()) { System.out.println("列:" + new String(keyValue.getFamily()) + "====值:" + new String(keyValue.getValue())); } } rs.close();