Spark连接数据库方法
来源:互联网 发布:linux自启动脚本 编辑:程序博客网 时间:2024/06/05 11:17
Version:Spark 1.3.0
方法一:
val rdd = new JdbcRDD(sc,()=>{ Class.forName("com.mysql.jdbc.Driver").newInstance() DriverManager.getConnection("url", "user", "password") },"select * from table_name where 1 = ? AND 1 = ?",1,1,30 ,r=>r.getString(1)).cache()
参数说明:
第一个:SparkContext对象
第二个:获取Java中Connection对象连接
第三个:执行SQL语句,####注:必须要接两个参数
第四个:第1个参数值
第五个:第2个参数值
第六个:分区
第七个:返回一个结果集,对结果集进行遍历
返回:JdbcRDD对象
方法二:
val rdd = sqlContext.load("jdbc",Map( "url" -> "jdbc:mysql://localhost:3306/test?user=root&password=root", "dbtable" -> "schema.table"))
参数说明:
第一个:源名
第二个:创建Map对象设置"url","dbtable",更多如下:
url
The JDBC URL to connect to.dbtable
The JDBC table that should be read. Note that anything that is valid in a `FROM` clause of a SQL query can be used. For example, instead of a full table you could also use a subquery in parentheses.driver
The class name of the JDBC driver needed to connect to this URL. This class with be loaded on the master and workers before running an JDBC commands to allow the driver to register itself with the JDBC subsystem.partitionColumn, lowerBound, upperBound, numPartitions
These options must all be specified if any of them is specified. They describe how to partition the table when reading in parallel from multiple workers.partitionColumn
must be a numeric column from the table in question.返回:DataFrame对象
方法三:
val rdd = sqlContext.jdbc("jdbc:mysql://localhost:3306/test?user=root&password=root","schema.test")
参数说明:
第一个:数据库连接字符串
第二个:查询的表名
返回:DataFrame对象
总结:
连接方法一目前本人测试连接关系型数据库,MongoDB都是没有问题的。
连接方法二,方法三目前本人测试连接关系型数据库,但连接MongoDB没有返回结果。个人觉得是无法支持非关系型数据库。
至于Load是不是类似于数据库中的sqlload操作,这个需要测试。
注:本人在连接MongoDB与MySQL时查询出的对DataFrame进行Join运行时则出现卡住现象,但使用RDD进行Join进则不会出现卡住现象
0 0
- Spark连接数据库方法
- spark连接数据库操作(scala实现)
- Spark-SQL连接MySql关系型数据库
- spark连接mysql数据库(python语言)
- Spark 数据库 连接池 java DAO 工厂
- 连接池方法连接数据库
- 数据库常用连接方法
- 数据库的连接方法
- 连接数据库方法汇总
- 数据库的连接方法
- ACCESS连接数据库方法
- 连接数据库的方法
- JAVA连接数据库方法
- FLASH连接数据库方法
- .NET 连接数据库方法
- 所有连接数据库方法
- java连接数据库方法
- JDBC连接数据库方法
- AssetBundle机制相关资料收集
- iOS开发之Objective-c的AES加密和解密算法的实现
- js 面向对象,多重创建对象方法
- Oracle带输出参数的存储过程和调用
- log4j2 使用详解
- Spark连接数据库方法
- 1到正整数N中所有整数中1的个数
- NGUI用UIGrid加载Item会有部分空出来的解决办法
- android代码混淆,打包签名出错
- jquery 网页滚动到底部自动加载
- linux sort命令
- DP·数位DP
- java 反射通过get方法获得属性值
- c++简单链表