日志
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表中有一个列族article2.我们要插入的是列名 title列值AboutYunArticle看到上面该如何实现,下面贴上代码- package www.aboutyun.com;
- import java.io.IOException;
- 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.MasterNotRunningException;
- import org.apache.hadoop.hbase.TableName;
- import org.apache.hadoop.hbase.ZooKeeperConnectionException;
- import org.apache.hadoop.hbase.client.HBaseAdmin;
- import org.apache.hadoop.hbase.client.HTable;
- import org.apache.hadoop.hbase.client.Put;
- import org.apache.hadoop.hbase.util.Bytes;
- public class testHbase {
- private static Configuration conf = null;
- static {
- conf = HBaseConfiguration.create();
- conf.set("hbase.zookeeper.quorum", "master");// 使用eclipse时必须添加这个,否则无法定位master需要配置hosts
- conf.set("hbase.zookeeper.property.clientPort", "2181");
- }
- public static void main(String[] args) throws IOException {
- String[] cols = new String[1];
- String[] colsValue = new String[1];
- cols[0] = "title";
- colsValue[0] = "AboutYunArticle";
- // 创建表
- createTable();
- // 添加值
- addData("www.aboutyun.com", "blog", cols, colsValue);
- }
- private static void createTable() throws MasterNotRunningException,
- ZooKeeperConnectionException, IOException {
- HBaseAdmin admin = new HBaseAdmin(conf);// 新建一个数据库管理员//新api
- if (admin.tableExists(TableName.valueOf("LogTable"))) {
- System.out.println("table is not exist!");
- System.exit(0);
- } else {
- HTableDescriptor desc = new HTableDescriptor(
- TableName.valueOf("blog"));
- desc.addFamily(new HColumnDescriptor("article"));
- admin.createTable(desc);
- admin.close();
- System.out.println("create table Success!");
- }
- }
- /*
- * 为表添加数据(适合知道有多少列族的固定表)
- *
- * @rowKey rowKey
- *
- * @tableName 表名
- *
- * @column1 第一个列族列表
- *
- * @value1 第一个列的值的列表
- *
- */
- private static void addData(String rowKey, String tableName,String[] column1, String[] value1) throws IOException {
- Put put = new Put(Bytes.toBytes(rowKey));// 设置rowkey
- HTable table = new HTable(conf, Bytes.toBytes(tableName));// HTabel负责跟记录相关的操作如增删改查等//
- // 获取表
- HColumnDescriptor[] columnFamilies = table.getTableDescriptor() // 获取所有的列族
- .getColumnFamilies();
- for (int i = 0; i < columnFamilies.length; i++) {
- String familyName = columnFamilies[i].getNameAsString(); // 获取列族名
- if (familyName.equals("article")) { // article列族put数据
- for (int j = 0; j < column1.length; j++) {
- put.add(Bytes.toBytes(familyName),
- Bytes.toBytes(column1[j]), Bytes.toBytes(value1[j]));
- }
- }
- }
- table.put(put);
- System.out.println("add data Success!");
- }
- }
下面为程序运行后结果
- 新手帮助
- 新手帮助:注册遇到问题,领取资源,加入铁粉群,不会使用搜索,如何获取积分等
查看 »