spark将数据写入hbase

atmip9wb  于 2021-06-09  发布在  Hbase
关注(0)|答案(1)|浏览(362)

我写了一个向hbase写入数据的演示,但是没有响应,没有错误,没有日志。我的hbase是0.98,hadoop2.3,spark 1.4。我在客户机模式下运行。你知道吗?谢谢。

  1. object SparkConnectHbase2 extends Serializable {
  2. def main(args: Array[String]) {
  3. new SparkConnectHbase2().toHbase();
  4. }
  5. }
  6. class SparkConnectHbase2 extends Serializable {
  7. def toHbase() {
  8. val conf = new SparkConf().setAppName("ljh_ml3");
  9. val sc = new SparkContext(conf)
  10. val tmp = sc.parallelize(Array(601, 701, 801, 901)).foreachPartition({ a =>
  11. val configuration = HBaseConfiguration.create();
  12. configuration.set("hbase.zookeeper.property.clientPort", "2181");
  13. configuration.set("hbase.zookeeper.quorum", 192.168.1.66");
  14. configuration.set("hbase.master", “192.168.1.66:60000");
  15. val table = new HTable(configuration, "ljh_test4");
  16. var put = new Put(Bytes.toBytes(a+""));
  17. put.add(Bytes.toBytes("f"), Bytes.toBytes("c"), Bytes.toBytes(a + "value"));
  18. table.put(put);
  19. table.flushCommits();
  20. })
  21. }
  22. }

谢谢。

tvokkenx

tvokkenx1#

写入hbase表

  1. import org.apache.hadoop.hbase.client.{HBaseAdmin, HTable, Put}
  2. import org.apache.hadoop.hbase.{HBaseConfiguration, HTableDescriptor, HColumnDescriptor, TableName}
  3. import org.apache.hadoop.hbase.mapreduce.TableInputFormat
  4. import org.apache.spark._
  5. val hconf = HBaseConfiguration.create()
  6. hconf.set(TableInputFormat.INPUT_TABLE, tablename)
  7. val admin = new HBaseAdmin(hconf)
  8. if(!admin.isTableAvailable(tablename)) {
  9. val tabledesc= new HTableDescriptor(tablename)
  10. tabledesc.addFamily(new HColumnDescriptor("cf1".getBytes()));
  11. admin.createTable(tabledesc)
  12. }
  13. val newtable= new HTable(hconf, tablename);
  14. val put = new Put(new String("row").getBytes());
  15. put .add("cf1".getBytes(), "col1".getBytes(), new String("data").getBytes());
  16. newtable.put(put);
  17. newtable.flushCommits();
  18. val hbaserdd = sc.newAPIHadoopRDD(hconf, classOf[TableInputFormat],
  19. classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable],
  20. classOf[org.apache.hadoop.hbase.client.Result])
展开查看全部

相关问题