歡迎來到魔據教育大數據學院,專注大數據工程師培養!
當前位置:首頁 > 學習資料 > 講師博文 > 幾個常用HBase API說明

幾個常用HBase API說明

時間:2017-08-17 18:20:47作者:[!--zuozhe--]

 

在用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);   
   
  }   
小編今天先分享到這里。

更多大數據相關資訊敬請關注魔據教育,為您分享最及時的大數據資訊。
學習大數據敬請關注魔據教育微信二維碼。
魔據教育微信二維碼

【版權與免責聲明】如發現內容存在版權問題,煩請提供相關信息發郵件至[email protected],我們將及時溝通與處理。本站內容除非來源注明魔據教育,否則均為網友轉載,涉及言論、版權與本站無關。

全國咨詢熱線:18501996998,值班手機:18501996998(7*24小時)

在線咨詢:張老師QQ 320169340

企業合作服務專線:010-82340234-821, 院校合作洽談專線:010-82340234

Copyright 2001-2019 魔據教育 - 北京華育興業科技有限公司 版權所有,京ICP備17018991號-2

免費在線咨詢立即咨詢

免費索取技術資料立即索取

大數據技術交流QQ:226594285

電話咨詢010-82340234

119期两码中特免费公开