ClassCastException:org.apache.hadoop.io.LongWritable无法强制转换为org.apache.hadoop.hbase.io.ImmutableBytesWritable

ClassCastException:org.apache.hadoop.io.LongWritable无法强制转换为org.apache.hadoop.hbase.io.ImmutableBytesWritable,hadoop,mapreduce,hbase,Hadoop,Mapreduce,Hbase,我已经将一个表从Hbase导出到一个类似org.apache.hadoop.mapreduce.lib.output.TextOutputFormat的文件中,为了导入导出的文本格式文件,我调整了从开源导入的代码,以支持导入基于文本的文件,而不是SequenceFile。 setInputFormatClass(TextInputFormat.class) 在运行导入类时,我得到以下异常 java.lang.ClassCastException: org.apache.hadoop.io.Lon

我已经将一个表从Hbase导出到一个类似org.apache.hadoop.mapreduce.lib.output.TextOutputFormat的文件中,为了导入导出的文本格式文件,我调整了从开源导入的代码,以支持导入基于文本的文件,而不是SequenceFile。 setInputFormatClass(TextInputFormat.class)

在运行导入类时,我得到以下异常

java.lang.ClassCastException: org.apache.hadoop.io.LongWritable cannot be cast to org.apache.hadoop.hbase.io.ImmutableBytesWritable
    at Import$Importer.map(Import.java:1)
    at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:144)
    at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:764)
    at org.apache.hadoop.mapred.MapTask.run(MapTask.java:370)
    at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:212)
这是我的导出类,经过调整,可以将内容从ExpoterTable写入文件

public class Export
{
private static final Log LOG = LogFactory.getLog(Export.class);

final static String NAME = "export";

final static String RAW_SCAN = "hbase.mapreduce.include.deleted.rows";

private static OutputStream out;

private static final String utf8 = "UTF-8";

private static final byte[] newline;

private static final byte[] keyValueSeparator;

static {
    try {
        newline = "\n".getBytes(utf8);
        keyValueSeparator = "\t".getBytes(utf8);
    }
    catch (UnsupportedEncodingException uee) {
        throw new IllegalArgumentException("can't find " + utf8 + " encoding");
    }
}

/**
 * Mapper.
 */
static class ExporterTable extends TableMapper<ImmutableBytesWritable, Result>
{
    /**
     * @param row  The current table row key.
     * @param value  The columns.
     * @param context  The current context.
     * @throws IOException When something is broken with the data.
     * @see org.apache.hadoop.mapreduce.Mapper#map(KEYIN, VALUEIN,
     *   org.apache.hadoop.mapreduce.Mapper.Context)
     */
    @Override
    public void map(ImmutableBytesWritable row, Result value, Context context) throws IOException {
        try {
            context.write(row, value);
            write(row, value);
            System.out.println(row);
            System.out.println(value);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

/**
 * Sets up the actual job.
 *
 * @param conf  The current configuration.
 * @param args  The command line parameters.
 * @return The newly created job.
 * @throws IOException When setting up the job fails.
 */
public static Job createSubmittableJob(Configuration conf, String[] args) throws IOException {
    String tableName = args[0];
    // this.out = new DataOutputStream(fos);
    Path outputDir = new Path(args[1]);
    Job job = new Job(conf, NAME + "_" + tableName);
    job.setJobName(NAME + "_" + tableName);
    job.setJarByClass(ExporterTable.class);
    // Set optional scan parameters
    Scan s = getConfiguredScanForJob(conf, args);
    TableMapReduceUtil.initTableMapperJob(tableName, s, ExporterTable.class, ImmutableBytesWritable.class, IntWritable.class, job);
    // No reducers.  Just write straight to output files.
    job.setNumReduceTasks(0);
    job.setOutputValueClass(Text.class);
    //  FileOutputFormat.setOutputPath(job, outputDir);
    job.setOutputFormatClass(NullOutputFormat.class);
    TableMapReduceUtil.addHBaseDependencyJars(conf);
    TableMapReduceUtil.addDependencyJars(conf, JsonProcessingException.class);
    TableMapReduceUtil.addDependencyJars(job);
    return job;
}

private static Scan getConfiguredScanForJob(Configuration conf, String[] args) throws IOException {
    Scan s = new Scan();
    // Optional arguments.
    // Set Scan Versions
    int versions = args.length > 2 ? Integer.parseInt(args[2]) : 1;
    s.setMaxVersions(versions);
    // Set Scan Range
    long startTime = args.length > 3 ? Long.parseLong(args[3]) : 0L;
    long endTime = args.length > 4 ? Long.parseLong(args[4]) : Long.MAX_VALUE;
    s.setTimeRange(startTime, endTime);
    // Set cache blocks
    s.setCacheBlocks(false);
    // Set Scan Column Family
    boolean raw = Boolean.parseBoolean(conf.get(RAW_SCAN));
    if (raw) {
        s.setRaw(raw);
    }

    if (conf.get(TableInputFormat.SCAN_COLUMN_FAMILY) != null) {
        s.addFamily(Bytes.toBytes(conf.get(TableInputFormat.SCAN_COLUMN_FAMILY)));
    }
    // Set RowFilter or Prefix Filter if applicable.
    Filter exportFilter = getExportFilter(args);
    if (exportFilter != null) {
        LOG.info("Setting Scan Filter for Export.");
        s.setFilter(exportFilter);
    }
    LOG.info("versions=" + versions + ", starttime=" + startTime + ", endtime=" + endTime + ", keepDeletedCells=" + raw);
    return s;
}

private static Filter getExportFilter(String[] args) {
    Filter exportFilter = null;
    String filterCriteria = (args.length > 5) ? args[5] : null;
    if (filterCriteria == null)
        return null;
    if (filterCriteria.startsWith("^")) {
        String regexPattern = filterCriteria.substring(1, filterCriteria.length());
        exportFilter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator(regexPattern));
    }
    else {
        exportFilter = new PrefixFilter(Bytes.toBytes(filterCriteria));
    }
    return exportFilter;
}

/*
 * @param errorMsg Error message. Can be null.
 */
private static void usage(final String errorMsg) {
    if (errorMsg != null && errorMsg.length() > 0) {
        System.err.println("ERROR: " + errorMsg);
    }
    System.err.println("Usage: Export [-D <property=value>]* <tablename> <outputdir> [<versions> " + "[<starttime> [<endtime>]] [^[regex pattern] or [Prefix] to filter]]\n");
    System.err.println("  Note: -D properties will be applied to the conf used. ");
    System.err.println("  For example: ");
    System.err.println("   -D mapred.output.compress=true");
    System.err.println("   -D mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec");
    System.err.println("   -D mapred.output.compression.type=BLOCK");
    System.err.println("  Additionally, the following SCAN properties can be specified");
    System.err.println("  to control/limit what is exported..");
    System.err.println("   -D " + TableInputFormat.SCAN_COLUMN_FAMILY + "=<familyName>");
    System.err.println("   -D " + RAW_SCAN + "=true");
    System.err.println("For performance consider the following properties:\n" + "   -Dhbase.client.scanner.caching=100\n" + "   -Dmapred.map.tasks.speculative.execution=false\n" + "   -Dmapred.reduce.tasks.speculative.execution=false");
}

/**
 * Main entry point.
 *
 * @param args  The command line parameters.
 * @throws Exception When running the job fails.
 */
public static void main(String[] args) throws Exception {
    Configuration conf = HBaseConfiguration.create();
    conf.set("mapreduce.framework.name", "local");
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    if (otherArgs.length < 2) {
        usage("Wrong number of arguments: " + otherArgs.length);
        System.exit(-1);
    }
    boolean jobStatus = false;
    Job job = createSubmittableJob(conf, otherArgs);
    try {
        File f = new File("Test");
        out = new FileOutputStream(f);
        jobStatus = job.waitForCompletion(true);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        IOUtils.closeStream(out);
    }

  //  convertTextToSequence(conf);

    System.exit(jobStatus ? 0 : 1);
}



public  static void write(ImmutableBytesWritable key, Result value) throws IOException {

    boolean nullKey = key == null;
    boolean nullValue = value == null;
    if (nullKey && nullValue) {
        return;
    }
    if (!nullKey) {
        writeObject(key);
    }
    if (!(nullKey || nullValue)) {
        out.write(keyValueSeparator);
    }
    if (!nullValue) {
        writeObject(value);
    }
    out.write(newline);
}

/**
 * Write the object to the byte stream, handling Text as a special
 * case.
 * @param o the object to print
 * @throws IOException if the write throws, we pass it on
 */
private static void writeObject(Object o) throws IOException {
    if (o instanceof Text) {
        Text to = (Text) o;
        out.write(to.getBytes(), 0, to.getLength());
    }
    else {
        out.write(o.toString().getBytes(utf8));
    }
}
公共类导出
{
私有静态最终日志日志=LogFactory.getLog(Export.class);
最终静态字符串NAME=“export”;
最终静态字符串RAW\u SCAN=“hbase.mapreduce.include.deleted.rows”;
私有静态输出流输出;
私有静态最终字符串utf8=“UTF-8”;
私有静态最终字节[]换行符;
私有静态最终字节[]keyValueSeparator;
静止的{
试一试{
换行符=“\n”.getBytes(utf8);
keyValueSeparator=“\t”.getBytes(utf8);
}
捕获(不支持的编码异常uee){
抛出新的IllegalArgumentException(“找不到”+utf8+“编码”);
}
}
/**
*制图员。
*/
静态类ExporterTable扩展了TableMapper
{
/**
*@param row当前表行键。
*@param为列指定值。
*@param context当前上下文。
*@在数据出现问题时抛出IOException。
*@see org.apache.hadoop.mapreduce.Mapper#map(KEYIN,VALUEIN,
*(org.apache.hadoop.mapreduce.Mapper.Context)
*/
@凌驾
公共void映射(ImmutableBytesWritable行、结果值、上下文)引发IOException{
试一试{
context.write(行、值);
写入(行、值);
系统输出打印项次(行);
系统输出打印项次(值);
}
捕捉(中断异常e){
e、 printStackTrace();
}
}
}
/**
*设置实际作业。
*
*@param conf当前配置。
*@param指定命令行参数。
*@返回新创建的作业。
*@设置作业失败时引发IOException。
*/
公共静态作业createSubmittableJob(配置配置配置,字符串[]args)引发IOException{
字符串tableName=args[0];
//this.out=新数据输出流(fos);
Path outputDir=新路径(args[1]);
作业作业=新作业(conf,NAME+“”+tableName);
job.setJobName(NAME+“”+tableName);
job.setJarByClass(ExporterTable.class);
//设置可选的扫描参数
Scan s=getConfiguredScanForJob(conf,args);
TableMapReduceUtil.initTableMapperJob(tableName,s,ExporterTable.class,ImmutableBytesWritable.class,IntWritable.class,job);
//没有还原程序。只需直接写入输出文件。
job.setNumReduceTasks(0);
job.setOutputValueClass(Text.class);
//setOutputPath(作业,outputDir);
setOutputFormatClass(NullOutputFormat.class);
TableMapReduceUtil.addHBaseDependencyJars(配置);
TableMapReduceUtil.addDependencyJars(conf,JsonProcessingException.class);
TableMapReduceUtil.addDependencyJars(作业);
返回工作;
}
私有静态扫描getConfiguredScanForJob(配置配置,字符串[]args)引发IOException{
扫描s=新扫描();
//可选参数。
//设置扫描版本
int versions=args.length>2?Integer.parseInt(args[2]):1;
s、 setMaxVersions(版本);
//设置扫描范围
long startTime=args.length>3?long.parseLong(args[3]):0L;
long-endTime=args.length>4?long.parseLong(args[4]):long.MAX_值;
s、 设置时间范围(开始时间、结束时间);
//设置缓存块
s、 设置缓存块(假);
//设置扫描列族
boolean raw=boolean.parseBoolean(conf.get(raw_SCAN));
如果(未加工){
s、 setRaw(raw);
}
if(conf.get(TableInputFormat.SCAN\u COLUMN\u FAMILY)!=null){
s、 addFamily(Bytes.toBytes(conf.get(TableInputFormat.SCAN_COLUMN_FAMILY));
}
//设置行筛选器或前缀筛选器(如果适用)。
Filter exportFilter=getExportFilter(args);
if(exportFilter!=null){
LOG.info(“为导出设置扫描过滤器”);
s、 设置过滤器(导出过滤器);
}
LOG.info(“versions=“+versions+”,starttime=“+starttime+”,endtime=“+endtime+”,keepDeletedCells=“+raw”);
返回s;
}
专用静态筛选器getExportFilter(字符串[]args){
Filter exportFilter=null;
字符串filterCriteria=(args.length>5)?args[5]:null;
如果(filterCriteria==null)
返回null;
if(filterCriteria.startsWith(“^”)){
字符串regexpatern=filterCriteria.substring(1,filterCriteria.length());
exportFilter=new RowFilter(CompareOp.EQUAL,new RegexStringComparator(regexpatern));
}
否则{
exportFilter=新的前缀过滤器(Bytes.toBytes(filterCriteria));
}
返回输出滤波器;
}
/*
*@param errorMsg错误消息。可以为空。
*/
私有静态void用法(最终字符串errorMsg){
如果(errorMsg!=null&&errorMsg.length()>0){
System.err.println(“错误:+errorMsg”);
}
System.err.println(“用法:导出[-D]*[“+”[[]][^[regex模式]或[Prefix]到过滤器]]\n”);
System.err.println(“注意:-D属性将应用于所使用的配置。”);
System.err.println(“例如:”);
System.err.println(“-D mapred.output.compress=true”);
System.err.println(“-D mapred.output.compression.codec=org.apache.hadoop.io.compress.gzip代码”);
System.err.println(“-D mapred.output.compression.type=BLOCK”);
System.err.println(“另外,可以指定以下扫描属性”);
System.err.println(“控制/限制导出内容”);
System.err.println(“-D”+TableInputFormat.SCAN\u COLUMN\u FAMILY+“=”);
System.err.println(“-D”+原始扫描+”=true”);
系统。
public void map(ImmutableBytesWritable row, Result value, Context context) 
    throws IOException {
        try {
            context.write(row, value);
job.setMapOutputKeyClass(ImmutableBytesWritable.class);
job.setMapOutputValueClass(Result.class);