简体   繁体   English

HBase与MapReduce

[英]HBase with MapReduce

I have set up a HBase cluster over hadoop cluster where IPv6 is disabled in all nodes. 我已经在hadoop群集上建立了一个HBase群集,其中所有节点均禁用了IPv6。

Everything is running fine; 一切运行良好; I am able to run java client to access HBase using standard Put, Scan, Get, ... 我能够运行Java客户端以使用标准的Put,Scan,Get,...访问HBase ...

I wrote a map-reduce program to access HBase, But I have got the following error: 我编写了一个map-reduce程序来访问HBase,但是出现以下错误:

Exception in thread "main" java.lang.NullPointerException
at org.apache.hadoop.net.DNS.reverseDns(DNS.java:72)
at org.apache.hadoop.hbase.mapreduce.TableInputFormatBase.reverseDNS(TableInp...
at org.apache.hadoop.hbase.mapreduce.TableInputFormatBase.reverseDNS(TableInp...

This is my /etc/hosts file in all nodes: 这是我在所有节点中的/ etc / hosts文件:

127.0.0.1       localhost.localdomain   localhost
192.168.0.252   master.hadoop.com       master
192.168.0.251   slave.hadoop.com        slave

And this is my map-reduce client program: 这是我的map-reduce客户程序:

import java.io.IOException;
import java.util.StringTokenizer;

import java.net.InetAddress;
import org.apache.hadoop.net.DNS.*;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.fs.Path;

public class HBaseAndMapReduceExample {

  public static class MyMapper extends TableMapper<ImmutableBytesWritable, Put> {
    public void map(ImmutableBytesWritable row, Result value, Context context) throws IOException, InterruptedException {
      // this example is just copying the data from the source table...
      context.write(row, resultToPut(row,value));
    }

    private static Put resultToPut(ImmutableBytesWritable key, Result result) throws IOException {
      Put put = new Put(key.get());
      for (KeyValue kv : result.raw()) {
        put.add(kv);
      }

      return put;
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration config = HBaseConfiguration.create();
    Job job = new Job(config,"HBaseAndMapReduceExample");
    job.setJarByClass(HBaseAndMapReduceExample.class);  // class that contains mapper

    Scan scan = new Scan();
    scan.setCaching(500);  // 1 is the default in Scan, which will be bad for MapReduce jobs
    scan.setCacheBlocks(false);  // don't set to true for MR jobs
    // set other scan attrs

    TableMapReduceUtil.initTableMapperJob(
    "testtable",      // input table
    scan,             // Scan instance to control CF and attribute selection
    MyMapper.class,   // mapper class
    null,             // mapper output key
    null,             // mapper output value
    job);

    TableMapReduceUtil.initTableReducerJob(
    "testtable2",     // output table
    null,             // reducer class
    job);
    job.setNumReduceTasks(0);

    boolean b = job.waitForCompletion(true);
    if (!b) {
      throw new IOException("error with job!");
    }
  }
}

Also try adding your zookeeper address : 另外,尝试添加您的Zookeeper地址:

Configuration config = HBaseConfiguration.create();
   conf.setStrings("hbase.zookeeper.quorum",
            "your-zookeeper-addr");
Job job = new Job(config,"HBaseAndMapReduceExample");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM