1、方法1:分别将两张表中的数据加载为DataFrame

  1. /*
  2. * 方法1:分别将两张表中的数据加载为DataFrame
  3. * */
  4. /*
         Map<String,String> options = new HashMap<String,String>();
  5. options.put("url","jdbc:mysql://localhost:3306/tset");
  6. options.put("driver","com.mysql.jdbc.Driver");
  7. options.put("user","root");
  8. options.put("password","admin");
  9. options.put("dbtable","information");
  10. Dataset myinfromation = sqlContext.read().format("jdbc").options(options).load();
  11. //如果需要多张表,则需要再put一遍
  12. options.put("dbtable","score");
  13. Dataset scores = sqlContext.read().format("jdbc").options(options).load();*/

2、方法2:分别将mysql中两张表的数据加载为DataFrame

  1. //方法2:分别将mysql中两张表的数据加载为DataFrame
  2. DataFrameReader reader = sqlContext.read().format("jdbc");
  3. reader.option("url","jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT");
  4. reader.option("driver","com.mysql.cj.jdbc.Driver");
  5. reader.option("user","root");
  6. reader.option("password","admin");
  7. reader.option("dbtable","information");
  8. Dataset myinformation = reader.load();
  9. reader.option("dbtable","score");
  10. Dataset scores = reader.load();

3、问题:

在程序运行过程报错

(1)

解决:

在idea中加入jar包

(2)运行报错

  1. The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents .....

解决:添加信息。

  1. dbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT

4、成功运行

附:程序源码:

  1. package sparkSQl;
  2.  
  3. import org.apache.spark.SparkConf;
  4. import org.apache.spark.api.java.JavaSparkContext;
  5. import org.apache.spark.sql.DataFrameReader;
  6. import org.apache.spark.sql.Dataset;
  7. import org.apache.spark.sql.SQLContext;
  8.  
  9. import java.util.HashMap;
  10. import java.util.Map;
  11.  
  12. public class mysqlToDataFrame {
  13. public static void main(String[] args) {
  14. //首先新建一个sparkconf定义参数
  15. SparkConf conf = new SparkConf().setMaster("local").setAppName("JDBCDataSource");
  16. //创建sparkContext,是通往spark集群的唯一通道
  17. JavaSparkContext sc = new JavaSparkContext(conf);
  18. //新建一个sparksql
  19. SQLContext sqlContext = new SQLContext(sc);
  20. //sparksql连接mysql
  21. /*
  22. * 方法1:分别将两张表中的数据加载为DataFrame
  23. * */
  24. /*Map<String,String> options = new HashMap<String,String>();
  25. options.put("url","jdbc:mysql://localhost:3306/tset");
  26. options.put("driver","com.mysql.jdbc.Driver");
  27. options.put("user","root");
  28. options.put("password","admin");
  29. options.put("dbtable","information");
  30. Dataset myinfromation = sqlContext.read().format("jdbc").options(options).load();
  31. //如果需要多张表,则需要再put一遍
  32. options.put("dbtable","score");
  33. Dataset scores = sqlContext.read().format("jdbc").options(options).load();*/
  34.  
  35. //方法2:分别将mysql中两张表的数据加载为DataFrame
  36. DataFrameReader reader = sqlContext.read().format("jdbc");
  37. reader.option("url","jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT");
  38. reader.option("driver","com.mysql.cj.jdbc.Driver");
  39. reader.option("user","root");
  40. reader.option("password","admin");
  41. reader.option("dbtable","information");
  42. Dataset myinformation = reader.load();
  43. reader.option("dbtable","score");
  44. Dataset scores = reader.load();
  45.  
  46. //将两个DataFrame转换为javapairrdd,执行join操作
  47. myinformation.registerTempTable("info");
  48. scores.registerTempTable("score");
  49.  
  50. //定义sql语句
  51. String sql = "select info.name,age"
  52. +" from info join score"
  53. +" on(info.name=score.name)"
  54. +" where score.score>90";
  55.  
  56. Dataset sql2 = sqlContext.sql(sql);
  57. sql2.show();
  58.  
  59. }
  60. }

sparksql连接mysql的更多相关文章

  1. Spark-SQL连接MySql关系型数据库

    本文主要分析Spark SQL官方文档中有关于JDBC To Other Databases部分,以MySQL数据库为例,结合数据读写操作的实例代码进行详细的分析.本文中的代码需要使用到Mysql J ...

  2. Spark之 使用SparkSql操作mysql和DataFrame的Scala实现

    通过读取文件转换成DataFrame数据写入到mysql中 package com.zy.sparksql import java.util.Properties import org.apache. ...

  3. nodejs进阶(6)—连接MySQL数据库

    1. 建库连库 连接MySQL数据库需要安装支持 npm install mysql 我们需要提前安装按mysql sever端 建一个数据库mydb1 mysql> CREATE DATABA ...

  4. 【初学python】使用python连接mysql数据查询结果并显示

    因为测试工作经常需要与后台数据库进行数据比较和统计,所以采用python编写连接数据库脚本方便测试,提高工作效率,脚本如下(python连接mysql需要引入第三方库MySQLdb,百度下载安装) # ...

  5. Node.js Express连接mysql完整的登陆注册系统(windows)

    windows学习环境: node 版本: v0.10.35 express版本:4.10.0 mysql版本:5.6.21-log 第一部分:安装node .Express(win8系统 需要&qu ...

  6. PDO连接mysql数据库

    1.PDO简介 PDO(PHP Data Object) 是PHP 5 中加入的东西,是PHP 5新加入的一个重大功能,因为在PHP 5以前的php4/php3都是一堆的数据库扩展来跟各个数据库的连接 ...

  7. 使用ABP EntityFramework连接MySQL数据库

    ASP.NET Boilerplate(简称ABP)是在.Net平台下一个很流行的DDD框架,该框架已经为我们提供了大量的函数,非常方便与搭建企业应用. 关于这个框架的介绍我就不多说,有兴趣的可以参见 ...

  8. jmeter之连接mysql和SQL Server配置

    下载jdbc驱动 在使用jmeter做性能或自动化测试的时候,往往需要直接对数据库施加压力,或者某些参数只能从数据库获取,这时候就必须使用jmeter连接数据库. 1.下载对应的驱动包 mysql驱动 ...

  9. Windows操作系统下远程连接MySQL数据库

    用Eclipse做一个后台项目,但是数据库不想放在本地电脑,于是买了一个腾讯云服务器(学生有优惠,挺便宜的),装上MySQL数据库,但是测试连接的时候,发现总是连接不是上,但是本地数据库可以连接,于是 ...

随机推荐

  1. CRC32加密算法原理

    [转]:https://blog.csdn.net/android_mnbvcxz/article/details/78902737

  2. Display PDF in browser | Acrobat, Reader XI

    点这个链接查看详细的解决办法 http://helpx.adobe.com/acrobat/using/display-pdf-browser-acrobat-xi.html When you cli ...

  3. c# 解释器模式与sping.net表达式的结合应用(金融里经常需要用到公式,这个公式是抽象的需要自己解释)

    .代码 using Spring.Expressions; using System; using System.Collections.Generic; using System.Linq; usi ...

  4. DIV+CSS布局时, DIV的高度和宽度特性

    这个没有特别的做要求,你要根据你自己的页面整体布局来设置,还有根据div的特性来设置,div默认情况是宽度最大化(100%).高度最小化,高度随着内容自动伸展: 一般情况做网页的话,大部分都是固定了总 ...

  5. [GO]go使用etcd

    package main import ( "go.etcd.io/etcd/clientv3" //笔者在使用clientv3的时间曾经使用过github.com/coreos/ ...

  6. HAService 刨坑

    High availability is a characteristic of a system, which describes the duration (length of time) for ...

  7. PL/SQL Developer 窥探事务

    一次登录代表一个连接 一个SQL Window 代表一个会话(session),有唯一的SID 事务(transaction) 由 insert .update 或者 delete 开启 由 comm ...

  8. require的路径问题(比较重要)

    dojo.baseUrl baseUrl用来存储dojo.js存放 的跟目录,例如dojo.js的路径是“/web/scripts/dojo-1.3/dojo/dojo.js”则baseUrl为“/w ...

  9. Codeforces 768A Oath of the Night's Watch 2017-02-21 22:13 39人阅读 评论(0) 收藏

    A. Oath of the Night's Watch time limit per test 2 seconds memory limit per test 256 megabytes input ...

  10. JUnit4简易教程

    1.下载JUnit4的jar包,在项目上右键选properties->Java Build Path ->Libraries->Add library添加刚才的jar包 2.在项目中 ...