您好,登录后才能下订单哦!
这篇文章将为大家详细讲解有关Spark如何批量存取HBase ,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
FileAna.scala
object FileAna {
// val conf: Configuration = HBaseConfiguration.create()
val hdfsPath = "hdfs://master:9000"
val hdfs = FileSystem.get(new URI(hdfsPath), new Configuration())
def main(args: Array[String]) {
val conf = new SparkConf().setAppName("FileAna").setMaster("spark://master:7077").
set("spark.driver.host", "192.168.1.127").
setJars(List("/home/pang/woozoomws/spark-service.jar",
"/home/pang/woozoomws/spark-service/lib/hbase/hbase-common-1.2.2.jar",
"/home/pang/woozoomws/spark-service/lib/hbase/hbase-client-1.2.2.jar",
"/home/pang/woozoomws/spark-service/lib/hbase/hbase-protocol-1.2.2.jar",
"/home/pang/woozoomws/spark-service/lib/hbase/htrace-core-3.1.0-incubating.jar",
"/home/pang/woozoomws/spark-service/lib/hbase/hbase-server-1.2.2.jar",
"/home/pang/woozoomws/spark-service/lib/hbase/metrics-core-2.2.0.jar"))
val sc = new SparkContext(conf)
val rdd = sc.textFile("hdfs://master:9000/woozoom/msgfile.txt")
val rdd2 = rdd.map(x => convertToHbase(anaMavlink(x)))
val hbaseConf = HBaseConfiguration.create()
hbaseConf.addResource("/home/hadoop/software/hbase-1.2.2/conf/hbase-site.xml");
val jobConf = new JobConf(hbaseConf, this.getClass)
jobConf.setOutputFormat(classOf[TableOutputFormat])
jobConf.set(TableOutputFormat.OUTPUT_TABLE, "MissionItem")
rdd2.saveAsHadoopDataset(jobConf)
sc.stop()
}
def convertScanToString(scan: Scan) = {
val proto = ProtobufUtil.toScan(scan)
Base64.encodeBytes(proto.toByteArray)
}
def convertToHbase(msg: MAVLinkMessage) = {
val p = new Put(Bytes.toBytes(UUID.randomUUID().toString()))
if (msg.isInstanceOf[msg_mission_item]) {
val missionItem = msg.asInstanceOf[msg_mission_item]
p.addColumn(Bytes.toBytes("data"), Bytes.toBytes("x"), Bytes.toBytes(missionItem.x))
p.addColumn(Bytes.toBytes("data"), Bytes.toBytes("y"), Bytes.toBytes(missionItem.y))
p.addColumn(Bytes.toBytes("data"), Bytes.toBytes("z"), Bytes.toBytes(missionItem.z))
}
(new ImmutableBytesWritable, p)
}
val anaMavlink = (str: String) => {
val bytes = ByteAndHex.hexStringToBytes(str)
QuickParser.parse(bytes).unpack()
}
}
ReadHBase.scala
object ReadHBase {
// val conf: Configuration = HBaseConfiguration.create()
val hdfsPath = "hdfs://master:9000"
val hdfs = FileSystem.get(new URI(hdfsPath), new Configuration())
def main(args: Array[String]) {
val conf = new SparkConf().setAppName("FileAna").setMaster("spark://master:7077").
set("spark.driver.host", "192.168.1.127").
setJars(List("/home/pang/woozoomws/spark-service.jar",
"/home/pang/woozoomws/spark-service/lib/hbase/hbase-common-1.2.2.jar",
"/home/pang/woozoomws/spark-service/lib/hbase/hbase-client-1.2.2.jar",
"/home/pang/woozoomws/spark-service/lib/hbase/hbase-protocol-1.2.2.jar",
"/home/pang/woozoomws/spark-service/lib/hbase/htrace-core-3.1.0-incubating.jar",
"/home/pang/woozoomws/spark-service/lib/hbase/hbase-server-1.2.2.jar",
"/home/pang/woozoomws/spark-service/lib/hbase/metrics-core-2.2.0.jar"))
val sc = new SparkContext(conf)
val hbaseConf = HBaseConfiguration.create()
hbaseConf.addResource("/home/hadoop/software/hbase-1.2.2/conf/hbase-site.xml");
hbaseConf.set(TableInputFormat.INPUT_TABLE, "MissionItem")
val scan = new Scan()
hbaseConf.set(TableInputFormat.SCAN, convertScanToString(scan))
val readRDD = sc.newAPIHadoopRDD(hbaseConf, classOf[TableInputFormat],
classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable],
classOf[org.apache.hadoop.hbase.client.Result])
val count = readRDD.count()
println("Mission Item Count:" + count)
sc.stop()
}
def convertScanToString(scan: Scan) = {
val proto = ProtobufUtil.toScan(scan)
Base64.encodeBytes(proto.toByteArray)
}
}
关于“Spark如何批量存取HBase ”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。