1. Spark连接hbase
Spark连接hbase的步骤:
1. 构建sparkconf配置信息,设置spark主机位置,设置程序名称,资源数等
2. 构建sparkcontext
3. 构建Sqlcontext
4. 通过sqlcontext操作构建RDD
5. 将RDD转换为dataframe
6. 用DataFrame注册表
7. 操作表进行处理
package cn.Dalong.test.DaLong_hbase
|
2. Spark连接hive数据
1. 本地运行需要将hive的hive-site.xml hdfs-site.xml文件配置到项目中的/main/resources文件夹下使本地程序找到集群就可以运行了
2. 服务器上运行的命令:
spark-submit --executor-memory 2g--driver-memory 200M --total-executor-cores 288 --executor-cores 2 --confspark.kryoserializer.buffer.max=256m --conf spark.kryoserializer.buffer=64m--master yarn --class cn.deppon.sparkhive.Dalong/home/appman/DL_test/original-MavenTest-1.0-SNAPSHOT.jar
object Dalong {
|
3.spark连接hbase 读写操作
代码:
package cn.DaLong_hbase
import com.deppon.spark.hbase.HbaseTool.hbaseConf
import com.deppon.spark.hbase.{HbaseTool, SparkCont}
import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.client.Put
import org.apache.hadoop.hbase.io.ImmutableBytesWritable
import org.apache.hadoop.hbase.mapred.TableOutputFormat
import org.apache.hadoop.hbase.mapreduce.TableInputFormat
import org.apache.hadoop.hbase.util.Bytes
import org.apache.hadoop.mapred.JobConf
import org.apache.spark.{SparkConf, SparkContext}
/**
* Created by DreamBoy on 2017/5/8.
*/
object Hbase_read_write {
def main(args: Array[String]): Unit = {
//val sc = SparkCont.getSparkContext("Hbase_read_Write")
//构建sc
val conf = new SparkConf().setMaster("local[2]").setAppName("Hbase_read_Write")
// .set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
val sc = new SparkContext(conf)
//val hbase_read = HbaseTool.HbaseTool_Read("member",sc)
//构建读取表信息的rdd
hbaseConf.set(TableInputFormat.INPUT_TABLE,"member")
val hbase_read = sc.newAPIHadoopRDD(
hbaseConf,
classOf[TableInputFormat],
classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable],
classOf[org.apache.hadoop.hbase.client.Result]
)
//构建写表信息的rdd
//val hbase_write = HbaseTool.HbaseTool_Write("member",sc)
val hbase_write = new JobConf(hbaseConf)
hbase_write.setOutputFormat(classOf[TableOutputFormat])
hbase_write.set(TableOutputFormat.OUTPUT_TABLE,"member")
val h_Read_data = hbase_read.map{
case(_,result)=>{
val row = Bytes.toString(result.getRow)
val name = Bytes.toString(result.getValue("info".getBytes(),"name".getBytes()))
val age = Bytes.toString(result.getValue("info".getBytes(),"age".getBytes()))
(row,name,age)
}
}
// val result = h_Read_data.collect()
//println(result.toBuffer)
//由于数据存在部分为空的情况,所以控制台会报空指针的情况
// \x00\x00\x00\x01 column=info:age, timestamp=1494219674019, value=\x00\x00\x00\x0F
// \x00\x00\x00\x01 column=info:name, timestamp=1494219674019, value=jack
// 这两行数据中row的值传入的本身是Int类型的但是存储的时候转换成了16进制的形式,读取出来可能无法解析导致结果的是多个空格
//但是数据(包括上面这种情况)都做了对应的处理,并已经正确写入到hbase中
//将读出的数据加工之后写入到表中
val rdd = h_Read_data.map{
arr=>{
val put = new Put(Bytes.toBytes(arr._1+20+"<<"))
put.addColumn("info".getBytes(),"name".getBytes(),Bytes.toBytes(arr._2))
put.addColumn("info".getBytes(),"age".getBytes(),Bytes.toBytes(arr._3+"=="))
//构成(ImmutableBytesWritable,result)的元组的形式
(new ImmutableBytesWritable,put)
}
}
rdd.saveAsHadoopDataset(hbase_write)
sc.stop()
}
}