hbase 访问zookeeper的问题 坑

1、 Configuration config = HBaseConfiguration.create(); 书上说是会自动加载hbase-site.xml里面配置的zookeeper 配置

但是实际上是访问的是localhost本地的zookeeper2181端口

2、因此如果本地不是zookeeper server 的话就无法访问hbase 了

3、解决办法通过参数传给程序

例子:

public static class CountReducer extends Reducer {
        private Text TABLE_NAME = new Text();
        private Text ZOOKEEPER = new Text();
        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            // 读取配置
            Configuration conf = context.getConfiguration();
            TABLE_NAME = new Text(conf.get("TABLE_NAME"));
            ZOOKEEPER = new Text(conf.get("hbase.zookeeper.quorum"));
            super.setup(context);
        }
        protected void reduce(Text text, Iterable values,Context context) throws IOException, InterruptedException {
            Configuration config = HBaseConfiguration.create();
            config.set("hbase.zookeeper.quorum",ZOOKEEPER.toString());
            HTable table = new HTable(config, Bytes.toBytes(TABLE_NAME.toString()));
            String cuid = text.toString();
            long start = System.currentTimeMillis();
            Map userInfo = getUserInfo(table, cuid);
            //long costParseProfile = System.currentTimeMillis() - start;
            if (userInfo == null) {
                //System.err.println("cuid " + cuid + " not found cost timeis:"+costParseProfile);
                return ;
            }
            String value="";
            for (Map.Entry user : userInfo.entrySet()) {
                System.out.println(cuid + " " + user.getKey() + " " + user.getValue());
                value += user.getKey()+":"+user.getValue()+"\t";
            }
           // long costParseProfile2 = System.currentTimeMillis() - start;
           // System.err.println("time cost :"+costParseProfile2+"  cuid is:"+cuid);
            context.write(text, new Text(value));
        }


    }

@Override
    public int run(String[] args) throws Exception {
        List other_args = new ArrayList();
        for (int i = 0; i < args.length; ++i) {
            other_args.add(args[i]);
        }
        //String tname = other_args.get(3);
        String strReduceNum = other_args.get(4);
       // System.out.println("table name is:"+tname);
        //System.out.println("reducenum is:"+strReduceNum);
        int reduceNum = Integer.parseInt(strReduceNum);
        Configuration rawConf = getConf();
        Configuration conf = HBaseConfiguration.create(rawConf);
        conf.set("TABLE_NAME",other_args.get(3));
        conf.set("hbase.zookeeper.quorum",other_args.get(5));
        TableMapReduceUtil.addHBaseDependencyJars(conf);
        Job job = Job.getInstance(conf, "HBaseBatchCuidProf:"+other_args.get(2));
        job.setJarByClass(HBaseBatchCuidProf.class);


你可能感兴趣的:(hadoop,java)