def main(args: Array[String]): Unit = {
val hConf = HBaseConfiguration.create();
hConf.set("hbase.zookeeper.quorum","m6,m7,m8")
val tableName = "t_person"
hConf.set(TableInputFormat.INPUT_TABLE, tableName)
val hAdmin = new HBaseAdmin(hConf)
val conf = new SparkConf()
conf.set("spark.master", "local")
conf.set("spark.app.name", "spark demo")
val sc = new SparkContext(conf);
val rs = sc.newAPIHadoopRDD(hConf, classOf[TableInputFormat], classOf[ImmutableBytesWritable], classOf[Result])
rs.foreach(x => {
println(Bytes.toString(x._2.getRow))
// 通过列族和列名获取列
println(Bytes.toInt(x._2.getValue("base_info".getBytes, "age".getBytes)))
})
}

 保存数据到hbase数据库中

def main(args: Array[String]): Unit = {
val conf = new SparkConf()
conf.set("spark.master", "local")
conf.set("spark.app.name", "spark demo")
// 创建SparkSession对象
val spark = SparkSession.builder().appName("spark sql").config(conf).getOrCreate();
// 创建sparkContext对象
val sc = spark.sparkContext val hbaseConf = HBaseConfiguration.create()
val tableName = "t_person"
hbaseConf.set("hbase.zookeeper.quorum","m6,m7,m8")
hbaseConf.set(TableInputFormat.INPUT_TABLE, tableName) // val hbaseAdmin = new HBaseAdmin(hbaseConf)
val jobConf = new JobConf(hbaseConf, this.getClass)
jobConf.setOutputFormat(classOf[TableOutputFormat])
jobConf.set(TableOutputFormat.OUTPUT_TABLE, tableName) val pairs = sc.parallelize(List(("p_0000010", "12"))) def convert(data : (String, String)) = {
val p = new Put(Bytes.toBytes(data._1))
p.add(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes(data._2))
(new ImmutableBytesWritable , p)
} // 保存数据到hbase数据库中
new PairRDDFunctions(pairs.map(convert)).saveAsHadoopDataset(jobConf)
}

  

spark读取hbase数据的更多相关文章

  1. Spark 读取HBase数据

    Spark1.6.2 读取 HBase 1.2.3 //hbase-common-1.2.3.jar //hbase-protocol-1.2.3.jar //hbase-server-1.2.3.j ...

  2. SparkSQL读取HBase数据

    这里的SparkSQL是指整合了Hive的spark-sql cli(关于SparkSQL和Hive的整合,见文章后面的参考阅读). 本质上就是通过Hive访问HBase表,具体就是通过hive-hb ...

  3. 关于mapreducer 读取hbase数据 存入mysql的实现过程

    mapreducer编程模型是一种八股文的代码逻辑,就以用户行为分析求流存率的作为例子 1.map端来说:必须继承hadoop规定好的mapper类:在读取hbase数据时,已经有现成的接口 Tabl ...

  4. 使用MapReduce读取HBase数据存储到MySQL

    Mapper读取HBase数据 package MapReduce; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hba ...

  5. Spark 读取HBase和SolrCloud数据

    Spark1.6.2读取SolrCloud 5.5.1 //httpmime-4.4.1.jar // solr-solrj-5.5.1.jar //spark-solr-2.2.2-20161007 ...

  6. Spark读取Hbase中的数据

    大家可能都知道很熟悉Spark的两种常见的数据读取方式(存放到RDD中):(1).调用parallelize函数直接从集合中获取数据,并存入RDD中:Java版本如下: JavaRDD<Inte ...

  7. spark读取hbase形成RDD,存入hive或者spark_sql分析

    object SaprkReadHbase { var total:Int = 0 def main(args: Array[String]) { val spark = SparkSession . ...

  8. spark读取hbase(NewHadoopAPI 例子)

    package cn.piesat.controller import java.text.{DecimalFormat, SimpleDateFormat}import java.utilimpor ...

  9. spark读取kafka数据 createStream和createDirectStream的区别

    1.KafkaUtils.createDstream 构造函数为KafkaUtils.createDstream(ssc, [zk], [consumer group id], [per-topic, ...

随机推荐

  1. [Unity3D插件]2dToolKit系列三 碰撞检测功能的实现以及障碍物的随机摆放

    貌似有一段时间没更新2dtoolkit系列了,这段时间一直在忙着其他事情,今天开始继续这个插件系列的教程,网上搜索,貌似关于这个插件的教程无非还是跟官方的教程很类似,有的甚至都没有自己照着亲手实践一遍 ...

  2. 注解:Hibernate双向N->N关联(两端都控制关联关系)

    Person与Address关联:双向N->N,[连接表必须有],两端都控制关联关系 #需要说明的是:如果程序希望某一端放弃控制关联关系,则可以在这一段的@ManyToMany注解中指定mapp ...

  3. [leetcode] 题型整理之二叉树

    94. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' va ...

  4. Chart图表

    这东西挺直观 封装个类 public class aaa { private string name; public string Name { get { return name; } set { ...

  5. 读取EXCEL数据到内存DataTable

    protected void Page_Load(object sender, EventArgs e) { string filepath = Server.MapPath("~/file ...

  6. 【BZOJ1672】[Usaco2005 Dec]Cleaning Shifts 清理牛棚 动态规划

    [BZOJ1672][Usaco2005 Dec]Cleaning Shifts Description Farmer John's cows, pampered since birth, have ...

  7. VB下对HTML元素的操作

    <!DOCTYPE html> <html> <head> <title>test</title> </head> <bo ...

  8. 【BZOJ】3991: [SDOI2015]寻宝游戏

    题意 给一个\(n\)个点带边权的树.有\(m\)次操作,每一次操作一个点\(x\),如果\(x\)已经出现,则\(x\)消失.否则\(x\)出现.每一操作后,询问从某个点开始走,直到经过所有出现的点 ...

  9. BZOJ1055: [HAOI2008]玩具取名

    ... #include<bits/stdc++.h> using namespace std; int q[255]; char s[205]; char p[]={'W','I','N ...

  10. Unity: Passing Constructor Parameters to Resolve

    In this tutorial we will go through of couple different ways of using custom constructor parameters ...