package cn.sniper.hbase.util; import java.io.IOException; import java.util.ArrayList; 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.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; /** * * @author sniper * */ public class HBaseUtil { /** * hbase操作必备 * @return */ public static Configuration getConfiguration() { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.rootdir", "hdfs://sniper1:9000/hbase"); //使用eclipse时必须添加这个,否则无法定位 conf.set("hbase.zookeeper.quorum", "sniper1, sniper2, sniper3"); return conf; } /** * 创建表(单个列族) * @param tableName * @param columnFamily * @throws IOException */ public static void create(String tableName, String columnFamily) throws IOException { Configuration conf = getConfiguration(); HBaseAdmin admin = new HBaseAdmin(conf); if(admin.tableExists(tableName)) { System.out.println("table exists..."); } else { HTableDescriptor tableDesc = new HTableDescriptor(tableName); tableDesc.addFamily(new HColumnDescriptor(columnFamily)); admin.createTable(tableDesc); System.out.println("table create successful... "); } } /** * 创建表(多个列族) * @param tableName * @param columnFamily * @throws IOException */ public static void create(String tableName, String[] columnFamily) throws IOException { Configuration conf = getConfiguration(); HBaseAdmin admin = new HBaseAdmin(conf); if(admin.tableExists(tableName)) { System.out.println("table exists..."); } else { HTableDescriptor tableDesc = new HTableDescriptor(tableName); for(String family:columnFamily) { tableDesc.addFamily(new HColumnDescriptor(family)); } admin.createTable(tableDesc); System.out.println("table create successful... "); } } /** * 添加一条记录 * @param tableName * @param rowKey * @param columnFamily * @param column * @param value * @throws IOException */ public static void put(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException { Configuration conf = getConfiguration(); HTable table = new HTable(conf, tableName); Put p1 = new Put(Bytes.toBytes(rowKey)); //p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value)); p1.add(columnFamily.getBytes(), column.getBytes(), value.getBytes()); table.put(p1); System.out.println("put " + "table:"+tableName+" rowKey:"+rowKey+" columnFamily:"+columnFamily+" colun:"+column +" value:"+value); } /** * 添多条记录 * @param tableName * @param rowKey * @param columnFamily * @param colValueMap * @throws IOException */ public static void put(String tableName, String rowKey, String columnFamily, Map<String, String> colValueMap) throws IOException { Configuration conf = getConfiguration(); HTable table = new HTable(conf, tableName); List<Put> putList = new ArrayList<Put>(); Set<String> set = colValueMap.keySet(); for(String column : set) { String value = colValueMap.get(column); Put p = new Put(Bytes.toBytes(rowKey)); p.add(columnFamily.getBytes(), column.getBytes(), value.getBytes()); putList.add(p); } table.put(putList); System.out.println("put " + "table:"+tableName+" rowKey:"+rowKey+" columnFamily:"+columnFamily+" colValueMap:"+colValueMap); } /** * 读取一条记录 * @param tableName * @param rowKey * @param columnFamily * @param column * @return * @throws IOException */ public static String get(String tableName, String rowKey, String columnFamily, String column) throws IOException { HTable table = new HTable(getConfiguration(), tableName); //拿到rowKey对应的所有的列,result0.list().get(0),result0.list().get(1)... Get get0 = new Get(Bytes.toBytes(rowKey)); Result result0 = table.get(get0); //根据rowkey取得记录,打印记录的字段名,字段值 List<KeyValue> keyValueList = result0.list(); for(KeyValue keyValue : keyValueList) { System.out.println("key:" + Bytes.toString(keyValue.getKey()) + " value:" + Bytes.toString(keyValue.getValue())); } //System.out.println("get:" + result0.size() + " " + result0.list() + " " + Bytes.toString(result0.list().get(0).getValue())); //拿到rowKey中,某个列的数据 Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column)); Result result = table.get(get); System.out.println(result); System.out.println(result.list()); System.out.println(result.list().get(0)); System.out.println(result.list().get(0).getValue()); System.out.println("size:" + result.size() + " value:" + Bytes.toString(result.list().get(0).getValue())); Get g = new Get(rowKey.getBytes()); g.addColumn(columnFamily.getBytes(), column.getBytes());//可以不加该条件 Result get1 = table.get(g); String value = new String(get1.getValue(columnFamily.getBytes(), column.getBytes())); return value; } /** * 显示所有数据 * @param tableName * @throws IOException */ public static void scan(String tableName) throws IOException { HTable table = new HTable(getConfiguration(), tableName); Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); for(Result result : scanner) { System.out.println("scan:"+result); } } /** * 显示所有数据 * @param tableName * @param columnFamily * @param column * @throws IOException */ public static void scan(String tableName, String columnFamily, String column) throws IOException { HTable table = new HTable(getConfiguration(), tableName); Scan scan = new Scan(); if(null != column && !column.trim().equals("")) { //扫描列 scan.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column)); } else { //扫描列族 scan.addFamily(Bytes.toBytes(columnFamily)); } ResultScanner scanner = table.getScanner(scan); for(Result result : scanner) { System.out.println("scan:"+result); List<KeyValue> keyValueList = result.list(); for(KeyValue keyValue:keyValueList) { System.out.println("key:" + Bytes.toString(keyValue.getKey()) + " value:" + Bytes.toString(keyValue.getValue())); } } scanner.close(); } /** * 显示所有数据[分页] * @param tableName * @param columnFamily * @param column * @param rowKeyBegin * @param rowKeyEnd * @throws IOException */ public static void scan(String tableName, String columnFamily, String column, String rowKeyBegin, String rowKeyEnd) throws IOException { HTable table = new HTable(getConfiguration(), tableName); Scan scan = new Scan(); scan.setStartRow(rowKeyBegin.getBytes());//开始位置 scan.setStopRow(rowKeyEnd.getBytes());//结束位置 if(null != column && !column.trim().equals("")) { //扫描列 scan.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column)); } else { //扫描列族 scan.addFamily(Bytes.toBytes(columnFamily)); } ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { String rowKey = new String(result.getRow());//拿到rowkey String name = new String(result.getValue(columnFamily.getBytes(), column.getBytes())); System.out.println(rowKey+"\t"+name+"\t"); } } /** * 删除表 * @param tableName * @throws IOException */ public static void drop(String tableName) throws IOException { HBaseAdmin admin = new HBaseAdmin(getConfiguration()); if(admin.tableExists(tableName)) { admin.disableTable(tableName); admin.deleteTable(tableName); } } /** * 删除一条记录 * @param tableName * @param rowKey * @throws IOException */ public static void delete(String tableName, String rowKey) throws IOException { HTable table = new HTable(getConfiguration(), tableName); Delete delete = new Delete(rowKey.getBytes()); table.delete(delete); } public static void main(String[] args) throws IOException { //Configuration conf = HBaseUtil.getConfiguration(); //System.out.println(conf); //HBaseUtil.create("t5", "f1"); //HBaseUtil.create("t5", new String[]{"f1", "f2"}); /*Map<String, String> map = new HashMap<String, String>(); map.put("c1", "aaaa"); map.put("c2", "bbb"); HBaseUtil.put("t5", "r1", "f1", map);*/ System.err.println(HBaseUtil.get("t5", "r1", "f1", "c1")); /*HBaseUtil.put("t5", "r1", "f1", "c1", "aaaaaa"); HBaseUtil.put("t5", "r1", "f1", "c2", "bbbbbb"); HBaseUtil.put("t5", "r1", "f2", "c1", "cccccc"); HBaseUtil.put("t5", "r2", "f2", "c1", "cccccc"); HBaseUtil.get("t5", "r1", "f1", "c1"); HBaseUtil.scan("t5"); HBaseUtil.scan("t5", "f1", ""); HBaseUtil.scan("t5", "f1", "c1");*/ //HBaseUtil.drop("t5"); //System.out.println(conf); } }