spark保存数据到hbase_用spark写数据到hbase

本帖最后由 remarkzhao 于 2017-7-19 14:24 编辑

请问各位大神这是什么情况。。

从网上看了一个例子   用spark向hbase写数据

抛出的异常:java.lang.IllegalArgumentException: Can not create a Path from a null string

用sbt打包的时候出现的warning:

[warn] there were four deprecation warnings; re-run with -deprecation for details

[warn] one warning found

[warn] Multiple main classes detected.  Run 'show discoveredMainClasses' to see the list  (我在里面写了2个scala,另一个scala能正常运行并已成功)

PS:hbase的student表能成功写入数据

我的环境: hadoop全分布集群

hbase全分布集群

代码如下:

import org.apache.hadoop.hbase.HBaseConfiguration  import org.apache.hadoop.hbase.mapreduce.TableOutputFormat  import org.apache.spark._  import org.apache.hadoop.mapreduce.Job  import org.apache.hadoop.hbase.io.ImmutableBytesWritable

import org.apache.hadoop.hbase.client.Result  import org.apache.hadoop.hbase.client.Put

import org.apache.hadoop.hbase.util.Bytes

object SparkWriteHBase {

def main(args: Array[String]): Unit = {

val sparkConf = new SparkConf().setAppName("SparkWriteHBase").setMaster("local")

val sc = new SparkContext(sparkConf)

val tablename = "student"

sc.hadoopConfiguration.set(TableOutputFormat.OUTPUT_TABLE, tablename)

val job = new Job(sc.hadoopConfiguration)

job.setOutputKeyClass(classOf[ImmutableBytesWritable])

job.setOutputValueClass(classOf[Result])

job.setOutputFormatClass(classOf[TableOutputFormat[ImmutableBytesWritable]])

val indataRDD = sc.makeRDD(Array("3,Rongcheng,M,26","4,Guanhua,M,27"))

val rdd = indataRDD.map(_.split(',')).map{arr=>{        val put = new Put(Bytes.toBytes(arr(0)))              put.add(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes(arr(1)))        put.add(Bytes.toBytes("info"),Bytes.toBytes("gender"),Bytes.toBytes(arr(2)))              put.add(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes(arr(3).toInt))       (new ImmutableBytesWritable, put)       }}            rdd.saveAsNewAPIHadoopDataset(job.getConfiguration())    }    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值