Flink--基于mysql的sink和source
基于mysql的source操作
object MysqlSource {
def main(args: Array[String]): Unit = {
val env = StreamExecutionEnvironment.getExecutionEnvironment
val source: DataStream[Student] = env.addSource(new SQL_source)
source.print()
env.execute()
}
}
class SQL_source extends RichSourceFunction[Student]{
private var connection: Connection = null
private var ps: PreparedStatement = null
override def open(parameters: Configuration): Unit = {
val driver = "com.mysql.jdbc.Driver"
val url = "jdbc:mysql://hadoop01:3306/test"
val username = "root"
val password = "root"
Class.forName(driver)
connection = DriverManager.getConnection(url, username, password)
val sql = "select stuid , stuname , stuaddr , stusex from Student"
ps = connection.prepareStatement(sql)
}
override def close(): Unit = {
if(connection != null){
connection.close()
}
if(ps != null){
ps.close()
}
}
override def run(sourceContext: SourceContext[Student]): Unit = {
val queryRequest = ps.executeQuery()
while (queryRequest.next()){
val stuid = queryRequest.getInt("stuid")
val stuname = queryRequest.getString("stuname")
val stuaddr = queryRequest.getString("stuaddr")
val stusex = queryRequest.getString("stusex")
val stu = new Student(stuid , stuname , stuaddr , stusex)
sourceContext.collect(stu)
}
}
override def cancel(): Unit = {}
}
case class Student(stuid:Int , stuname:String , stuaddr:String , stusex:String){
override def toString: String = {
"stuid:"+stuid+" stuname:"+stuname+" stuaddr:"+stuaddr+" stusex:"+stusex
}
}
基于mysql的sink操作
object MysqlSink {
def main(args: Array[String]): Unit = {
//1.创建流执行环境
val env = StreamExecutionEnvironment.getExecutionEnvironment
//2.准备数据
val dataStream:DataStream[Student] = env.fromElements(
Student(8, "xiaoming", "beijing biejing", "female")
// Student(6, "daming", "tainjing tianjin", "male "),
// Student(7, "daqiang ", "shanghai shanghai", "female")
)
//3.将数据写入到自定义的sink中(这里是mysql)
dataStream.addSink(new StudentSinkToMysql)
//4.触发流执行
env.execute()
}
}
class StudentSinkToMysql extends RichSinkFunction[Student]{
private var connection:Connection = null
private var ps:PreparedStatement = null
override def open(parameters: Configuration): Unit = {
val driver = "com.mysql.jdbc.Driver"
val url = "jdbc:mysql://hadoop01:3306/test"
val username = "root"
val password = "root"
//1:加载驱动
Class.forName(driver)
//2:创建连接
connection = DriverManager.getConnection(url , username , password)
val sql = "insert into Student(stuid , stuname , stuaddr , stusex) values(?,?,?,?);"
//3:获得执行语句
ps = connection.prepareStatement(sql)
}
//关闭连接操作
override def close(): Unit = {
if(connection != null){
connection.close()
}
if(ps != null){
ps.close()
}
}
//每个元素的插入,都要触发一次invoke,这里主要进行invoke插入
override def invoke(stu: Student): Unit = {
try{
//4.组装数据,执行插入操作
ps.setInt(1, stu.stuid)
ps.setString(2, stu.stuname)
ps.setString(3, stu.stuaddr)
ps.setString(4, stu.stusex)
ps.executeUpdate()
}catch {
case e:Exception => println(e.getMessage)
}
}
}
Flink--基于mysql的sink和source的更多相关文章
- flink写入mysql的两种方式
方式一 通过JDBCOutputFormat 在flink中没有现成的用来写入MySQL的sink,但是flink提供了一个类,JDBCOutputFormat,通过这个类,如果你提供了jdbc的dr ...
- 基于 Mysql 实现一个简易版搜索引擎
前言 前段时间,因为项目需求,需要根据关键词搜索聊天记录,这不就是一个搜索引擎的功能吗? 于是我第一时间想到的就是 ElasticSearch 分布式搜索引擎,但是由于一些原因,公司的服务器资源比较紧 ...
- 一个基于mysql构建的队列表
通常大家都会使用redis作为应用的任务队列表,redis的List结构,在一段进行任务的插入,在另一端进行任务的提取. 任务的插入 $redis->lPush("key:task:l ...
- 在Jena框架下基于MySQL数据库实现本体的存取操作
在Jena框架下基于MySQL数据库实现本体的存取操作 转自:http://blog.csdn.net/jtz_mpp/article/details/6224311 最近在做一个基于本体的管理系统. ...
- 基于MySQL协议的数据库中间层项目Atlas - 360团队
一.简介 Atlas是由 Qihoo 360公司Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了 ...
- Mysql数据库导入命令Source详解
Mysql数据库导入命令Source详解 几个常用用例: 1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u root -p dat ...
- Mysql Explain 解读(基于MySQL 5.6.36)
Mysql Explain 解读(基于MySQL 5.6.36) 1.语法 explain < table_name > #例子 explain select * from t3 wher ...
- 搭建基于MySQL的读写分离工具Amoeba
搭建基于MySQL的读写分离工具Amoeba: Amoeba工具是实现MySQL数据库读写分离的一个工具,前提是基于MySQL主从复制来实现的: 实验环境(虚拟机): 主机 角色 10.10.10.2 ...
- 实操重写IK分词器源码,基于mysql热更新词库
实操重写IK分词器源码,基于mysql热更新词库参考网址:https://blog.csdn.net/wuzhiwei549/article/details/80451302 问题一:按照这篇文章的介 ...
随机推荐
- selenium——获取元素的尺寸、文本信息、元素的属性、元素是否可见
[is_disabled 可以用来检查元素是否存在]
- Windows 下搭建 SVN服务器及使用
目录 一 .安装Visual SVN 二.配置SVN 三.安装TortoiseSVN 四.上传项目到远程仓库 五.从远程仓库下载项目 六.检出项目 七.版本回退 参考链接 http://blog.cs ...
- oracle 根据一个时间段获取这个时间段内所有月份、天数、日期
注:本文来源于< oracle 根据一个时间段获取这个时间段内所有月份.天数.日期 > 获取月份列表: SELECT TO_CHAR(ADD_MONTHS(TO_DATE('2014-10 ...
- Confluence 6 在编辑器中控制参数的显示
你可以决定宏参数在 Confluence 编辑器中如何进行显示的. 在默认的情况下,在宏占位符下尽可能显示能显示的所有参数: 你可以控制这里显示的参数数量,通过这种控制你可能尽量的为编辑者提供有效的信 ...
- Confluence 6 用户目录图例 - 可读写连接 LDAP
上面的图:Confluence 连接到一个 LDAP 目录. https://www.cwiki.us/display/CONFLUENCEWIKI/Diagrams+of+Possible+Conf ...
- plugin-barcodescanner 报错
https://github.com/phonegap/phonegap-plugin-barcodescanner/issues/418 ionic cordova platform rm andr ...
- PHP array_combine()
// 需要替换 key 的数组 $arr_old = array( '0' => array('id' => 1, 'name' => 'Carroll'), '1' => a ...
- laravel 容器注入的坑
今天遍历添加数据时遇到个坑,哪位大神知道什么原因?? 起初的代码是这样的:(部分) public function addActive(Request $request, Activenorms $a ...
- Practical Web Penettation Testing (the first one Mutillidae 大黄蜂 之二)
1.how to use dpkg cmmand first it can be used for list all software , dpkg -l (由于kali linux 没有启动所以 ...
- Web前端渗透测试技术小结(一)
首先端正一下态度不可干违法的事 1.SQL注入测试 对于存在SQL注入的网页,使用SQL语句进行关联查询(仿照C/S模式)eg http://www.foo.com/user.php?id=1 常 ...