hadoop(九) - hbase shell命令及Java介面

來源:互聯網
上載者:User

標籤:java   指定   public   tde   標示符   zhang   scanner   ati   truncate   

一. shell命令

1. 進入hbase命令列  ./hbase shell


2. 顯示hbase中的表  list

3. 建立user表,包括info、data兩個列族
create ‘user‘, ‘info‘, ‘data‘
create ‘user‘, {NAME => ‘info‘, VERSIONS => ‘3‘}


4. 向表中插入資訊:

向user表中插入資訊。row key為rk0001。列族info中加入name列標示符,值為zhangsan
put ‘user‘, ‘rk0001‘, ‘info:name‘, ‘zhangsan‘

向user表中插入資訊。row key為rk0001。列族info中加入gender列標示符,值為female
put ‘user‘, ‘rk0001‘, ‘info:gender‘, ‘female‘

向user表中插入資訊。row key為rk0001,列族info中加入age列標示符。值為20
put ‘user‘, ‘rk0001‘, ‘info:age‘, 20

向user表中插入資訊,row key為rk0001,列族data中加入pic列標示符,值為picture
put ‘user‘, ‘rk0001‘, ‘data:pic‘, ‘picture‘


5. 擷取表中的資訊:

擷取user表中row key為rk0001的全部資訊
get ‘user‘, ‘rk0001‘

擷取user表中row key為rk0001,info列族的全部資訊
get ‘user‘, ‘rk0001‘, ‘info‘

擷取user表中row key為rk0001,info列族的name、age列標示符的資訊
get ‘user‘, ‘rk0001‘, ‘info:name‘, ‘info:age‘

擷取user表中row key為rk0001,info、data列族的資訊
get ‘user‘, ‘rk0001‘, ‘info‘, ‘data‘
get ‘user‘, ‘rk0001‘, {COLUMN => [‘info‘, ‘data‘]}
get ‘user‘, ‘rk0001‘, {COLUMN => [‘info:name‘, ‘data:pic‘]}

擷取user表中row key為rk0001,列族為info,版本最新5個的資訊
get ‘people‘, ‘rk0002‘, {COLUMN => ‘info‘, VERSIONS => 2}
get ‘user‘, ‘rk0001‘, {COLUMN => ‘info:name‘, VERSIONS => 5}
get ‘user‘, ‘rk0001‘, {COLUMN => ‘info:name‘, VERSIONS => 5, TIMERANGE => [1392368783980, 1392380169184]}

擷取user表中row key為rk0001。cell的值為zhangsan的資訊
get ‘people‘, ‘rk0001‘, {FILTER => "ValueFilter(=, ‘binary:圖片‘)"}

擷取user表中row key為rk0001,列標示符中含有a的資訊
get ‘people‘, ‘rk0001‘, {FILTER => "(QualifierFilter(=,‘substring:a‘))"}

put ‘user‘, ‘rk0002‘, ‘info:name‘, ‘fanbingbing‘
put ‘user‘, ‘rk0002‘, ‘info:gender‘, ‘female‘
put ‘user‘, ‘rk0002‘, ‘info:nationality‘, ‘中國‘
get ‘user‘, ‘rk0002‘, {FILTER => "ValueFilter(=, ‘binary:中國‘)"}


6. 查詢表中資訊:

查詢user表中的全部資訊
scan ‘user‘

查詢user表中列族為info的資訊
scan ‘people‘, {COLUMNS => ‘info‘}
scan ‘user‘, {COLUMNS => ‘info‘, RAW => true, VERSIONS => 5}
scan ‘persion‘, {COLUMNS => ‘info‘, RAW => true, VERSIONS => 3}
查詢user表中列族為info和data的資訊
scan ‘user‘, {COLUMNS => [‘info‘, ‘data‘]}
scan ‘user‘, {COLUMNS => [‘info:name‘, ‘data:pic‘]}

查詢user表中列族為info、列標示符為name的資訊
scan ‘user‘, {COLUMNS => ‘info:name‘}

查詢user表中列族為info、列標示符為name的資訊,而且版本號碼最新的5個
scan ‘user‘, {COLUMNS => ‘info:name‘, VERSIONS => 5}

查詢user表中列族為info和data且列標示符中含有a字元的資訊
scan ‘people‘, {COLUMNS => [‘info‘, ‘data‘], FILTER => "(QualifierFilter(=,‘substring:a‘))"}

查詢user表中列族為info。rk範圍是[rk0001, rk0003)的資料
scan ‘people‘, {COLUMNS => ‘info‘, STARTROW => ‘rk0001‘, ENDROW => ‘rk0003‘}

查詢user表中row key以rk字元開頭的
scan ‘user‘,{FILTER=>"PrefixFilter(‘rk‘)"}

查詢user表中指定範圍的資料
scan ‘user‘, {TIMERANGE => [1392368783980, 1392380169184]}

7. 刪除表中資料:
刪除user表row key為rk0001,列標示符為info:name的資料

delete ‘people‘, ‘rk0001‘, ‘info:name‘


刪除user表row key為rk0001,列標示符為info:name。timestamp為1392383705316的資料
delete ‘user‘, ‘rk0001‘, ‘info:name‘, 1392383705316

清空user表中的資料
truncate ‘people‘


8. 其它操作

改動表結構:
首先停用user表(新版本號碼不用)
disable ‘user‘

加入兩個列族f1和f2
alter ‘people‘, NAME => ‘f1‘

alter ‘user‘, NAME => ‘f2‘


啟用表enable ‘user‘


###disable ‘user‘(新版本號碼不用)
刪除一個列族:
alter ‘user‘, NAME => ‘f1‘, METHOD => ‘delete‘ 或 alter ‘user‘, ‘delete‘ => ‘f1‘

加入列族f1同一時候刪除列族f2
alter ‘user‘, {NAME => ‘f1‘}, {NAME => ‘f2‘, METHOD => ‘delete‘}

將user表的f1列族版本改為5
alter ‘people‘, NAME => ‘info‘, VERSIONS => 5
啟用表
enable ‘user‘

刪除表
disable ‘user‘

drop ‘user‘



二. java介面

public class HbaseDemo {private Configuration conf = null;@Beforepublic void init(){conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");}@Testpublic void testDrop() throws Exception{HBaseAdmin admin = new HBaseAdmin(conf);admin.disableTable("account");admin.deleteTable("account");admin.close();}@Testpublic void testPut() throws Exception{HTable table = new HTable(conf, "user");Put put = new Put(Bytes.toBytes("rk0003"));put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("liuyan"));table.put(put);table.close();}@Testpublic void testGet() throws Exception{HTable table = new HTable(conf, "user");Get get = new Get(Bytes.toBytes("rk0001"));get.setMaxVersions(5);Result result = table.get(get);for(KeyValue kv : result.list()){String family = new String(kv.getFamily());String qualifier = new String(kv.getQualifier());String value = new String(kv.getValue());System.out.println("family: " + ", qualifier: " + qualifier + ", value: " + value);}table.close();}@Testpublic void testScan() throws Exception{HTablePool pool = new HTablePool(conf, 10);HTableInterface table = pool.getTable("user");Scan scan = new Scan(Bytes.toBytes("rk0001"), Bytes.toBytes("rk0002"));scan.addFamily(Bytes.toBytes("info"));ResultScanner scanner = table.getScanner(scan);for(Result r : scanner){byte[] value = r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));System.out.println(new String(value));}pool.close();}@Testpublic void testDel() throws Exception{HTable table = new HTable(conf, "user");Delete del = new Delete(Bytes.toBytes("rk0001"));del.deleteColumn(Bytes.toBytes("data"), Bytes.toBytes("pic"));table.delete(del);table.close();}}




hadoop(九) - hbase shell命令及Java介面

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.