陈辉煌 发表于 2018-8-30 07:16:52

HBbaseUtils(HBbase shell的java代码实现)

  package com.yuhui.gd.hadoop.hbase;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Map.Entry;
  import java.util.NavigableMap;
  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.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>  // 声明静态配置 HBaseConfiguration
  static Configuration config = HBaseConfiguration.create();
  static {
  config.set("hbase.zookeeper.quorum", "hd204,hd205,hd206");
  }
  // 创建一张表,通过HBaseAdmin HTableDecriptor来创建
  public static void create(String tableName, String[] columnFamilys)
  throws Exception {
  HBaseAdmin admin = new HBaseAdmin(config);
  if (admin.tableExists(tableName)) {
  System.err.println("表名为: " + tableName + " 已经存在!");
  System.exit(0);
  } else {
  HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
  for (String columnFamily : columnFamilys) {
  HColumnDescriptor columnDescriptor = new HColumnDescriptor(
  columnFamily);
  tableDescriptor.addFamily(columnDescriptor);
  }
  admin.createTable(tableDescriptor);
  System.out.println("Create table Success!");
  }
  }
  // 添加一条数据,通过HTable Put为已经存在的表来添加数据
  public static void put(String tableName, String row, String columnFamily,
  String columnQualifer, String data) throws Exception {
  HTable table = new HTable(config, tableName);
  Put put = new Put(Bytes.toBytes(row));
  put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifer),
  Bytes.toBytes(data));
  table.put(put);
  System.out.println("put '" + row + "' , '" + columnFamily + ":"
  + columnQualifer + "','" + data + "'");
  }
  // 根据表名,行键值查询一行记录
  public static void get(String tableName, String row) throws Exception {
  HTable table = new HTable(config, tableName);
  Get get = new Get(Bytes.toBytes(row));
  Result result = table.get(get);
  NavigableMap map = result.getNoVersionMap();
  Set set = map.entrySet();
  System.out.println("ROW\t\tCOLUMN:CELL");
  for (Entry entry : set) {
  String familyName = new String(entry.getKey());
  NavigableMap nmap = entry.getValue();
  Set valueSet = nmap.entrySet();
  for (Entry column : valueSet) {
  String columnName = new String(column.getKey());
  String columnValue = new String(column.getValue());
  System.out.println(row + "\t\t" + familyName + ":" + columnName
  + "\t" + columnValue);
  }
  }
  }
  // 根据表名,查询全表(不包含版本信息---版本时间戳)
  public static void get(String tableName) throws Exception {
  HTable table = new HTable(config, tableName);
  ResultScanner scanner = table.getScanner(new Scan());
  Iterator itr = scanner.iterator();
  System.out.println("ROW\t\t\tCOLUMN:CELL");
  while (itr.hasNext()) {
  Result result = itr.next();
  String rowValue = new String(result.getRow());
  String tabCount = rowValue.length() >= 8 ? "\t" : "\t\t";
  NavigableMap map = result.getNoVersionMap();
  for (Entry entry : map.entrySet()) {
  String family = new String(entry.getKey());
  NavigableMap navigableMap = entry.getValue();
  Set set = navigableMap.entrySet();
  for (Entry en : set) {
  System.out.println(rowValue + tabCount + "\t" + family
  + ":" + new String(en.getKey()) + "\t" + new String(en.getValue()));
  }
  }
  }
  }
  // 根据表名,查询全表(包含版本信息---版本时间戳)
  public static void get(String tableName, boolean containsTimeStamp)
  throws Exception {
  if (!containsTimeStamp) {
  get(tableName);
  } else {
  HTable table = new HTable(config, tableName);
  Scan scan = new Scan();
  ResultScanner resultScanner = table.getScanner(scan);
  Iterator resultItr = resultScanner.iterator();
  System.out.println("ROW\t\t\tCOLUMN:CELL");
  while (resultItr.hasNext()) {
  Result result = resultItr.next();
  String rowValue = new String(result.getRow());
  String tabCount = rowValue.length() >= 8 ? "\t" : "\t\t";
  NavigableMap map                         = result.getMap();
  Set                         set = map.entrySet();
  for (Entry entry                     : set) {
  String familyName = new String(entry.getKey());
  NavigableMap columnMap =                                     entry.getValue();
  Set columnSet =                                     columnMap.entrySet();
  for (Entry ent : columnSet) {
  String columnName = new String(ent.getKey());
  NavigableMap valueMap = ent.getValue();
  Set valueSet = valueMap.entrySet();
  for (Entry e : valueSet) {
  Long timeStamp = e.getKey();
  String columnValue = new String(e.getValue());
  System.out.println(rowValue + tabCount+ "\tcolumn=" +                                             familyName + ":"+ columnName + "\ttimestamp="
  + String.valueOf(timeStamp) + "\tvalue="+ columnValue);
  }
  }
  }
  }
  }
  }
  // 删除表中某一行
  public static void deleteRow(String tableName, String row, String family,
  Long timestamp) throws Exception {
  HTable table = new HTable(config, tableName);
  Delete delete = new Delete(Bytes.toBytes(row));
  if (null == timestamp) {
  table.delete(delete.deleteFamily(Bytes.toBytes(family)));
  } else {
  table.delete(delete.deleteFamily(Bytes.toBytes(family),timestamp.longValue()));
  }
  }
  // 删除表中某一行的某个字段
  public static void deleteColumn(String tableName, String row,
  String family, String column, Long timestamp) throws Exception {
  HTable table = new HTable(config, tableName);
  Delete delete = new Delete(Bytes.toBytes(row));
  if (null == timestamp) {
  table.delete(delete.deleteColumn(Bytes.toBytes(family),Bytes.toBytes(column)));
  } else {
  table.delete(delete.deleteColumn(Bytes.toBytes(family),
  Bytes.toBytes(column), timestamp.longValue()));
  }
  }
  // 删除某个表
  public static boolean deleteTable(String tableName) throws Exception {
  HBaseAdmin admin = new HBaseAdmin(config);
  if (admin.tableExists(tableName)) {
  admin.disableTable(tableName);
  admin.deleteTable(tableName);
  return true;
  }
  return false;
  }
  public static void main(String[] args) throws Exception {
  get("heroes", "1");
  }
  }

页: [1]
查看完整版本: HBbaseUtils(HBbase shell的java代码实现)