最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

HBase常用Java API

2020-09-14 11:02 作者:自學(xué)Python的小姐姐呀  | 我要投稿


本節(jié)介紹與 HBase數(shù)據(jù)存儲(chǔ)管理相關(guān)的 Java API(基于 HBase 版本 1.2.3)。HBase 的常用Java API
HBase 主要包括 5 大類操作:HBase 的配置、HBase 表的管理、列族的管理、列的管理、數(shù)據(jù)操作等。
1)org.apache.hadoop.hbase.HBaseConfiguration

HBaseConfiguration 類用于管理 HBase 的配置信息,使用舉例如下。static Configuration cfg = HBaseConfiguration.create();
2)org.apache.hadoop.hbase.client.Admin

Admin 是 Java 接口類型,不能直接用該接口來(lái)實(shí)例化一個(gè)對(duì)象,而是必須通過(guò)調(diào)用 Connection.getAdmin() 方法,來(lái)調(diào)用返回子對(duì)象的成員方法。該接口用來(lái)管理 HBase 數(shù)據(jù)庫(kù)的表信息。它提供的方法包括創(chuàng)建表,刪除表,列出表項(xiàng),使表有效或無(wú)效,以及添加或刪除表列族成員等。

創(chuàng)建表使用的例子如下。

  1. Configuration configuration = HBaseConfiguration.create();

  2. Connection connection = ConnectionFactory.createConnection(configuration);

  3. Admin admin = connection.getAdmin();

  4. if(admin.tableExists(tableName)) {//如果存在要?jiǎng)?chuàng)建的表,那么先刪除,再創(chuàng)建

  5. admin.disableTable(tableName);

  6. admin.deleteTable(tableName);

  7. }

  8. admin.createTable(tableDescriptor);

  9. admin.disableTable(tableName);

  10. HColumnDescriptor hd =?new?HColumnDescriptor(columnFamily);

  11. admin.addColumn(tableName,hd);


3)org.apache.hadoop.hbase.HTableDescriptor

HTableDescriptor 包含了表的詳細(xì)信息。創(chuàng)建表時(shí)添加列族使用的例子如下。

  1. HTableDescriptor tableDescriptor =?new?HTableDescriptor (tableName);// 表的數(shù)據(jù)模式

  2. tableDescriptor. addFamily (new?HColumnDescriptor("name"));// 增加列族

  3. tableDescriptor.addFamily(new?HColumnDescriptor("age"));

  4. tableDescriptor.addFamily(new?HColumnDescriptor("gender"));

  5. admin.createTable(tableDescriptor);


4)org.apache.hadoop.hbase.HColumnDescriptor

HColumnDescriptor 類維護(hù)著關(guān)于列族的信息,如版本號(hào)、壓縮設(shè)置等。它通常在創(chuàng)建表或者為表添加列族的時(shí)候使用。列族被創(chuàng)建后不能直接修改,只能通過(guò)刪除然后重新創(chuàng)建的方式。列族被刪除的時(shí)候,列族里面的數(shù)據(jù)也會(huì)同時(shí)被刪除。創(chuàng)建表時(shí)添加列族的例子如下。

  1. HTableDescriptor tableDescriptor =?new?HTableDescriptor(tableName);// 表的數(shù)據(jù)模式

  2. HColumnDescriptor hcd = HColumnDescriptor("name".getBytes.());// 構(gòu)造列族

  3. hcd.setValue("firstName".getBytes(),"John".getBytes());//給列族添加列

  4. hcd.setValue("lastName".getBytes(),"Bates".getBytes());// 給列族添加列

  5. tableDescriptor.addFamily(hcd);//把列族加入表描述


5)org.apache.hadoop.hbase.client.Table

Table 是 Java 接口類型,不可以用 Table 直接實(shí)例化一個(gè)對(duì)象,而是必須通過(guò)調(diào)用 connection.getTable() 的一個(gè)子對(duì)象,來(lái)調(diào)用返回子對(duì)象的成員方法。這個(gè)接口可以用來(lái)和 HBase 表直接通信,可以從表中獲取數(shù)據(jù)、添加數(shù)據(jù)、刪除數(shù)據(jù)和掃描數(shù)據(jù)。例子如下。

  1. Configuration configuration = HBaseConfiguration.create();

  2. Connection connection = ConnectionFactory.createConnection(configuration);

  3. Table table = connection.getTable();

  4. ResultScanner scanner = table.getScanner(family);


6)org.apache.hadoop.hbase.client.Put

Put 類用來(lái)對(duì)單元執(zhí)行添加數(shù)據(jù)操作。給表里添加數(shù)據(jù)的例子如下。

  1. Configuration configuration = HBaseConfiguration.create();

  2. Connection connection = ConnectionFactory.createConnection(configuration);

  3. Table table = connection.getTable();

  4. Put put =?new?Put ("*1111".getBytes());//—個(gè) Put 代表一行數(shù)據(jù),行鍵為構(gòu)造方法中傳入的值

  5. put.addColumn("name".getBytes(),null,"Ghander".getBytes());//本行數(shù)據(jù)的第一列

  6. put.addColumn("age".getBytes(),null,"20".getBytes());// 本行數(shù)據(jù)的第二列

  7. put.addColumn("gender".getBytes(),null,"male".getBytes());// 本行數(shù)據(jù)的第三列

  8. put.add("score".getBytes(),"Math".getBytes(),"99".getBytes());// 本行數(shù)據(jù)的第四列

  9. table.put(put);


7)org.apache.hadoop.hbase.client.Get

Get 類用來(lái)獲取單行的數(shù)據(jù)。獲取指定單元的數(shù)據(jù)的例子如下。

  1. Configuration configuration = HBaseConfiguration.create();

  2. Connection connection = ConnectionFactory.createConnection(configuration);

  3. Table table = connection.getTable();

  4. Get g =?new?Get(rowKey.getBytes());

  5. Result rs = table.get(g);


8)org.apache.hadoop.hbase.client.Result

Result 類用來(lái)存放 Get 或 Scan 操作后的查詢結(jié)果,并以 <key,value〉的格式存儲(chǔ)在映射表中。
獲取指定單元的數(shù)據(jù)的例子如下。

  1. Configuration configuration = HBaseConfiguration.create();

  2. Connection connection = ConnectionFactory.createConnection(configuration);

  3. Table table = connection.getTable();

  4. Get g =?new?Get(rowKey.getBytes());

  5. Result.rs = table.get(g);

  6. for?(KeyValue kv : rs.raw()) {

  7. System.out.printIn("rowkey:"+new?String(kv.getRow()));

  8. System.out.printIn("Column Family:"+new?String(kv.getFamily()));

  9. System.out.printIn("Column :"+?new?String(kv.getQualifier()));

  10. System.out.printIn("value :"+?new?String(kv.getValue()));

  11. }


9)org.apache.hadoop.hbase.client.Scan

Scan 類可以用來(lái)限定需要查找的數(shù)據(jù),如版本號(hào)、起始行號(hào)、終止行號(hào)、列族、列限定符、返回值的數(shù)量的上限等。設(shè)置 Scan 的列族、時(shí)間戳的范圍和每次最多返回的單元數(shù)目的例子如下。

  1. Scan scan =?new?Scan();

  2. scan.addFamily(Bytes.toBytes("columnFamily1"));

  3. scan.setTimeRange(1,3);

  4. scan.setBatch(1000);



10)org.apache.hadoop.hbase.client.ResultScanner

ResultScanner 類是客戶端獲取值的接口,可以用來(lái)限定需要查找的數(shù)據(jù),如版本號(hào)、起始行號(hào)、終止行號(hào)、列族、列限定符、返回值的數(shù)量的上限等。獲取指定單元的數(shù)據(jù)的例子如下。

  1. Scan scan =?new?Scan();

  2. scan.addColumn(Bytes.toBytes("columnFamily1"),Bytes.toBytes("column1"));

  3. scan.setTimeRange(1,3);

  4. scan.setBatch(10);

  5. Configuration configuration = HBaseConfiguration.create();

  6. Connection connection = ConnectionFactory.createConnection(configuration);

  7. Table table = connection.getTable();

  8. try?{

  9. ResultScanner resuitScanner = table.getScanner(scan);

  10. Result rs = resultScanner.next();

  11. for?(; rs !=?null;rs = resultScanner.next()){

  12. for?(KeyValue kv : rs.list()){

  13. System.out.printIn("--------------");

  14. System.out.printIn("rowkey:"+?new?String(kv.getRow()));

  15. System.out.printIn("Column Family: "+?new?String(kv.getFamily()));

  16. System.out.printIn("Column :" +?new?String(kv.getQualifier ()));

  17. System.out.printIn("value :"+?new?String(kv.getValue()));

  18. }

  19. }

  20. }?catch?(IOException e) {

  21. e.printStackTrace();

  22. }


HBase常用Java API的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
高青县| 朝阳区| 巴东县| 禄丰县| 大埔县| 光山县| 灵璧县| 沈阳市| 西华县| 长宁县| 临潭县| 阳曲县| 依安县| 宁河县| 天柱县| 甘泉县| 六盘水市| 饶阳县| 旌德县| 陇川县| 苏尼特右旗| 灵石县| 新津县| 虞城县| 湾仔区| 高密市| 高陵县| 靖宇县| 揭阳市| 贡山| 义马市| 磐安县| 洪泽县| 珠海市| 绥棱县| 邓州市| 缙云县| 苗栗县| 清原| 怀仁县| 成都市|