我正在尝试使用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 = ...
我怎样才能做到这一点?
答案 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;
}
});