将JavaPairRDD <immutablebyteswritable,result =“”>转换为JavaRDD <string>

时间:2017-12-20 16:07:43

标签: java apache-spark hbase rdd java-pair-rdd

我正在尝试使用Apache Spark从HBase读取数据。我只想扫描一个特定的列。我正在创建我的HBase数据的RDD,如下所示

SparkConf sparkConf = new SparkConf().setAppName("HBaseRead").setMaster("local[2]");
JavaSparkContext sc = new JavaSparkContext(sparkConf);
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "localhost:2181");

String tableName = "myTable";

conf.set(TableInputFormat.INPUT_TABLE, tableName);
conf.set(TableInputFormat.SCAN_COLUMN_FAMILY, "myCol");


 JavaPairRDD<ImmutableBytesWritable, Result> hBaseRDD = sc.newAPIHadoopRDD(conf, TableInputFormat.class,
        ImmutableBytesWritable.class, Result.class);

以下是我要将JavaPairRDD转换为JavaRDD字符串的位置。

JavaRDD<String> rdd = ...

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以使用JavaRDD<String>函数获取map,如下所示。

import org.apache.spark.api.java.function.Function;
import org.apache.hadoop.hbase.util.Bytes;
import scala.Tuple2;

JavaRDD<String> javaRDD = javaPairRdd.map(new Function<Tuple2<ImmutableBytesWritable,Result>, String>() {
    @Override
    public String call(Tuple2<ImmutableBytesWritable, Result> tuple) throws Exception {
        Result result = tuple._2;
        String rowKey = Bytes.toString(result.getRow());//row key
        String fName = Bytes.toString(result.getValue(Bytes.toBytes("myColumnFamily"), Bytes.toBytes("firstName")));//firstName column 
        return fName;
    }       
});