• 使用eclipse开发hbase程序


     
    一:在eclipse创建一个普通的java项目
    二:新建一个文件夹,把hbase需要的jar放进去,我这里把hbase/lib/*.jar 下所有的jar都放进去了,最后发现就用到了下面三个jar包
    hadoop-common-2.6.4.jar
    hbase-common-1.2.4.jar
    hbase-client-1.2.4.jar

    三:
    将你在服务器中配置的hbase-site.xml文件复制到ecplise工程里(在工程的跟目录下新建一个文件夹命名conf,右键conf文件--->build path---->use as source folder)
     
     
     
    四:新创建一个测试类,就可以写代码了
    package com.zhang.insert;
    
    import java.io.IOException;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.Cell;
    import org.apache.hadoop.hbase.CellUtil;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.HColumnDescriptor;
    import org.apache.hadoop.hbase.HTableDescriptor;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.Admin;
    import org.apache.hadoop.hbase.client.Connection;
    import org.apache.hadoop.hbase.client.ConnectionFactory;
    import org.apache.hadoop.hbase.client.Get;
    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.client.Result;
    import org.apache.hadoop.hbase.util.Bytes;
    import org.junit.Test;
    
    public class HbaseInsert1 {
        public static Log log=LogFactory.getLog(HbaseInsert1.class);
        public static String tableName="employees";//表名
        public static String columnFamily="student_name";//列族
        public static String rowFamily="h2";//行键
        static Configuration conf=null;
        
        static{
            Configuration confi=new HBaseConfiguration().create();
            conf=confi;
             //指定zookeeper
            conf.set("hbase.zookeeper.quorum", "node4");
            System.setProperty("hadoop.home.dir", "E:\hadoop-2.6.4");//设置hadoop的安装目录,如果在eclipse已经设置过可以不用设置
            
        }
        /**
         * 创建表
         * HBaseAdmin 
         * HTableDescriptor
         */
        @Test
        public void testCreateTable(){
            
            HBaseAdmin admin=null;
            HTableDescriptor htd=null;
            try {
                 admin=new HBaseAdmin(conf);
                //判断要创建的表是否已经存在
                if(admin.tableExists(al_TableName)){
                    System.out.println(tableName+"表已经存在");
                    return;
                }else{
                    htd=new HTableDescriptor(al_TableName.getBytes());    
                    //设置列族
                    htd.addFamily(new HColumnDescriptor(columnFamily));
                    admin.createTable(htd);
                    System.out.println(tableName+"表创建成功!!!");
                }
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
        /**
         * 删除表
         */
        @Test
        public void testDeletTable(){
            HBaseAdmin admin=null;
            try {
                admin=new HBaseAdmin(conf);
                if(admin.tableExists(tableName)){
                    System.out.println("table is exits");
                    //如果表存在,则注为失效状态
                    admin.disableTable(tableName);//删除表
                    admin.deleteTable(tableName);
                    System.out.println("删除成功");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
     
    问题:
    1,在创建表的时候抛出异常
    java.io.IOException: Could not locate executable nullinwinutils.exe in the Hadoop binaries.
    这个异常是因为你缺少winutils.exe
    需要下载这个文件,然后放到你的/hadoop/bin/  即可
    2:
    2017-01-04 22:20:05,567 INFO [main-SendThread(127.0.0.1:2181)] zookeeper.ClientCnxn (ClientCnxn.java:logStartConnect(975)) - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
    下面异常是因为连接的zk不对,需要指定你的zk
    Configuration conf=new HBaseConfiguration().create();
    conf.set("hbase.zookeeper.quorum", "node4");//node4 是你的服务器的名称
     
    3:这个异常是我在创建表后,使用'scan'和'put'都会抛出下面错误
    ERROR: No server address listed in hbase:meta for region employess_info,,1483706778862.6d09a04dec7bfba654d6b393ba966534. containing row
     
    我使用 scan 'hbase:meta'查询hbase元数据
    ROW COLUMN+CELL
    hbase:namespace,,1482832588064.6899522395b4ee74 column=info:regioninfo, timestamp=1482832590934, value={ENCODED => 6899522395b4ee74d8a55be4b8f31fb9, NAME => 'hbase:namespace,,1482832588064.6
    d8a55be4b8f31fb9. 899522395b4ee74d8a55be4b8f31fb9.', STARTKEY => '', ENDKEY => ''}
    hbase:namespace,,1482832588064.6899522395b4ee74 column=info:seqnumDuringOpen, timestamp=1483554191715, value=x00x00x00x00x00x00x00x0D
    d8a55be4b8f31fb9.
    hbase:namespace,,1482832588064.6899522395b4ee74 column=info:server, timestamp=1483554191715, value=hadoop-node4.com:16201
    d8a55be4b8f31fb9.
    hbase:namespace,,1482832588064.6899522395b4ee74 column=info:serverstartcode, timestamp=1483554191715, value=1483554113504
    d8a55be4b8f31fb9.
    student,,1482855412525.d49d109d9fe387bdcda9f748 column=info:regioninfo, timestamp=1482855420324, value={ENCODED => d49d109d9fe387bdcda9f748f2d959e3, NAME => 'student,,1482855412525.d49d109d9
    f2d959e3. fe387bdcda9f748f2d959e3.', STARTKEY => '', ENDKEY => ''}
    student,,1482855412525.d49d109d9fe387bdcda9f748 column=info:seqnumDuringOpen, timestamp=1483554191720, value=x00x00x00x00x00x00x00x0D
    f2d959e3.
    student,,1482855412525.d49d109d9fe387bdcda9f748 column=info:server, timestamp=1483554191720, value=hadoop-node4.com:16201
    f2d959e3.
    student,,1482855412525.d49d109d9fe387bdcda9f748 column=info:serverstartcode, timestamp=1483554191720, value=1483554113504
    f2d959e3.
    student_1,,1483556027914.ded9aeece2edc985bf9999 column=info:regioninfo, timestamp=1483556033173, value={ENCODED => ded9aeece2edc985bf9999d0744fad1e, NAME => 'student_1,,1483556027914.ded9aee
    d0744fad1e. ce2edc985bf9999d0744fad1e.', STARTKEY => '', ENDKEY => ''}
    3 row(s) in 0.4820 seconds
     
    (这个异常是因为我的eclipse工程里没有hbase-site.xml文件,所以操作是habse默认的。因为我在创建表后,对表进行操作虽然报错,但是我重启集群之后,再对表进行操作就没问题了,所以我觉得是ecplise的问题,不是hbase的问题,查看hbase:meta数据也是正常的)
  • 相关阅读:
    前端程序员学好算法系列(七)二叉树和递归
    前端程序员学好算法系列(六)队列
    前端程序员学好算法系列(五)栈
    前端程序员学好算法系列(四)链表
    前端程序员学好算法系列(三)链表
    前端程序员学好算法系列(二)数组
    前端程序员学好算法系列(一)数组
    egg.js-基于koa2的node.js入门
    vuex所有核心概念完整解析State Getters Mutations Actions
    关于vue2用vue-cli搭建环境后域名代理的http-proxy-middleware解决api接口跨域问题
  • 原文地址:https://www.cnblogs.com/zhangXingSheng/p/6277257.html
Copyright © 2020-2023  润新知