hbase中建立表、插入資料,更新資料,刪除資料

來源:互聯網
上載者:User

所需要的包有:

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");
        }
       
       
       //建立riapguh表
        System.out.println("建立 table");
        HTableDescriptor tableDescripter = new HTableDescriptor("riapguh".getBytes());//建立表
        tableDescripter.addFamily(new HColumnDescriptor("user"));//建立列簇user
        tableDescripter.addFamily(new HColumnDescriptor("dpt"));//建立列簇dpt
        admin.createTable(tableDescripter);
       
        HTable table = new HTable(configuration, "riapguh");
   
      //插入資料 
      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);
  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.