千家信息网

spark sql怎么连接和使用mysql数据源

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,这篇文章主要介绍"spark sql怎么连接和使用mysql数据源",在日常操作中,相信很多人在spark sql怎么连接和使用mysql数据源问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作
千家信息网最后更新 2026年10月31日spark sql怎么连接和使用mysql数据源

这篇文章主要介绍"spark sql怎么连接和使用mysql数据源",在日常操作中,相信很多人在spark sql怎么连接和使用mysql数据源问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"spark sql怎么连接和使用mysql数据源"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

spark sql 可以通过标准的jdbc连接数据库,获得数据源

public class SparkSql {public static SimpleDateFormat sdf = new SimpleDateFormat("_yyyyMMdd_HH_mm_ss");    private static final String appName = "spark sql test";    private static final String master = "spark://192.168.1.21:7077";    private static final String JDBCURL = "jdbc:mysql://192.168.1.18:3306/lng?user=root&password=123456";    public static void main(String[] avgs){        SparkContext context = new SparkContext(master, appName);        SQLContext sqlContext = new SQLContext(context);        // Creates a DataFrame based on a table named "people"        // stored in a MySQL database.        DataFrame df = sqlContext                .read()                .format("jdbc")                .option("url", JDBCURL)                .option("dbtable", "tsys_user")                .load();        // Looks the schema of this DataFrame.        df.printSchema();        // Counts people by age        DataFrame countsByAge = df.groupBy("customStyle").count();        countsByAge.show();        // Saves countsByAge to S3 in the JSON format.        countsByAge.write().format("json").save("hdfs://192.168.1.17:9000/administrator/sql-result" + sdf.format(new Date()));    }}

如果没有包含mysql的驱动程序,需要参考http://stackoverflow.com/questions/34764505/no-suitable-driver-found-for-jdbc-in-spark

  1. You might want to assembly you application with your build manager (Maven,SBT) thus you'll not need to add the dependecies in your spark-submit cli. (意思就是把mysql的驱动程序打包到提交到spark的jar包里)

  2. You can use the following option in your spark-submit cli :(改成下面,经测试,可行,或者加入export SPARK_CLASSPATH=$SPARK_CLASSPATH:/usr/local/spark-1.6.1-bin-hadoop2.6/conf/driverLib/mysql-connector-java-5.1.36.jar 到conf/spark-env.sh)

    spark-submit --driver-class-path /usr/local/spark-1.6.1-bin-hadoop2.6/conf/driverLib/mysql-connector-java-5.1.36.jar --class com.xxx.SparkSql  /usr/local/spark.jar


    Explanation : Supposing that you have all your jars in a lib directory in your project root, this will read all the libraries and add them to the application submit.

  3. You can also try to configure these 2 variables : spark.driver.extraClassPath and spark.executor.extraClassPath in SPARK_HOME/conf/spark-default.conf file and specify the value of these variables as the path of the jar file. Ensure that the same path exists on workernodes.(经测,不行)

到此,关于"spark sql怎么连接和使用mysql数据源"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

数据 数据源 学习 更多 程序 驱动程序 帮助 驱动 不行 可行 实用 接下来 可以通过 就是 意思 数据库 文章 方法 标准 理论 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 精品咖啡豆 上海喔噻互联网科技有限公司 互联网科技行业发展 北京互联网科技有限公司 互联网金融公司科技 深圳艾佳互联网科技 互联网科技产品 湖北码尼互联网科技有限公司电话 华夏互联网科技业 互联网科技与大数据专业 恒生互联网科技指数跳水 想开互联网科技公司怎么样 佛山市翊博互联网络科技有限公司 无锡工业互联网科技有限公司 沐思想互联网科技有限公司 山东大美朱楼互联网科技有限公司 北京互联网科技类企业分布 保山浩联互联网科技有限公司 承德科技产业互联网平台 安徽九吞吴互联网科技有限公司 互联网科技海报 互联网核心科技是什么 互联网 科技 深圳齐家互联网科技 互联网 科技服务平台 镁科技互联网资讯平台 西藏七维空间互联网科技有限公司 消费医药和互联网科技 众安科技工业互联网合作 如何强化互联网金融科技
0