将流数据输出到Mysql中
outputMysqlApp.scala
import java.sql.DriverManager
import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext }
object outputMysqlApp extends App {
//配置入口点
val conf = new SparkConf().setAppName(getClass.getSimpleName).setMaster("local[2]")
val ssc= new StreamingContext(conf, Seconds(1))
//输入数据流(DStream)
val lines = ssc.socketTextStream("localhost", 9999)
//todo...
val words = lines.flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_)
// 方式三:
words.foreachRDD ( rdd => {
rdd.foreachPartition(partitionOfRecords => {
if (partitionOfRecords.size > 0) {
val connection = createNewConnection()
partitionOfRecords.foreach(record => {
val sql = "insert into wordcount(word, wordcount) vlaues('" + record._1 + "'," + record._2 + ")"
connection.createStatement().execute(sql)
})
connection.close()
}
})
})
//启动StreamingContext,接收数据,然后处理数据
ssc.start()
ssc.awaitTermination()
//创建Mysql数据库连接/**
/**
* 获取Mysql数据库连接
* @return 注意返回值,这块不能为空
*/
def createNewConnection()= {
Class.forName("com.mysql.jdbc.Driver")
DriverManager.getConnection("jdbc:mysql://192.168.1.100:3306/streaming_mysql","root","root")
}
}
将流数据输出到Mysql中的更多相关文章
- 新增访客数量MR统计之MR数据输出到MySQL
关注公众号:分享电脑学习回复"百度云盘" 可以免费获取所有学习文档的代码(不定期更新)云盘目录说明:tools目录是安装包res 目录是每一个课件对应的代码和资源等doc 目录是一 ...
- Sql Server中的数据类型和Mysql中的数据类型的对应关系(转)
Sql Server中的数据类型和Mysql中的数据类型的对应关系(转):https://blog.csdn.net/lilong329329/article/details/78899477 一.S ...
- talend 将hbase中数据导入到mysql中
首先,解决talend连接hbase的问题: 公司使用的机器是HDP2.2的机器,上面配置好Hbase服务,在集群的/etc/hbase/conf/hbase-site.xml下,有如下配置: < ...
- shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中
shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中 利用shell脚本将文本数据导入到mysql中 需求1:处理文本中的数据,将文本中的数据插入到mys ...
- sqlserver 中数据导入到mysql中的方法以及注意事项
数据导入从sql server 到mysql (将数据以文本格式从sqlserver中导出,注意编码格式,再将文本文件导入mysql中): 1.若从slqserver中导出的表中不包含中文采用: bc ...
- 使用命令行将Excel数据表导入Mysql中的方法小结
从Excel数据表导入MySQL,已经做过好几次了,但每次都会碰到各种问题:invalid utf8 character string, data too long, ...,浪费了不少时间 为了提高 ...
- 利用workbench将excel数据导入到MySQL中
数据导入的方式(csv,txt之类) 在MySQL中,数据导入的方式有两种方式 通过第三方客户端导入(workbench) 通过mysql client 方式导入 通过mysql clinet的导入方 ...
- 将爬取的数据保存到mysql中
为了把数据保存到mysql费了很多周折,早上再来折腾,终于折腾好了 安装数据库 1.pip install pymysql(根据版本来装) 2.创建数据 打开终端 键入mysql -u root -p ...
- python将oracle中的数据导入到mysql中。
一.导入表结构.使用工具:navicate premium 和PowerDesinger 1. 先用navicate premium把oracle中的数据库导出为oracle脚本. 2. 在Power ...
随机推荐
- ssh-copy-id 实现ssh无密码登录 openssh putty
ssh-keygen 生成公私钥对 $ssh-keygen ssh-copy-id将本机的公钥复制到远程机器 ssh-copy-id -i .ssh/id_rsa.pub(公钥路径) 用户名字@192 ...
- java的加载与运行
jdk中有一个javac.exe(java编译器) *Java程序的运行包括两非常重要的阶段 -编译阶段 -运行阶段 *编译阶段 -主要任务是检查Java源程序是否符合Java语法 符合Java语法则 ...
- SpringCloud学习笔记(七、SpringCloud Netflix Zuul)
目录: springcloud整合eureka.config.zuul zuul源码分析 springcloud整合eureka.config.zuul: 1.架构图 2.GitHub:https:/ ...
- 二,java框架学习
二,java框架学习 实体类的编写规则 实体类里面的属性是私有的 私有属性使用公开的set,get,方法操作 要求实体类有属性作为唯一值(一般使用id值) 实体类属性建议不使用基本数据类型,使用基本数 ...
- Fontconfig error: Cannot load config file "infinality/conf.d"
reference: https://forums.gentoo.org/viewtopic-t-1079210-start-0.html resolved with following method ...
- AtCoder Beginner Contest 139F Engines
链接 problem 给出\(n\)个二元组\((x,y)\).最初位于原点\((0,0)\),每次可以从这\(n\)个二元组中挑出一个,然后将当前的坐标\((X,Y)\)变为\((X+x,Y+y)\ ...
- Note | Ubuntu
目录 0. 教程 1. 安装 2. 系统 0. 教程 <Linux就该这么学>:https://www.cnblogs.com/RyanXing/p/9462850.html 1. 安装 ...
- 动态修改maven的jdk版本
当环境变量jdk为1.7,项目为jdk1.8,用mvn clean package指令打包项目时,想不修改环境变量的情况下,修改maven的jdk版本 方法如下: 官网给出了方法 https://ma ...
- Appium移动自动化测试-----(三)Intellij IDEA + Android SDK + Genymotion Emulator
下载安装Intellij IDEA 略 下载Android SDK http://tools.android-studio.org/index.php/sdk 下载后解压 http://www. ...
- pixijs shader 扫光加强版
pixijs shader 扫光加强版 const app = new PIXI.Application({ transparent: true }); document.body.appendChi ...