Hbase1.0.0实战(2)连接hbase数据库

来源:互联网 发布:ubuntu svn 库建立 编辑:程序博客网 时间:2024/06/06 08:56

环境搭好以后,就得开始干活了,不归路也得就这么走下去了。
用eclipse新建一个项目,把相关类库扔进去,果然不出所料,在打开这里写图片描述 时出现了这个错误:
这里写图片描述
找了很久也没找到问题在哪,请看到的朋友指导一下。不过这个错误不影响连接服务器,能够继续开发。
有一种开发方式叫做面向复制粘贴的编程,尝试了一下,效果还是比较好的。
参考了http://javacrazyer.iteye.com/blog/1186881 这篇文章,代码粘过去以后,跑起来是没有问题的。但原文用的是hbase-0.90.4,出现了这样的问题:
这里写图片描述
虽然不影响运行,但作为一个洁癖患者,这是不能忍的。
这里写图片描述
好在有一种编程模式叫做面向google的编程,我尝试了一下,找到的相关文献都停在0.94这个版本,很难找到1.0.1的,看来这种方法不靠谱。于是我忽略了英语渣的现实,愤而转战官网,官网提供了一个例子,是这么写的:

public static void createSchemaTables(Configuration config) {        try {            final Admin admin = new Admin(config);            HTableDescriptor table = new HTableDescriptor(                    TableName.valueOf(TABLE_NAME));            table.addFamily(new HColumnDescriptor(CF_DEFAULT)                    .setCompressionType(Algorithm.SNAPPY));            System.out.print("Creating table. ");            createOrOverwrite(admin, table);            System.out.println(" Done.");            admin.close();        } catch (Exception e) {            e.printStackTrace();            System.exit(-1);        }    }

于是我试了试,发现一个悲剧:

不能实例化类型 Admin

这里写图片描述
于是问google,google表示不知道,这对于面向复制粘贴编程的选手来说压力山大。下载源代码看看:

/** * The administrative API for HBase. Obtain an instance from an {@link Connection#getAdmin()} and * call {@link #close()} afterwards. * <p>Admin can be used to create, drop, list, enable and disable tables, add and drop table * column families and other administrative operations. * * @see ConnectionFactory * @see Connection * @see Table * @since 0.99.0 */@InterfaceAudience.Public@InterfaceStability.Evolvingpublic interface Admin extends Abortable, Closeable 

呵呵,Admin是个接口,作为一个官方文档,你让我实例化一个接口,你家里人知道吗?
这里写图片描述
洋装穿在身,羊驼在我心,作为一名不屈不挠的共产主义战士,我不能就这么放弃。终于,我发现了Connection#getAdmin(),顺藤摸瓜摸过去,找到了ConnectionFactory类,一切都和谐了。
这里写图片描述

代码如下:

    /**     * 建立服务器连接     */    public static Configuration configuration;//连接参数    static {        configuration = HBaseConfiguration.create();        configuration.set("hbase.zookeeper.property.clientPort", "2181");        configuration.set("hbase.zookeeper.quorum", "192.168.3.206");        configuration.set("hbase.master", "hdfs://192.168.3.206:16030");    }    public static void main(String[] args) throws IOException {        createTable("tableName");    }    /**     * 插入表     * @param tableName     *            需要插入的表名     */    public static void createTable(String tableName) {        System.out.println("start create table ......");        try {            Connection connect = ConnectionFactory                    .createConnection(configuration);// 建立连接            HTableDescriptor table = new HTableDescriptor(                    TableName.valueOf(tableName));            final Admin hBaseAdmin = connect.getAdmin();            if (hBaseAdmin.tableExists(table.getTableName())) {// 判断表是否存在                System.out.print("表已存在");            } else {                table.addFamily(new HColumnDescriptor("column1"));// 建立列族                table.addFamily(new HColumnDescriptor("column2"));                table.addFamily(new HColumnDescriptor("column3"));//测试用,所以建了仨,不过看了一些文献,都说多列族影响性能,建议一个就好                hBaseAdmin.createTable(table);                System.out.println("end create table ......");                hBaseAdmin.close();            }        } catch (MasterNotRunningException e) {            e.printStackTrace();        } catch (ZooKeeperConnectionException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }

这里写图片描述
成功。

对于新东西,看源代码还是很重要的,虽然看上去很烦。
这里写图片描述

0 0
原创粉丝点击