日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
基于Java的HBase客戶端編程

本文以HBase 0.90.2為例,介紹如何在Windows系統(tǒng),Eclipse IDE集成環(huán)境下,使用Java語(yǔ)言,進(jìn)行HBase客戶端編程,包含建立表、刪除表、插入記錄、刪除記錄、各種方式下的查詢操作等。

海滄網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,海滄網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為海滄千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營(yíng)銷網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的海滄做網(wǎng)站的公司定做!

1. 準(zhǔn)備工作

  1. 下載后安裝jdk包(這里使用的是jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008);
  2. 下載eclipse,解壓到本地(這里使用的是eclipse-java-helios-SR2-win32);
  3. 下載HBase包,解壓安裝包到本地(這里使用的是hbase-0.90.2)。

2. 搭建開(kāi)發(fā)環(huán)境

  1. 運(yùn)行Eclipse,創(chuàng)建一個(gè)新的Java工程“HBaseClient”,右鍵項(xiàng)目根目錄,選擇 “Properties”->“Java Build Path”->“Library”->“Add External JARs”,將HBase解壓后根目錄下的hbase-0.90.2.jar、hbase-0.90.2-tests.jar和lib子目錄下所有jar 包添加到本工程的Classpath下。
  2. 按照步驟1中的操作,將自己所連接的HBase的配置文件hbase-site.xml添加到本工程的Classpath中,如下所示為配置文件的一個(gè)示例:
  3.   
      
      
    1. hbase.rootdir
    2. hdfs://hostname:9000/hbase
    3. hbase.cluster.distributed
    4. true
    5. hbase.zookeeper.quorum
    6. *.*.*.*, *.*.*.*, *.*.*.*
    7. hbase.defaults.for.version
    8. 0.90.2
  4. 下面可以在Eclipse環(huán)境下進(jìn)行HBase編程了。

3. HBase基本操作代碼示例

3.1 初始化配置

 
 
 
  1. private static Configuration conf = null;
  2. /**
  3.  * 初始化配置
  4.  */
  5. static {
  6.     conf = HBaseConfiguration.create();
  7. }

3.2 創(chuàng)建表

 
 
 
  1. /**
  2.  * 創(chuàng)建表操作
  3.  * @throws IOException
  4.  */
  5. public void createTable(String tablename, String[] cfs) throws IOException {
  6.     HBaseAdmin admin = new HBaseAdmin(conf);
  7.     if (admin.tableExists(tablename)) {
  8.         System.out.println("表已經(jīng)存在!");
  9.     }
  10.     else {
  11.         HTableDescriptor tableDesc = new HTableDescriptor(tablename);
  12.         for (int i = 0; i < cfs.length; i++) {
  13.             tableDesc.addFamily(new HColumnDescriptor(cfs[i]));
  14.         }
  15.         admin.createTable(tableDesc);
  16.         System.out.println("表創(chuàng)建成功!");
  17.     }
  18. }

3.3 刪除表

 
 
 
  1. /**
  2.  * 刪除表操作
  3.  * @param tablename
  4.  * @throws IOException
  5.  */
  6. public void deleteTable(String tablename) throws IOException {
  7.     try {
  8.         HBaseAdmin admin = new HBaseAdmin(conf);
  9.         admin.disableTable(tablename);
  10.         admin.deleteTable(tablename);
  11.         System.out.println("表刪除成功!");
  12.     } catch (MasterNotRunningException e) {
  13.         e.printStackTrace();
  14.     } catch (ZooKeeperConnectionException e) {
  15.         e.printStackTrace();
  16.     }
  17. }

3.4 插入一行記錄

 
 
 
  1. /**
  2.  * 插入一行記錄
  3.  * @param tablename
  4.  * @param cfs
  5.  */
  6. public void writeRow(String tablename, String[] cfs) {
  7.     try {
  8.         HTable table = new HTable(conf, tablename);
  9.         Put put = new Put(Bytes.toBytes("rows1"));
  10.         for (int j = 0; j < cfs.length; j++) {
  11.             put.add(Bytes.toBytes(cfs[j]),
  12.                     Bytes.toBytes(String.valueOf(1)),
  13.                     Bytes.toBytes("value_1"));
  14.             table.put(put);
  15.         }
  16.     } catch (IOException e) {
  17.         e.printStackTrace();
  18.     }
  19. }

3.5 刪除一行記錄

 
 
 
  1. /**
  2.  * 刪除一行記錄
  3.  * @param tablename
  4.  * @param rowkey
  5.  * @throws IOException
  6.  */
  7. public void deleteRow(String tablename, String rowkey) throws IOException {
  8.     HTable table = new HTable(conf, tablename);
  9.     List list = new ArrayList();
  10.     Delete d1 = new Delete(rowkey.getBytes());
  11.     list.add(d1);
  12.     table.delete(list);
  13.     System.out.println("刪除行成功!");
  14. }

3.6 查找一行記錄

 
 
 
  1. /**
  2.  * 查找一行記錄
  3.  * @param tablename
  4.  * @param rowkey
  5.  */
  6. public static void selectRow(String tablename, String rowKey)
  7.         throws IOException {
  8.     HTable table = new HTable(conf, tablename);
  9.     Get g = new Get(rowKey.getBytes());
  10.     Result rs = table.get(g);
  11.     for (KeyValue kv : rs.raw()) {
  12.         System.out.print(new String(kv.getRow()) + "  ");
  13.         System.out.print(new String(kv.getFamily()) + ":");
  14.         System.out.print(new String(kv.getQualifier()) + "  ");
  15.         System.out.print(kv.getTimestamp() + "  ");
  16.         System.out.println(new String(kv.getValue()));
  17.     }
  18. }

3.7 查詢表中所有行

 
 
 
  1. /**
  2.  * 查詢表中所有行
  3.  * @param tablename
  4.  */
  5. public void scaner(String tablename) {
  6.     try {
  7.         HTable table = new HTable(conf, tablename);
  8.         Scan s = new Scan();
  9.         ResultScanner rs = table.getScanner(s);
  10.         for (Result r : rs) {
  11.             KeyValue[] kv = r.raw();
  12.             for (int i = 0; i < kv.length; i++) {
  13.                 System.out.print(new String(kv[i].getRow()) + "  ");
  14.                 System.out.print(new String(kv[i].getFamily()) + ":");
  15.                 System.out.print(new String(kv[i].getQualifier()) + "  ");
  16.                 System.out.print(kv[i].getTimestamp() + "  ");
  17.                 System.out.println(new String(kv[i].getValue()));
  18.             }
  19.         }
  20.     } catch (IOException e) {
  21.         e.printStackTrace();
  22.     }
  23. }

網(wǎng)站標(biāo)題:基于Java的HBase客戶端編程
地址分享:http://www.5511xx.com/article/dhodspd.html