加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

几个常用HBase API说明

(2017-09-12 09:44:31)
标签:

大数据

教育

文化

   在用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. 读取一条记录的几中方法

1for (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());

      }

2for(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());

}

3for (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 batch = new ArrayList();

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 puts = new ArrayList(); // 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 gets = new ArrayList();

    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 {   

    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 {   

   public void reduce(Text key, Iterable values,   

        Context context) throws IOException, InterruptedException {   

   int sum = 0;   

   Iterator 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);   

   

  }   

    想了解更多请查看http://www.mojuedu.com

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有