[英]read data from HBase by using Spark with JAVA
我想使用 JAVA 通过 Spark 访问 HBase。 除了这个,我还没有找到任何例子。 答案中写着,
你也可以用Java写这个
我从How to read from hbase using spark复制了这段代码:
import org.apache.hadoop.hbase.client.{HBaseAdmin, Result}
import org.apache.hadoop.hbase.{ HBaseConfiguration, HTableDescriptor }
import org.apache.hadoop.hbase.mapreduce.TableInputFormat
import org.apache.hadoop.hbase.io.ImmutableBytesWritable
import org.apache.spark._
object HBaseRead {
def main(args: Array[String]) {
val sparkConf = new SparkConf().setAppName("HBaseRead").setMaster("local[2]")
val sc = new SparkContext(sparkConf)
val conf = HBaseConfiguration.create()
val tableName = "table1"
System.setProperty("user.name", "hdfs")
System.setProperty("HADOOP_USER_NAME", "hdfs")
conf.set("hbase.master", "localhost:60000")
conf.setInt("timeout", 120000)
conf.set("hbase.zookeeper.quorum", "localhost")
conf.set("zookeeper.znode.parent", "/hbase-unsecure")
conf.set(TableInputFormat.INPUT_TABLE, tableName)
val admin = new HBaseAdmin(conf)
if (!admin.isTableAvailable(tableName)) {
val tableDesc = new HTableDescriptor(tableName)
admin.createTable(tableDesc)
}
val hBaseRDD = sc.newAPIHadoopRDD(conf, classOf[TableInputFormat], classOf[ImmutableBytesWritable], classOf[Result])
println("Number of Records found : " + hBaseRDD.count())
sc.stop()
}
}
谁能给我一些提示如何找到正确的依赖项、对象和东西?
似乎HBaseConfiguration
在hbase-client
中,但我实际上坚持使用TableInputFormat.INPUT_TABLE
。 这不应该在同一个依赖项中吗?
有没有更好的方法来使用 spark 访问 hbase?
TableInputFormat
类位于 hbase-server.jar 中,您需要在 pom.xml 中添加该依赖项。 请在 Spark 用户列表中检查HBase 和不存在的 TableInputFormat 。
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.0</version>
</dependency>
下面是使用 Spark 从 Hbase 读取的示例代码。
public static void main(String[] args) throws Exception {
SparkConf sparkConf = new SparkConf().setAppName("HBaseRead").setMaster("local[*]");
JavaSparkContext jsc = new JavaSparkContext(sparkConf);
Configuration hbaseConf = HBaseConfiguration.create();
hbaseConf.set(TableInputFormat.INPUT_TABLE, "my_table");
JavaPairRDD<ImmutableBytesWritable, Result> javaPairRdd = jsc.newAPIHadoopRDD(hbaseConf, TableInputFormat.class,ImmutableBytesWritable.class, Result.class);
jsc.stop();
}
}
是的。 有。 使用 Cloudera 的SparkOnHbase 。
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-spark</artifactId>
<version>1.2.0-cdh5.7.0</version>
</dependency>
并且使用 HBase 扫描从 HBase 表中读取数据(如果您知道要检索的行的键,则使用 Bulk Get)。
Configuration conf = HBaseConfiguration.create();
conf.addResource(new Path("/etc/hbase/conf/core-site.xml"));
conf.addResource(new Path("/etc/hbase/conf/hbase-site.xml"));
JavaHBaseContext hbaseContext = new JavaHBaseContext(jsc, conf);
Scan scan = new Scan();
scan.setCaching(100);
JavaRDD<Tuple2<byte[], List<Tuple3<byte[], byte[], byte[]>>>> hbaseRdd = hbaseContext.hbaseRDD(tableName, scan);
System.out.println("Number of Records found : " + hBaseRDD.count())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.