日期:2014-05-16  浏览次数:20655 次

hbase-0.90.2中创建表、插入数据,更新数据,删除数据(
所需要的包有:

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(