package com.sdo.sjzx.jptj; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; 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.MasterNotRunningException; import org.apache.hadoop.hbase.ZooKeeperConnectionException; 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.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.util.Bytes; public class Test { private static Configuration conf = null; static { conf = HBaseConfiguration.create(); conf.set( "hbase.zookeeper.quorum", "zk1,zk2,zk3"); } //参数:表名,列名(可以有多个) public void createTable(String tablename, String[] cfs) throws IOException { HBaseAdmin admin = new HBaseAdmin(conf); if (admin.tableExists(tablename)) { System.out.println("table already exists!"); } else { HTableDescriptor tableDesc = new HTableDescriptor(tablename); for (int i = 0; i < cfs.length; i++) { tableDesc.addFamily(new HColumnDescriptor(cfs[i])); } admin.createTable(tableDesc); System.out.println("table create OK!"); } } //删除表,先disable,再delete public void deleteTable(String tablename) throws IOException { try { HBaseAdmin admin = new HBaseAdmin(conf); admin.disableTable(tablename); admin.deleteTable(tablename); System.out.println("delete ok"); } catch (MasterNotRunningException e) { e.printStackTrace(); } catch (ZooKeeperConnectionException e) { e.printStackTrace(); } } //insert记录, public void writeRow(String tablename,String rowKey,Map<String,Map<String,String>> m) { try { HTable table = new HTable(conf, tablename); Put put = new Put(Bytes.toBytes(rowKey));//"001" Set<String> key = m.keySet(); for(Iterator<String> it = key.iterator();it.hasNext();){ String k = it.next(); Map<String,String> tmpM = m.get(k); Set<String> keyInner = tmpM.keySet(); for(Iterator<String> itInner = keyInner.iterator();itInner.hasNext();) { String kInner = itInner.next(); String vInner = tmpM.get(kInner); put.add(Bytes.toBytes(k), Bytes.toBytes(kInner), Bytes.toBytes(vInner)); table.put(put); } } } catch (IOException e) { e.printStackTrace(); } } public void deleteRow(String tablename, String rowkey) throws IOException { HTable table = new HTable(conf, tablename); List list = new ArrayList(); Delete d1 = new Delete(rowkey.getBytes()); list.add(d1); table.delete(list); System.out.println(""); } public static void selectRow(String tablename, String rowKey) throws IOException { HTable table = new HTable(conf, tablename); Get g = new Get(rowKey.getBytes()); Result rs = table.get(g); for (KeyValue kv : rs.raw()) { System.out.print(new String(kv.getRow()) + " "); System.out.print(new String(kv.getFamily()) + ":"); System.out.print(new String(kv.getQualifier()) + " "); System.out.print(kv.getTimestamp() + " "); System.out.println(new String(kv.getValue())); } } public void scaner(String tablename) { try { HTable table = new HTable(conf, tablename); Scan s = new Scan(); ResultScanner rs = table.getScanner(s); for (Result r : rs) { KeyValue[] kv = r.raw(); for (int i = 0; i < kv.length; i++) { System.out.print(new String(kv[i].getRow()) + " "); System.out.print(new String(kv[i].getFamily()) + ":"); System.out.print(new String(kv[i].getQualifier()) + " "); System.out.print(kv[i].getTimestamp() + " "); System.out.println(new String(kv[i].getValue())); } } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] agrs) { try { String tablename = "lxwtest"; Test t = new Test(); t.deleteTable(tablename); //创建一张列为userinfo和userdeptinfo的表 String[] s = new String[2]; s[0] = "userinfo"; s[1] = "userdeptinfo"; t.createTable(tablename,s); String userId = "003"; String userName = "wangwu"; int age = 25; String dept = "data analysis"; //向userinfo中写入name=?和age=? //向userdeptinfo中写入dept=? //这里将源数据构建合适的数据结构,比如Map<String,Map<String,String>> //来完成insert Map<String,Map<String,String>> m = new HashMap<String,Map<String,String>>(); Map<String,String> kvUserInfo = new HashMap<String,String>(); kvUserInfo.put("name", userName); kvUserInfo.put("age",age+""); m.put("userinfo", kvUserInfo); Map<String,String> kvUserDeptInfo = new HashMap<String,String>(); kvUserDeptInfo.put("deptname", dept); m.put("userdeptinfo", kvUserDeptInfo); t.writeRow(tablename,userId,m); t.scaner(tablename); } catch (Exception e) { e.printStackTrace(); } } }
scan出来的结果:
hbase(main):028:0> scan 'lxwtest' ROW COLUMN+CELL 001 column=userdeptinfo:deptname, timestamp=1340092898883, value=data platform 001 column=userinfo:age, timestamp=1340092898883, value=27 001 column=userinfo:name, timestamp=1340092898883, value=zhangsan 002 column=userdeptinfo:deptname, timestamp=1340092951198, value=data platform 002 column=userinfo:age, timestamp=1340092951198, value=25 002 column=userinfo:name, timestamp=1340092951198, value=lisi 003 column=userdeptinfo:deptname, timestamp=1340093006923, value=data analysis 003 column=userinfo:age, timestamp=1340093006923, value=25 003 column=userinfo:name, timestamp=1340093006923, value=wangwu