Recently, I tested HBase related operations and found that the online examples are copied everywhere, and they are old versions. Many functions have been abandoned or deleted after hbase1.x. The following is an example of testing with the new version of hbase for your reference.
environment:
Hadoop2.5
hbase 1.1.4
package jyw.test; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.ArrayList; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Get; 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.client.Table; import org.apache.hadoop.hbase.filter.FilterList; import org.apache.hadoop.hbase.filter.PrefixFilter; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.RowFilter; import org.apache.hadoop.hbase.filter.CompareFilter; import org.apache.hadoop.hbase.filter.BinaryComparator; public class HBaseTableNewFunc { private static Connection conn=null; private static Configuration conf=null; public static synchronized void getConnect() throws IOException { conf=HBaseConfiguration.create(); conn =ConnectionFactory.createConnection(conf); // return conn; } public static void createTable(String tableName,String famName) throws IOException { Admin admin= conn.getAdmin(); if (admin.tableExists(TableName.valueOf(tableName))) { System.out.println("table Exists!"); // System.exit(0); } else{ HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName)); tableDesc.addFamily(new HColumnDescriptor(famName)); admin.createTable(tableDesc); System.out.println("create table success!"); } } public static void addRow(String tableName,String rowKey, String columnFamily, Map<String,String> colmap) throws Exception { Iterator<Map.Entry<String, String>> iterator = colmap.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); String colname = entry.getKey(); String colval = entry.getValue(); addRowKeyValue(tableName,rowKey,columnFamily,colname, colval); } } public static Map<String,String> getRow(String tableName, String rowKey) throws IOException { Map<String,String> colmap = new HashMap<String, String>(); Table table = conn.getTable(TableName.valueOf(tableName)); Get g=new Get(Bytes.toBytes(rowKey)); Result result=table.get(g); List<Cell> cells = result.listCells(); for (Cell c : cells){ colmap.put(Bytes.toString(CellUtil.cloneQualifier(c)),Bytes.toString(CellUtil.cloneValue(c))); } table.close(); return colmap; } /* * Get the specified field * */ public static Map<String,String> getRow(String tableName, String rowKey,String family, List<String> fields) throws IOException { Map<String,String> colmap = new HashMap<String, String>(); Table table = conn.getTable(TableName.valueOf(tableName)); Get g=new Get(Bytes.toBytes(rowKey)); Result result=table.get(g); for(String colname :fields){ colmap.put(colname,Bytes.toString(result.getValue(Bytes.toBytes(family), Bytes.toBytes(colname)))); } table.close(); return colmap; } /* * http://blog.csdn.net/cnweike/article/details/42920547 * */ public static Map<String,Map<String,String>> getListByFilter(String tableName, String rowprefix) throws IOException { Map<String,Map<String,String>> resultmap = new HashMap<String, Map<String,String>>(); Scan scan1 = new Scan(); Table table = conn.getTable(TableName.valueOf(tableName)); // Filter rowrf = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(rowprefix))); PrefixFilter rowrf = new PrefixFilter(Bytes.toBytes(rowprefix)); List<Filter> filters = new ArrayList<Filter>(); filters.add(rowrf); FilterList flist = new FilterList(FilterList.Operator.MUST_PASS_ALL, filters); scan1. // setStartRow(Bytes.toBytes("row1")). //setStopRow(Bytes.toBytes("row3")). setFilter(flist); ResultScanner scanner1 = table.getScanner(scan1); for(Result res : scanner1){ Map<String,String> colmap = new HashMap<String,String>(); String rowkey =null; for(Cell cell : res.rawCells()){ rowkey = Bytes.toString(CellUtil.cloneRow(cell)); colmap.put(Bytes.toString(CellUtil.cloneQualifier(cell)),Bytes.toString(CellUtil.cloneValue(cell))); } resultmap.put(rowkey, colmap); } scanner1.close(); table.close(); return resultmap; } //scan //delete /* * tablename table name * rowkey row key * columnFamily column family name * column column name * value value * */ public static void addRowKeyValue(String tableName, String rowKey, String columnFamily, String column, String value) throws Exception { Table table = conn.getTable(TableName.valueOf(tableName)); Put put = new Put(Bytes.toBytes(rowKey));// 指定行 // Parameters are: column family, column, value put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value)); table.put(put); } public static void main (String [] agrs) { String tablename="jyw_testnew"; String fam = "userinfo"; try{ System.out.println(" start connect hbase"); getConnect(); System.out.println(" start create table:"+tablename); createTable(tablename,fam); System.out.println(" start insert data: rowkey=j1 uname=jiayongwei password=abcdefg"); Map<String,String> rowmap = new HashMap<String,String>(); rowmap.put("uname", "jiayongwei"); rowmap.put("password", "abcdefg"); rowmap.put("age", "19"); addRow(tablename,"j1",fam,rowmap); //insert data System.out.println(" start insert data: rowkey=j2 uname=jiayongwei2 password=abcdefg2"); rowmap.clear(); rowmap.put("uname", "jiayongwei2"); rowmap.put("password", "abcdefg2"); addRow(tablename,"j2",fam,rowmap); System.out.println(" start insert data: rowkey=j22 uname=jiayongwei2 password=abcdefg2"); rowmap.clear(); rowmap.put("uname", "jiayongwei22"); rowmap.put("password", "abcdefg22"); addRow(tablename,"j22",fam,rowmap); // Get the entire row of data by key System.out.println(" get by key return one row all columns rowkey=j1"); Map<String,String> result=getRow(tablename,"j1"); System.out.println("data:"+result); //Get the specified field by key System.out.println(" get by key return set columns rowkey=j1, filed: password"); List<String> columns = new ArrayList<String>(); columns.add("password"); Map<String,String> result1=getRow(tablename, "j1", fam, columns); System.out.println(result1); //filter System.out.println("get data by row filter j2"); Map<String,Map<String,String>> mulresult= getListByFilter(tablename, "j2"); System.out.println("data List:"+mulresult); }catch(Exception e){ e.printStackTrace (); } } }
Built-in filter reference blog: http://blog.csdn.net/cnweike/article/details/42920547