一、環境服務器
(1)Hbase部署在CentOS下,單機模式,配置的host爲:hadoophostoop
(2)Hbase版本爲1.2.4ip
二、代碼hadoop
public class HBasePutExample { static Configuration conf = null; static { //設置鏈接信息 conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum","10.28.16.197"); conf.set("hbase.zookeeper.property.clientPort","2181"); conf.setInt("hbase.rpc.timeout",2000); conf.setInt("hbase.client.operation.timeout",3000); conf.setInt("hbase.client.scanner.timeout.period",6000); } public static void main(String[] args) throws Exception { String tableName = "test2"; String[] colfam1 = new String[]{"colfam1"}; creatTable(tableName,colfam1); HTable table = new HTable(conf, tableName); // queryResult(colfam1, table); queryByScanFilter(table,colfam1); // putData(colfam1, table); table.close(); } /** * 經過scan方式過濾數據 * @param table * @param colfam1 */ private static void queryByScanFilter(HTable table, String[] colfam1) { FilterList fList = new FilterList(); fList.addFilter(new SingleColumnValueFilter(colfam1[0].getBytes(),Bytes.toBytes("quall"), CompareFilter.CompareOp.EQUAL, Bytes.toBytes("value-83"))); Scan scan = new Scan(); scan.setFilter(fList); try { ResultScanner scanner = table.getScanner(scan); for(Result r=scanner.next();r != null;r = scanner.next()){ System.out.println(new String(r.getValue(Bytes.toBytes(colfam1[0]),Bytes.toBytes("quall")))); System.out.println(new String(r.getValue(Bytes.toBytes(colfam1[0]),Bytes.toBytes("qual2")))); for(Cell c: r.listCells()){ System.out.println("--row : "+ new String(c.getRowArray())); System.out.println("--column : "+ new String(c.getFamilyArray())); System.out.println("--value : "+ new String(c.getValueArray())); } } } catch (IOException e) { e.printStackTrace(); } } /** * 向表中添加數據 * @param colfam1 * @param table * @throws IOException */ private static void putData(String[] colfam1, HTable table) throws IOException { for(int i=3;i<100;i++){ Put put = new Put(Bytes.toBytes("row"+i)); put.add(Bytes.toBytes(colfam1[0]),Bytes.toBytes("quall"),Bytes.toBytes("value-"+i)); put.add(Bytes.toBytes(colfam1[0]),Bytes.toBytes("qual2"),Bytes.toBytes("value-2-"+i)); table.put(put); } table.flushCommits(); } /** * 根據列族的rowKey獲取單行數據 * @param colfam1 * @param table * @throws IOException */ private static void queryResult(String[] colfam1, HTable table) throws IOException { Get get = new Get(Bytes.toBytes("row1")); Result result = table.get(get); System.out.println("result = "+ new String(result.getValue(colfam1[0].getBytes(),"quall".getBytes()))); System.out.println(get.familySet().toString()); } /* * 建立表 * * @tableName 表名 * * @family 列族列表 */ public static void creatTable(String tableName, String[] family) throws Exception { Connection conn = ConnectionFactory.createConnection(conf); // HBaseAdmin admin = new HBaseAdmin(conf); HBaseAdmin admin = (HBaseAdmin)conn.getAdmin(); HTableDescriptor desc = new HTableDescriptor(tableName); for (int i = 0; i < family.length; i++) { desc.addFamily(new HColumnDescriptor(family[i])); } if (admin.tableExists(tableName)) { System.out.println("table Exists!"); return; } else { admin.createTable(desc); System.out.println("create table Success!"); } } }
三、運行rpc
運行前,須要在本地環境配置Hbase部署服務器的hosts,不然會鏈接超時,報主機名找不到,如:部署
10.28.16.197 hadoophostget