欢迎来到魔据教育大数据学院,专注大数据工程师培养!
当前位置:首页 > 学习资料 > 讲师博文 > 几个常用HBase API说明

几个常用HBase API说明

时间:2017-08-17 18:20:47作者:商莹

 

在用HBase新版本时,总会出现一些被划上横线,标识过期的方法。所以在小编介绍几个常用API之前,先给出新旧版本下一些常用方法的使用。
说明:小编的旧版本HBase0.94,小编的新版本HBase1.2.6
一、常用新旧版本HBase API对照:
1. 创建HBaseAdmin 实例
旧:HBaseAdmin admin = new HBaseAdmin(conf);
新:Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
2. HTableDescriptor
旧:HTableDescriptor desc = new HTableDescriptor(Bytes.toBytes("testtable"));
新:HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("GoodsOrders"));
3. admin.deleteTable
旧:admin.deleteTable(Bytes.toBytes("testtable"));
新:admin.deleteTable(TableName.valueOf("testtable"));
4. admin.disableTable
旧:admin.disableTable(Bytes.toBytes("testtable"));
新:admin.disableTable(TableName.valueOf("testtable"));
5. admin.isTableAvailable
旧:admin.isTableAvailable(Bytes.toBytes("testtable"));
新:admin.isTableAvailable(TableName.valueOf("testtable"));
6. admin.isTableAvailable
旧:admin.isTableEnabled(Bytes.toBytes("testtable"));
新:admin.isTableEnabled(TableName.valueOf("testtable"));
7. Table取值
旧: HTable table = new HTable(conf, "testtable");
新:Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("testtable"));
8. Result取值
旧:for (KeyValue kv : result.raw()) {Bytes.toString(kv.getRow()));}
新:for(Cell cell:result.rawCells()){new String(CellUtil.cloneRow(cell)));}
二、常用HBase API功能:
1. 读取一条记录的几中方法
(1)for (KeyValue kv : result.raw()) {
System.out.print("行健: "+ Bytes.toString(kv.getRow()));
System.out.print("列簇: "+ Bytes.toString(kv.getFamily()));
System.out.print(" 列: "+ Bytes.toString(kv.getQualifier()));
System.out.print(" 值: "+ Bytes.toString(kv.getValue()));
System.out.println("时间戳: "+kv.getTimestamp());
      }
(2)for(Cell cell:result.rawCells()){
System.out.print("行健: "+new String(CellUtil.cloneRow(cell)));
System.out.print("列簇: "+new String(CellUtil.cloneFamily(cell)));
System.out.print(" 列: "+new String(CellUtil.cloneQualifier(cell)));
System.out.print(" 值: "+new String(CellUtil.cloneValue(cell)));
System.out.println("时间戳: "+cell.getTimestamp());
}
(3)for (Cell cell : cells) {
System.out.print(new String(cell.getRowArray()).substring(cell.getRowOffset(),
cell.getRowOffset() + cell.getRowLength()));
System.out.print(new String(cell.getFamilyArray()).substring(cell.getFamilyOffset(),
cell.getFamilyOffset()+ cell.getFamilyLength()));
System.out.print(new String(cell.getFamilyArray()).substring(
cell.getQualifierOffset(),cell.getQualifierOffset() + cell.getQualifierLength()));
System.out.println(new String(new String(cell.getValueArray()) + " "+ cell.getValueOffset() + " "+ cell.getValueLength()));
System.out.print(cell.getTimestamp());
}
2. 建表的方法
Configuration conf = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("tablename"));
HColumnDescriptor coldef = new HColumnDescriptor(Bytes.toBytes("family1"));
desc.addFamily(coldef);
admin.createTable(desc);
boolean avail = admin.isTableAvailable(TableName.valueOf("tablename "));
System.out.println(avail);
3. 列举指定表结构
HTableDescriptor htd = admin.getTableDescriptor(TableName.valueOf("tablename "));
System.out.println(htd);
4. 修改表结构
HTableDescriptor htd1 = admin.getTableDescriptor(name);
HColumnDescriptor coldef = new HColumnDescriptor(Bytes.toBytes("colfamily"));
htd1.addFamily(coldef);
htd1.setMaxFileSize(1024 * 1024 * 1024L);
admin.disableTable(name);
admin.modifyTable(name, htd1);
admin.enableTable(name);
5. 批处理
List<Row> batch = new ArrayList<Row>();
batch.add(put);
batch.add(get1);
batch.add(delete);
batch.add(get2);
Object[] results = new Object[batch.size()];
table.batch(batch, results);
6. 删除操作
Admin admin = connection.getAdmin();
TableName tablename = TableName.valueOf("GoodsOrders");
if(admin.tableExists(tablename)) {
admin.disableTable(tablename);
HTableDescriptor newtd = admin.getTableDescriptor(tablename);
newtd.removeFamily(Bytes.toBytes("books"));
HColumnDescriptor newhcd = new HColumnDescriptor("addfamilytest");
newhcd.setMaxVersions(10);
newhcd.setKeepDeletedCells(KeepDeletedCells.TRUE);
newtd.addFamily(newhcd);
admin.modifyTable(Bytes.toBytes("rd_ns:itable"),newtd);
admin.modifyTable(tablename,newtd);
admin.enableTable(tablename);
admin.close();
7. Put读数据
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("testtable7"));
Put put = new Put(Bytes.toBytes("row1"));
put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),Bytes.toBytes("val1"));
put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"),Bytes.toBytes("val2"));
table.put(put);
 
List<Put> puts = new ArrayList<Put>(); // co PutListExample-1-CreateList Create a list that holds the Put instances.
Put put1 = new Put(Bytes.toBytes("row1"));
put1.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),Bytes.toBytes("val1"));
put1.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
Bytes.toBytes("val1"));
puts.add(put1); // co PutListExample-2-AddPut1 Add put to list.
Put put2 = new Put(Bytes.toBytes("row2"));
put2.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),Bytes.toBytes("val2"));
puts.add(put2);
table.put(puts);
 
8. Get读数据
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("testtable1"));
 
Get get = new Get(Bytes.toBytes("row1"));
get.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"));
Result result = table.get(get);
byte[] val = result.getValue(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"));
System.out.println("Value: " + Bytes.toString(val));
 
List<Get> gets = new ArrayList<Get>();
    Get get1 = new Get(row1);
    get1.addColumn(cf1, qf1);
    gets.add(get1);
    Get get2 = new Get(row2);
    get2.addColumn(cf1, qf1);
    gets.add(get2);
 
    Result[] results = table.get(gets);
9. Scan读数据
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(beginRowKey));
scan.setStopRow(Bytes.toBytes(endRowKey));
scan.setMaxVersions(1);
scan.setCaching(20);
scan.setBatch(10);
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
ResultScanner rs = table.getScanner(scan);
10. Filter读数据
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("testtable"));
Filter filter1 = new FamilyFilter(CompareFilter.CompareOp.LESS,
new BinaryComparator(Bytes.toBytes("colfam3")));
Scan scan = new Scan();
scan.setFilter(filter1);
ResultScanner scanner = table.getScanner(scan);
11. MapReduce On HBase举例
public static class Map extends  Mapper<LongWritable, Text, Text, IntWritable> {   
    private final static IntWritable one = new IntWritable(1);   
    private Text word = new Text();   
    public void map(LongWritable key, Text value, Context context)  throws Exception {   
      StringTokenizer itr = new StringTokenizer(value.toString());   
      while (itr.hasMoreTokens()) {   
         word.set(itr.nextToken());   
         context.write(word, one);   
      }   
    }   
  }   
   
public static class Reduce extends  TableReducer<Text, IntWritable, NullWritable> {   
   public void reduce(Text key, Iterable<IntWritable> values,   
        Context context) throws IOException, InterruptedException {   
   int sum = 0;   
   Iterator<IntWritable> iterator = values.iterator();   
      while (iterator.hasNext()) {   
       sum += iterator.next().get();   
      }   
   Put put = new Put(Bytes.toBytes(key.toString()));   
   put.add(Bytes.toBytes("content"), Bytes.toBytes("count"), Bytes.toBytes(String.valueOf(sum)));   
   context.write(NullWritable.get(), put);   
    }   
  }   
   
  public static void createHBaseTable(String tableName) throws IOException {   
    HTableDescriptor htd = new HTableDescriptor(tableName);   
    HColumnDescriptor col = new HColumnDescriptor("content");   
    htd.addFamily(col);   
    Configuration conf = HBaseConfiguration.create();   
    HBaseAdmin hAdmin = new HBaseAdmin(conf);   
       if (hAdmin.tableExists(tableName)) {   
       hAdmin.disableTable(tableName);   
    hAdmin.deleteTable(tableName);   
    }   
    hAdmin.createTable(htd);   
  }   
   
  public static void main(String[] args) throws Exception {   
    String tableName = "wordcount1";   
    WordCountHBase_old.createHBaseTable(tableName);   
    Configuration conf = new Configuration();   
    conf.set("mapred.job.tracker", "IP地址:9001");   
    conf.set(TableOutputFormat.OUTPUT_TABLE, tableName);   
    Job job = new Job(conf, "New Word Count");   
    job.setJarByClass(WordCountHBase_old.class);   
    job.setMapperClass(Map.class);   
    job.setReducerClass(Reduce.class);   
    job.setMapOutputKeyClass(Text.class);   
    job.setMapOutputValueClass(IntWritable.class);   
    job.setInputFormatClass(TextInputFormat.class);   
    job.setOutputFormatClass(TableOutputFormat.class);   
    FileInputFormat.addInputPath(job, new Path("hdfs://IP地址:8020/input/"));
    System.exit(job.waitForCompletion(true) ? 0 : 1);   
   
  }   
小编今天先分享到这里。

更多大数据相关资讯敬请关注魔据教育,为您分享最及时的大数据资讯。
学习大数据敬请关注魔据教育微信二维码。
魔据教育微信二维码

【版权与免责声明】如发现内容存在版权问题,烦请提供相关信息发邮件至kefu@mojuedu.com,我们将及时沟通与处理。本站内容除非来源注明魔据教育,否则均为网友转载,涉及言论、版权与本站无关。

全国咨询热线:18501996998,值班手机:18501996998(7*24小时)

在线咨询:张老师QQ 320169340

企业合作服务专线:010-82340234-821, 院校合作洽谈专线:010-82340234

Copyright 2001-2019 魔据教育 - 北京华育兴业科技有限公司 版权所有,京ICP备17018991号-2

免费在线咨询立即咨询

免费索取技术资料立即索取

大数据技术交流QQ:226594285

电话咨询010-82340234