Spark2.x写Hbase1-2.x
Posted zxbdboke
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spark2.x写Hbase1-2.x相关的知识,希望对你有一定的参考价值。
import org.apache.hadoop.hbase.io.ImmutableBytesWritable import org.apache.hadoop.hbase.mapreduce.TableOutputFormat import org.apache.hadoop.hbase.client.Result import org.apache.hadoop.hbase.client.Put import org.apache.hadoop.mapreduce.Job import org.apache.hadoop.hbase.util.Bytes import org.apache.spark.{SparkConf, SparkContext} /** * Spark写HBase */ object SparkWriteHbase { def main(args: Array[String]): Unit = { val conf = new SparkConf().setAppName("SparkWriteHBase").setMaster("local") val sc = new SparkContext(conf) 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.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes(arr(1))) put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("gender"),Bytes.toBytes(arr(2))) put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes(arr(3))) (new ImmutableBytesWritable(),put) }) rdd.saveAsNewAPIHadoopDataset(job.getConfiguration) } }
以上是关于Spark2.x写Hbase1-2.x的主要内容,如果未能解决你的问题,请参考以下文章
Spark2.x(六十):在Structured Streaming流处理中是如何查找kafka的DataSourceProvider?