Databricks 加载MySQL数据

本文介绍如何在Databricks环境中加载MySQL数据库的数据,并演示了数据处理过程,包括创建临时视图、修改列名、表连接及排序等操作。

 databricks加载MySQL数据,需要先安装MySQL的驱动包。可以从集群的Libraries安装。上传jar包即可。

查询如下

driver = "com.mysql.jdbc.Driver"
url = dbutils.secrets.get(scope = "db_test", key = "url")
user = dbutils.secrets.get(scope = "db_test", key = "username")
password = dbutils.secrets.get(scope = "db_test", key = "password")
obj = spark.read.format("jdbc").option("driver", driver).\
option("url", url).option("user", user).option("password", password)

table_sql = "(select * from students) tmp"
df_students = obj.option("dbtable", table_sql).load()
display(df_students)

 

创建临时表

df_students.createOrReplaceTempView("test_student")

修改列名

df_students_new = df_students.withColumnRenamed("name","student_name" )

表连接

#全连接
df_union1 = df_students_new.join(df_students,"id","full")
#左外连接
df_union2 = df_students_new.join(df_students,"id","left_outer")
#右外连接
df_union3 = df_students_new.join(df_students,"id","right_outer")

排序

df_union1 = df_union1.sort("age")

 转Pandas

df_union1 = df_union1.toPandas()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wo.austin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值