立即注册 登录
About云-梭伦科技 返回首页

pig2的个人空间 https://www.aboutyun.com/?61 [收藏] [复制] [分享] [RSS]

日志

hbase编程:Eclipse远程连接创建hbase表以及填充列与列数据

已有 969 次阅读2014-8-7 15:07

问题导读
1.如何通过Java ap创建表?
2.如何对已存在的表插入列以及列值?









远程创建表与数据,这是在hbase开发环境搭建及运行hbase小实例(HBase 0.98.3新api)基础上一个基础,我们创建表之后,该如何填充数据,

我们看下面代码.首先实例化HTable 
HTable table = new HTable(conf, Bytes.toBytes(tableName));// HTabel负责跟记录相关的操作如增删改查等 
HTable 表已经存在,我们该如何填充列,如何填充列族
(1)获取所有列

HColumnDescriptor[] columnFamilies = table.getTableDescriptor() // 获取所有的列族
(2)获取所有列之后,如何添加列
put.add(Bytes.toBytes(familyName),Bytes.toBytes(column1[j]), Bytes.toBytes(value1[j]));
上面三个字段:分别为列族名,列名、列值

那么下面函数具体是怎么实现的那?
1.首先是通过createTable(); 创建blog表,blog表中有一个列族article
2.我们要插入的是
列名 title
列值AboutYunArticle





看到上面该如何实现,下面贴上代码

  1. package www.aboutyun.com;

  2. import java.io.IOException;

  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.HBaseConfiguration;
  5. import org.apache.hadoop.hbase.HColumnDescriptor;
  6. import org.apache.hadoop.hbase.HTableDescriptor;
  7. import org.apache.hadoop.hbase.MasterNotRunningException;
  8. import org.apache.hadoop.hbase.TableName;
  9. import org.apache.hadoop.hbase.ZooKeeperConnectionException;
  10. import org.apache.hadoop.hbase.client.HBaseAdmin;
  11. import org.apache.hadoop.hbase.client.HTable;
  12. import org.apache.hadoop.hbase.client.Put;
  13. import org.apache.hadoop.hbase.util.Bytes;

  14. public class testHbase {

  15.         private static Configuration conf = null;
  16.         static {
  17.                 conf = HBaseConfiguration.create();
  18.                 conf.set("hbase.zookeeper.quorum", "master");// 使用eclipse时必须添加这个,否则无法定位master需要配置hosts
  19.                 conf.set("hbase.zookeeper.property.clientPort", "2181");
  20.         }

  21.         public static void main(String[] args) throws IOException {
  22.                 String[] cols = new String[1];
  23.                 String[] colsValue = new String[1];
  24.                 cols[0] = "title";
  25.                 colsValue[0] = "AboutYunArticle";

  26.                 // 创建表
  27.                 createTable();
  28.                 // 添加值
  29.                 addData("www.aboutyun.com", "blog", cols, colsValue);
  30.         }

  31.         private static void createTable() throws MasterNotRunningException,
  32.                         ZooKeeperConnectionException, IOException {

  33.                 HBaseAdmin admin = new HBaseAdmin(conf);// 新建一个数据库管理员//新api
  34.                 if (admin.tableExists(TableName.valueOf("LogTable"))) {
  35.                         System.out.println("table is not exist!");
  36.                         System.exit(0);
  37.                 } else {

  38.                         HTableDescriptor desc = new HTableDescriptor(
  39.                                         TableName.valueOf("blog"));
  40.                         desc.addFamily(new HColumnDescriptor("article"));

  41.                         admin.createTable(desc);
  42.                         admin.close();
  43.                         System.out.println("create table Success!");
  44.                 }
  45.         }

  46.         /*
  47.          * 为表添加数据(适合知道有多少列族的固定表)
  48.          * 
  49.          * @rowKey rowKey
  50.          * 
  51.          * @tableName 表名
  52.          * 
  53.          * @column1 第一个列族列表
  54.          * 
  55.          * @value1 第一个列的值的列表
  56.          *
  57.          */
  58.         private static void addData(String rowKey, String tableName,String[] column1, String[] value1) throws IOException {
  59.                 Put put = new Put(Bytes.toBytes(rowKey));// 设置rowkey
  60.                 HTable table = new HTable(conf, Bytes.toBytes(tableName));// HTabel负责跟记录相关的操作如增删改查等//
  61.                                                                                                                                         // 获取表
  62.                 HColumnDescriptor[] columnFamilies = table.getTableDescriptor() // 获取所有的列族
  63.                                 .getColumnFamilies();

  64.                 for (int i = 0; i < columnFamilies.length; i++) {
  65.                         String familyName = columnFamilies[i].getNameAsString(); // 获取列族名
  66.                         if (familyName.equals("article")) { // article列族put数据
  67.                                 for (int j = 0; j < column1.length; j++) {
  68.                                         put.add(Bytes.toBytes(familyName),
  69.                                                         Bytes.toBytes(column1[j]), Bytes.toBytes(value1[j]));
  70.                                 }
  71.                         }

  72.                 }
  73.                 table.put(put);
  74.                 System.out.println("add data Success!");
  75.         }

  76. }


下面为程序运行后结果
 

路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

关闭

推荐上一条 /2 下一条