溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》
  • 首頁 > 
  • 教程 > 
  • 服務器 > 
  • 云計算 > 
  • hbase中創(chuàng)建表、插入數(shù)據(jù),更新數(shù)據(jù),刪除數(shù)據(jù)的操作

hbase中創(chuàng)建表、插入數(shù)據(jù),更新數(shù)據(jù),刪除數(shù)據(jù)的操作

發(fā)布時間:2021-08-02 16:22:09 來源:億速云 閱讀:332 作者:chen 欄目:云計算

本篇內容介紹了“hbase中創(chuàng)建表、插入數(shù)據(jù),更新數(shù)據(jù),刪除數(shù)據(jù)的操作”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

所需要的包有:
commons-codec-1.4.jar
commons-logging-1.1.1.jar
hadoop-0.20.2-core.jar
hbase-0.90.2.jar
log4j-1.2.16.jar
zookeeper-3.3.2.jar
 
背景:
假設有一個不知道是干什么表:)
表里需要存入人員和其相對應的部門信息
 
代碼:

import java.util.ArrayList;import java.util.List;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.client.Delete;import org.apache.hadoop.hbase.client.HBaseAdmin;import org.apache.hadoop.hbase.client.HTable;import org.apache.hadoop.hbase.client.Put; public class HbaseAddEdtDel {  public static Configuration configuration=null;  static {  configuration = HBaseConfiguration.create();  configuration.set("hbase.master", "192.168.0.201:60000");  configuration.set("hbase.zookeeper.quorum","192.168.0.201,192.168.0.202,192.168.0.203");    configuration.set("hbase.zookeeper.property.clientPort", "2181");         }     public static void main(String[] args) throws Exception {            HBaseAdmin admin = new HBaseAdmin(configuration);                   if (admin.tableExists("riapguh")) {            System.out.println("刪除 table");            admin.disableTable("riapguh");            admin.deleteTable("riapguh");        }                     //創(chuàng)建riapguh表        System.out.println("創(chuàng)建 table");        HTableDescriptor tableDescripter = new HTableDescriptor("riapguh".getBytes());//創(chuàng)建表        tableDescripter.addFamily(new HColumnDescriptor("user"));//創(chuàng)建列簇user        tableDescripter.addFamily(new HColumnDescriptor("dpt"));//創(chuàng)建列簇dpt        admin.createTable(tableDescripter);               HTable table = new HTable(configuration, "riapguh");         //插入數(shù)據(jù)       System.out.println("add riapguh data");      List<Put> putuser = new ArrayList<Put>();                   Put user1 = new Put(new String("用戶A").getBytes());       //寫入用戶員信息       user1.add(new String("user").getBytes(), new String("user_code").getBytes(), new String("u_0001").getBytes());       user1.add(new String("user").getBytes(), new String("user_name").getBytes(), new String("u_用戶A").getBytes());             //寫入部門信息       user1.add(new String("dpt").getBytes(), new String("dpt_code").getBytes(), new String("d_001").getBytes());       user1.add(new String("dpt").getBytes(), new String("dpt_name").getBytes(), new String("d_部門A").getBytes());       putuser.add(user1);                     Put user2 = new Put(new String("用戶B").getBytes());       //寫入用戶員信息       user2.add(new String("user").getBytes(), new String("user_code").getBytes(), new String("u_0002").getBytes());       user2.add(new String("user").getBytes(), new String("user_name").getBytes(), new String("u_用戶B").getBytes());             //寫入部門信息       user2.add(new String("dpt").getBytes(), new String("dpt_code").getBytes(), new String("d_002").getBytes());       user2.add(new String("dpt").getBytes(), new String("dpt_name").getBytes(), new String("d_部門B").getBytes());       putuser.add(user2);                       Put user3 = new Put(new String("用戶C").getBytes());       //寫入用戶員信息       user3.add(new String("user").getBytes(), new String("user_code").getBytes(), new String("u_0003").getBytes());       user3.add(new String("user").getBytes(), new String("user_name").getBytes(), new String("u_用戶C").getBytes());             //寫入部門信息       user3.add(new String("dpt").getBytes(), new String("dpt_code").getBytes(), new String("d_003").getBytes());       user3.add(new String("dpt").getBytes(), new String("dpt_name").getBytes(), new String("d_部門C").getBytes());       putuser.add(user3);          table.put(putuser);      table.flushCommits();           //更新用戶B      Put updateb = new Put(new String("用戶B").getBytes());     //寫入用戶員信息      updateb.add(new String("user").getBytes(), new String("user_code").getBytes(), new String("u_000xsx").getBytes());      updateb.add(new String("user").getBytes(), new String("user_name").getBytes(), new String("u_用戶xsx").getBytes());        //寫入部門信息      updateb.add(new String("dpt").getBytes(), new String("dpt_code").getBytes(), new String("d_00xsx").getBytes());      updateb.add(new String("dpt").getBytes(), new String("dpt_name").getBytes(), new String("d_部門xsx").getBytes());      table.put(updateb);           table.flushCommits();      //HBaseBasic.selectByRowKey("riapguh");           System.out.println("-------------刪除用戶C---------------------");      //刪除用戶C      //able.delete(new Delete(new String("用戶C").getBytes()));      List<Delete> deld = new ArrayList<Delete>();      deld.add(new Delete(new String("用戶C").getBytes()));      table.delete(deld);           table.flushCommits();      //HBaseBasic.selectByRowKey("riapguh");    } }

“hbase中創(chuàng)建表、插入數(shù)據(jù),更新數(shù)據(jù),刪除數(shù)據(jù)的操作”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網(wǎng)站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI