如何使用Eclipse构建HBase开发环境

来源:互联网 发布:独立软件供应商 编辑:程序博客网 时间:2024/05/21 22:30
  1. 环境需求
    Eclipse版本:eclipse-jee-mars-1
    操作系统:Ubuntu15.04
    Hadoop:1.2.1
    HBase:0.94.13

  2. 搭建过程
    运行Eclipse,创建一个新的Java工程“hbaseTest”,右键项目根目录,选择 “Properties”->“Java Build Path”->“Library”->“Add External JARs”,将HBase解压后根目录下的hbase-0.94.13-security.jar、hbase-0.94.13-security-tests.jar和lib子目录下所有jar 包添加到本工程的Classpath下。
    这里写图片描述
    工程根目录下创建文件夹conf,将hbase解压后根目录下的conf文件夹内的hbase-site.xml拷贝过来。
    这里写图片描述

  3. 测试

    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.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 putExample {    public static void main(String[] args) throws IOException {    Configuration conf = HBaseConfiguration.create();    HTable table =new HTable(conf, "testtable1");    Put put = new Put(Bytes.toBytes("row1"));    put.add(Bytes.toBytes("colfam1"),Bytes.toBytes("qual1"),Bytes.toBytes("val1"));    put.add(Bytes.toBytes("colfam1"),Bytes.toBytes("qual2"),Bytes.toBytes("val2"));    table.put(put);    }}

会出现如下错误:
WARN client.HConnectionManager$HConnectionImplementation: Encountered problems when prefetch META table:
org.apache.hadoop.hbase.TableNotFoundException: Cannot find row in .META. for table: testtable1, row=testtable1,,99999999999999

是因为没有创建表结构,所以代码应改为

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.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 putExample {    public static void main(String[] args) throws IOException {        Configuration conf = HBaseConfiguration.create();        HBaseAdmin hbase=new HBaseAdmin(conf);        HTableDescriptor desc=new HTableDescriptor("testtable");        HColumnDescriptor colfam1=new HColumnDescriptor("colfam1".getBytes());        desc.addFamily(colfam1);        hbase.createTable(desc);        HTable table =new HTable(conf, "testtable");        Put put = new Put(Bytes.toBytes("row1"));        put.add(Bytes.toBytes("colfam1"),Bytes.toBytes("qual1"),Bytes.toBytes("val1"));        put.add(Bytes.toBytes("colfam1"),Bytes.toBytes("qual2"),Bytes.toBytes("val2"));        table.put(put);        }}
0 0
原创粉丝点击