2017-02-21 2 views
1

Ich möchte HBase über Spark mit JAVA zugreifen. Ich habe dafür neben this keine Beispiele gefunden. In der Antwort geschrieben wird,Lesen von Daten aus HBase mit Spark mit JAVA

Sie auch diese in Java

ich von How to read from hbase using spark kopiert diesen Code schreiben:

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() 
    } 
} 

Kann mir jemand ein paar Hinweise geben, wie man die richtige finden Abhängigkeiten, Objekte und Sachen?

Es scheint wie HBaseConfiguration ist in hbase-client, aber ich steckte tatsächlich auf TableInputFormat.INPUT_TABLE. Sollte das nicht in der gleichen Abhängigkeit sein?

Gibt es eine bessere Möglichkeit, mit Funken auf HBase zuzugreifen?

Antwort

0

TableInputFormat Klasse ist in hbase-server.jar und Sie müssten diese Abhängigkeit in Ihrer pom.xml hinzufügen. Bitte überprüfen Sie HBase and non-existent TableInputFormat in Spark Benutzerliste.

<dependency> 
    <groupId>org.apache.hbase</groupId> 
    <artifactId>hbase-server</artifactId> 
    <version>1.3.0</version> 
</dependency> 

Und unten ist der Beispielcode aus Hbase mit Spark zu lesen.

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(); 
    } 
} 
+0

Dank für den Beispielcode, ich bin dieses – monti

0

Ja. Es gibt. Verwenden Sie SparkOnHbase von Cloudera.

<dependency> 
    <groupId>org.apache.hbase</groupId> 
    <artifactId>hbase-spark</artifactId> 
    <version>1.2.0-cdh5.7.0</version> 
</dependency> 

Und die Verwendung HBase-Scandaten zu lesen, von Ihnen HBase Tabelle (oder Masse, wenn man die Schlüssel der Zeilen wissen Sie abrufen möchten).

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()) 
+0

sorry für die späte Antwort testen gehen, ich habe gerade versucht, 'hbase-spark' hinzuzufügen. Ich erkannte, dass die Artefakt-ID nicht in der Maven-Zentrale ist. Also habe ich 'https: // repository.cloudera.com/artifactory/cloudera-repos /' als Repository hinzugefügt. Es heißt immer noch 'Das POM für org.apache.hbase: hbase-spark: jar: 1.2.0-cdh5.7.0 fehlt, keine Abhängigkeitsinformationen verfügbar ', irgendein Vorschlag? – monti

Verwandte Themen