In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how Spark accesses HBase in batches. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
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)
}
}
This is the end of the article on "how to access HBase in bulk by Spark". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.