IDEA中Spark往Hbase中写数据
import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.io.ImmutableBytesWritable
import org.apache.hadoop.hbase.mapred.TableOutputFormat
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.hadoop.hbase.client.{Put, Result}
import org.apache.hadoop.hbase.util.Bytes
import org.apache.hadoop.mapred.JobConf object 写Hbase数据 {
def main(args: Array[String]): Unit = {
val sparkConf=new SparkConf().setAppName("往Hbase中写数据").setMaster("local[2]")
val sc=new SparkContext(sparkConf)
val tableName="student"
// sc.hadoopConfiguration.set(TableOutputFormat.OUTPUT_TABLE,tableName) val conf=HBaseConfiguration.create() val jobConf=new JobConf(conf)
jobConf.setOutputFormat(classOf[TableOutputFormat])
jobConf.set(TableOutputFormat.OUTPUT_TABLE,tableName)
//构建新纪录
val dataRDD=sc.makeRDD(Array("5,hadoop,B,29","6,spark,G,56"))
val rdd=dataRDD.map(_.split(",")).map{x=>{
val put=new Put(Bytes.toBytes(x())) //行健的值 Put.add方法接收三个参数:列族,列名,数据
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes(x())) //info:name列的值
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("gender"),Bytes.toBytes(x())) //info:gender列的值
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes(x()))//info:age列的值
(new ImmutableBytesWritable,put) ////转化成RDD[(ImmutableBytesWritable,Put)]类型才能调用saveAsHadoopDataset
}}
rdd.saveAsHadoopDataset(jobConf)
}
}
结果:
hbase(main)::> scan 'student'
ROW COLUMN+CELL
column=info:age, timestamp=, value=
column=info:gender, timestamp=, value=B
column=info:name, timestamp=, value=hadoop
column=info:age, timestamp=, value=
column=info:gender, timestamp=, value=G
column=info:name, timestamp=, value=spark
column=info:age, timestamp=, value=
column=info:gender, timestamp=, value=B
column=info:name, timestamp=, value=hadoop
column=info:age, timestamp=, value=
column=info:gender, timestamp=, value=G
column=info:name, timestamp=, value=spark
IDEA中Spark往Hbase中写数据的更多相关文章
- IDEA中Spark读Hbase中的数据
import org.apache.hadoop.hbase.HBaseConfiguration import org.apache.hadoop.hbase.io.ImmutableBytesWr ...
- 用Spark查询HBase中的表数据
java代码如下: package db.query; import org.apache.commons.logging.Log; import org.apache.commons.logging ...
- Spark读取Hbase中的数据
大家可能都知道很熟悉Spark的两种常见的数据读取方式(存放到RDD中):(1).调用parallelize函数直接从集合中获取数据,并存入RDD中:Java版本如下: JavaRDD<Inte ...
- IDEA中 Spark 读Hbase 报错处理:
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory] // :: ERROR RecoverableZooKeepe ...
- Spark读写Hbase中的数据
def main(args: Array[String]) { val sparkConf = new SparkConf().setMaster("local").setAppN ...
- 用Spark向HBase中插入数据
java代码如下: package db.insert; import java.util.Iterator; import java.util.StringTokenizer; import org ...
- 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 ...
- mapreduce 只使用Mapper往多个hbase表中写数据
只使用Mapper不使用reduce会大大减少mapreduce程序的运行时间. 有时候程序会往多张hbase表写数据. 所以有如题的需求. 下面给出的代码,不是可以运行的代码,只是展示driver中 ...
- 使用Hive或Impala执行SQL语句,对存储在HBase中的数据操作
CSSDesk body { background-color: #2574b0; } /*! zybuluo */ article,aside,details,figcaption,figure,f ...
随机推荐
- java 反射---学习笔记
一.java的动态性 反射机制 动态编译 动态执行JavaScript代码 动态字节码操作 二.动态语言 动态语言 程序运行时,可以改变程序结构或变量类型,典型的语言:python.ruby.java ...
- MyBatis 多参问题
当传入的参数为多个参数时 1 可以不封装为Javabean直接传入,写法如下 public List<XXXBean> getXXXBeanList(String xxId, String ...
- Lucene实现全文检索的流程
[索引和搜索流程图] 对要索引的原始内容进行索引构建一个索引库,索引过程包括:确定原始内容即要搜索的内容->采集文档->创建文档->分析文档->索引文档. 从索引库中搜索内容, ...
- Centos下安装X Window+GNOME Desktop+FreeNX
FreeNX是近年来继VNC之后新出现的远程控制解决方案,基本原理是将XWindows的信号压缩后传输到远程客户端显示,而VNC是直接截取 屏幕图像处理传输.这样,在同样的传输信道条件下,FreeNX ...
- english & utils & tools
english & utils & tools https://openlanguage.com/ https://www.grammarly.com/blog/email-writi ...
- C++字符串读入
int read() { ,f=;char ch=getchar(); ;ch=getchar();} +ch-';ch=getchar();} return x*f; } int main() { ...
- [转] 结构体file_operations
原文地址: http://www.cnblogs.com/sunyubo/archive/2010/12/22/2282079.html 结构体file_operations在头文件 linux/fs ...
- codevs——1385 挤牛奶
1385 挤牛奶 USACO 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题解 查看运行结果 题目描述 Description 三个农民每天清 ...
- html上传图片类型
<html> <head> <meta charset="utf-8"> <title>上传图片</title> ...
- [Spring Boot ] Creating the Spring Boot Project : Demo: Creating a REST Controller
In Spring boot, define a REST API endpoint is pretty easy. package com.globomatisc.bike.controllers; ...