Пример вставки и выборки данных в Hbase на языке Scala для Spark 1.3
Пример кода:
При запуске может возникнуть такая ошибка:
Для этого нужно запускать приложение с указанием driver-class-path:
Полезные ссылки:
http://massapi.com/method/org/apache/hadoop/hbase/client/Table.flushCommits.html
https://wiki.apache.org/hadoop/Hbase/Scala
http://scalapersistenceframework.org/?p=91
http://jimbojw.com/wiki/index.php?title=Understanding_Hbase_and_BigTable
http://hbase.apache.org/book.html#datamodel
import org.apache.hadoop.hbase.{HBaseConfiguration, HTableDescriptor}
import org.apache.hadoop.hbase.client.HBaseAdmin
import org.apache.hadoop.hbase.mapreduce.TableInputFormat
import org.apache.hadoop.hbase.client.Result
import org.apache.hadoop.hbase.io.ImmutableBytesWritable
import org.apache.hadoop.hbase.mapreduce.TableInputFormat
import org.apache.hadoop.fs.Path
import org.apache.hadoop.hbase.client.{HBaseAdmin,HTable,Put,Get}
import org.apache.hadoop.hbase.util.Bytes
import org.apache.hadoop.hbase.client.HTable;
val conf = HBaseConfiguration.create()
conf.addResource(new Path("/etc/hbase/conf/hdfs-site.xml"));
conf.addResource(new Path("/etc/hbase/conf/core-site.xml"));
conf.addResource(new Path("/etc/hbase/conf/hbase-site.xml"));
conf.set("hbase.zookeeper.quorum", "127.0.0.1:2181")
conf.set(TableInputFormat.INPUT_TABLE, "users_registered")
val row = Bytes.toBytes("test_key.111")
val p = new Put(row)
val myTable = new HTable(conf, "users_registered");
p.add(Bytes.toBytes("data"), Bytes.toBytes("channel_new_id"), Bytes.toBytes("76756"))
myTable.put(p)
myTable.flushCommits
//Выборка данных по rowkey
val theget= new Get(Bytes.toBytes("test_key.111"))
val result=myTable.get(theget)
val res1 = result.getValue(Bytes.toBytes("data"), Bytes.toBytes("channel_new_id"))
println(Bytes.toString(res1))
При запуске может возникнуть такая ошибка:
ClassNotFoundException: org.apache.htrace.Trace exception
Для этого нужно запускать приложение с указанием driver-class-path:
spark-shell --master "local[2]" --driver-class-path /etc/hbase/conf:/opt/cloudera/parcels/CDH/lib/hbase/lib/htrace-core-3.1.0-incubating.jar
Полезные ссылки:
http://massapi.com/method/org/apache/hadoop/hbase/client/Table.flushCommits.html
https://wiki.apache.org/hadoop/Hbase/Scala
http://scalapersistenceframework.org/?p=91
http://jimbojw.com/wiki/index.php?title=Understanding_Hbase_and_BigTable
http://hbase.apache.org/book.html#datamodel
Коментарі
Дописати коментар