user2007861
user2007861

Reputation: 427

How to use newAPIHadoopRDD (spark) in Java to read Hbase data

I try to read Hbase data with spark API.

The code :

    // Define SparkContext
    SparkConf sparkConf = new SparkConf().setAppName("Spark-Hbase").setMaster("master");
    sparkConf.set("XXX", "XXX");
    JavaSparkContext jsc = new JavaSparkContext(sparkConf);

    // Conf with Hbase
    Configuration conf = HBaseConfiguration.create();

    // Read data using spark
    JavaPairRDD<ImmutableBytesWritable, Result> hBaseRDD = 
            jsc.newAPIHadoopRDD(conf, TableInputFormat.class, ImmutableBytesWritable.class, Result.class);

The problem is on newAPIHadoopRDD method. I have this error, and I don't understand.

Bound mismatch: The generic method newAPIHadoopRDD(Configuration, Class<F>, Class<K>, Class<V>) of type JavaSparkContext is not applicable for the arguments (Configuration, Class<TableInputFormat>, Class<ImmutableBytesWritable>, Class<Result>). The inferred type TableInputFormat is not a valid substitute for the bounded parameter <F extends InputFormat<K,V>>

How to correct this ?

Upvotes: 3

Views: 7508

Answers (2)

Ram Ghadiyaram
Ram Ghadiyaram

Reputation: 29165

you can follow the below example

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableInputFormat;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;

public class HBaseReader {
    public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set(TableInputFormat.INPUT_TABLE, "table_name");
        JavaSparkContext jsc = new JavaSparkContext(new SparkConf());
        JavaPairRDD<ImmutableBytesWritable, Result> source = jsc
                .newAPIHadoopRDD(conf, TableInputFormat.class,
                        ImmutableBytesWritable.class, Result.class);
        Result result = Result.EMPTY_RESULT;

    }
}

Upvotes: 3

ankursingh1000
ankursingh1000

Reputation: 1419

you have to use InputFormat for newAPIHadoopRDD

public class MyInputFormat extends InputFormat<NullWritable, Record> { }

Record will be of any type extends writable

 interface Record extends Writable {}

Upvotes: 0

Related Questions