Hbase深入(一)

2017-11-03  本文已影响0人  Gallin_Qiu

Hbase 架构

image.png
读取数据的顺序

HBase架构剖析

HBase java API 使用

<groupId>org.aphche.hbase</groupId>
   <artifactId>hbase-server</artifactId>
   <version>${hbase.version}</version>
</dependency>

<dependency>
<groupId>org.aphche.hbase</groupId>
   <artifactId>hbase-client</artifactId>
   <version>${hbase.version}</version>
</dependency>
hdfs-site.xml
core-site.xml
hbase-site.xml
public class HBaseOperation{

    public static getHTableByTableName(string tableName) throws Exception {
        Configuration configuration =HBaseConfiguration.create();
        HTable table=new HTable(configuration ,tableName);
        return table;
  }
    public static void main(string[] args){
        String tablename='user';
        HTable table=getHTableByTableName(tableName);
        Put put =new put(Bytes.toBytes("10004"));
        put.add(Bytes.toBytes("info"),
        put.add(Bytes.toBytes("name"),
        put.add(Bytes.toBytes("gallin")
);
        Get get =new get (Bytes.toBytes("10002"));
        result result=table.get(get);
        for (Cell cell :result.rawCells(){
        system.out.println(
            Bytes.tostring(cellUtil.cloneFamily(cell))+":"
            +Bytes.tostring(cellUtil.cloneQualifier(cell))+"->"
            Bytes.tostring(cellUtil.cloneValue(cell));

}
        table.close()
  }
}

以上命令需要为速记,为get查询和put插入
scan

    public static void main(string[] args) throws Exception{
       String tablename='user';
       HTable table=null;
       try{
            table=getHTableByTableName(tableName);
       Scan scan=new Scan();
//Range 
       scan.setStartRow(Bytes.toBytes("10001"));
       scan.setStopRow(Bytes.toBytes("10003"));
       scan.setFilter(filter);//比较麻烦,可能会减慢速度
       for(result result:resulrScanner){
             system.out.println(Bytes.tostring(result.getRow()));
             system.out.println(result);
}
}catch(Exception e ){
         e.printStackTrace();
}finally {
         IOUtils.closeStream(resultScanner);
         IOUtils.closeStream(table);
}
}
上一篇 下一篇

猜你喜欢

热点阅读