分享

hbase-0.90.2中创建表、插入数据,更新数据,删除数据实例

pig2 2014-4-25 20:12:09 发表于 实操演练 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 8 35447
问题导读
删除表应该注意什么问题?
如何创建列族?
如何插入数据?
如何更新用户信息与关系型数据库更新的区别在什么地方?
hbase通过什么标准来区别两条记录是否相同?






所需要的包有:

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);



       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");
对于更新里面:这里面我们需要知道是否是我们关系数据库中所理解的更新:我们看下面内容:
hbase是以rowkey,column,timestamp这三个维度来区分的。
即如果两条记录其rowkey,column,timestamp一样的话,那么hbase就会认为其是相同的数据。
  1.          row     column   value   time
  2. put      r1     cf:c1      '5'       10
  3. put      r1     cf:c1      '6'       10
  4. put      r1     cf:c2      '7'  
  5. put      r1     af:c2      '8'
  6. put      r2     cf:c1      '9'
复制代码
如上所示首先插入一条数据其值为5,然后又插入一条数据其值为6.
此时用客户端接口取到的都是value=‘6’的数据,这可以认为是对原数据的一个覆盖,即可以认为是更新。






      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");
    }

}

已有(8)人评论

跳转到指定楼层
sunshine_junge 发表于 2014-6-29 10:36:09
回复

使用道具 举报

ascentzhen 发表于 2014-8-6 10:51:00
好,写的比较详细,学习了,赞一个
回复

使用道具 举报

xiaohao 发表于 2014-9-3 20:02:03
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
        at org.apache.hadoop.conf.Configuration.<clinit>(Configuration.java:142)
        at www.aboutyun.com.HbaseAddEdtDel.<clinit>(HbaseAddEdtDel.java:18)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 2 more
Exception in thread "main"
回复

使用道具 举报

xiaohao 发表于 2014-9-3 20:02:57
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
        at org.apache.hadoop.conf.Configuration.<clinit>(Configuration.java:142)
        at www.aboutyun.com.HbaseAddEdtDel.<clinit>(HbaseAddEdtDel.java:18)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 2 more
Exception in thread "main"

大神这个错误时什么问题就指教???
回复

使用道具 举报

pig2 发表于 2014-9-3 20:03:30
xiaohao 发表于 2014-9-3 20:02
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
        at org.apache.hadoop.conf.Co ...

应该是log4j有问题了,找不到引用到包
回复

使用道具 举报

hery 发表于 2014-9-6 18:44:37
正好需要的资料。。。。
回复

使用道具 举报

xiaohao 发表于 2014-10-9 10:42:22
pig2 发表于 2014-9-3 20:03
应该是log4j有问题了,找不到引用到包

我用的是log4j-1.2.16.jar包,还是不行,我就是不懂下面这句话是什么意思?14/10/09 10:39:15 WARN client.ZooKeeperSaslClient: SecurityException: java.lang.SecurityException: 无法定位登录配置 occurred when trying to find JAAS configuration.
14/10/09 10:39:15 INFO client.ZooKeeperSaslClient: Client will not SASL-authenticate because the default JAAS configuration section 'Client' could not be found. If you are not using SASL, you may ignore this. On the other hand, if you expected SASL to work, please fix your JAAS configuration.

回复

使用道具 举报

buildhappy 发表于 2014-10-24 14:16:17
你好,我现在想把网络中的数据流,保存到HBase中。(其实就是下载一个apk软件,保存到HBase中)
能不能用Mapreduce进行并行的处理啊?
谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条