当前位置:网站首页>如何在 TiDB Cloud 上使用 Databricks 进行数据分析 | TiDB Cloud 使用指南
如何在 TiDB Cloud 上使用 Databricks 进行数据分析 | TiDB Cloud 使用指南
2022-07-28 11:54:00 【InfoQ】

设置 TiDB Cloud Dev Tier 集群
- 注册 TiDB Cloud 账号并登录。
- 在Create Cluster > Developer Tier菜单下,选择1 year Free Trial。
- 设置集群名称,并为集群选择区域。
- 单击Create。大约 1~3 分钟后,TiDB Cloud 集群创建成功。
- 在Overview面板,单击Connect并创建流量过滤器。例如,添加 IP 地址 0.0.0.0/0,允许所有 IP 访问。
将样例数据导入 TiDB Cloud
- 在集群信息窗格,单击 Import。随后,将出现 Data Import Task 页面。
- 按如下所示配置导入任务:
- Data Source Type :
Amazon S3
- Bucket URL :
s3://tidbcloud-samples/data-ingestion/
- Data Format :
TiDB Dumpling
- Role-ARN :
arn:aws:iam::385595570414:role/import-sample-access
- 配置Target Database时,键入 TiDB 集群的Username和Password。
- 单击Import,开始导入样例数据。整个过程将持续大约 3 分钟。
- 返回概览面板,单击Connect to Get the MyCLI URL。
- 使用 MyCLI 客户端检查样例数据是否导入成功:
$ mycli -u root -h tidb.xxxxxx.aws.tidbcloud.com -P 4000
(none)> SELECT COUNT(*) FROM bikeshare.trips;
+----------+
| COUNT(*) |
+----------+
| 816090 |
+----------+
1 row in set
Time: 0.786s
使用 Databricks 连接 TiDB Cloud
- 在 Databricks 工作区,按如下所示方式创建并关联 Spark 集群:

- 在 Databricks 笔记本中配置 JDBC。TiDB 可以使用 Databricks 默认的 JDBC 驱动程序,因此无需配置驱动程序参数:
%scala
val url = "jdbc:mysql://tidb.xxxx.prod.aws.tidbcloud.com:4000"
val table = "bikeshare.trips"
val user = "root"
val password = "xxxxxxxxxx"
- url:用于连接 TiDB Cloud 的 JDBC URL
- table:指定数据表,例如:{table}
- user:用于连接 TiDB Cloud 的 用户名
- password:用户的密码
- 检查 TiDB Cloud 的连通性:
%scala
import java.sql.DriverManager
val connection = DriverManager.getConnection(url, user, password)
connection.isClosed()
res2: Boolean = false
在 Databricks 中分析数据
- 创建一个 Spark DataFrame 用于加载 TiDB 数据。这里,我们将引用在之前步骤中定义的变量:
%scala
val remote_table = spark.read.format("jdbc")
.option("url", url)
.option("dbtable", table)
.option("user", user)
.option("password", password)
.load()
- 查询数据。Databricks 提供强大的图表显示功能,您可以自定义图表类型:
%scala
display(remote_table.select("*"))

- 创建一个 DataFrame 视图或一张 DataFrame 表。我们创建一个名为 “trips” 的视图作为示例:
%scala
remote_table.createOrReplaceTempView("trips")
- 使用 SQL 语句查询数据。以下语句将查询每种类型单车的数量:
%sql
SELECT rideable_type, COUNT(*) count FROM trips GROUP BY rideable_type ORDER BY count DESC
- 将分析结果写入 TiDB Cloud:
%scala
spark.table("type_count")
.withColumnRenamed("type", "count")
.write
.format("jdbc")
.option("url", url)
.option("dbtable", "bikeshare.type_count")
.option("user", user)
.option("password", password)
.option("isolationLevel", "NONE")
.mode(SaveMode.Append)
.save()
将 TiDB Cloud 样例笔记本导入 Databricks
- 在 Databricks 工作区,单击Create > Import,并粘贴 TiDB Cloud 样例 URL,将笔记本下载到您的 Databricks 工作区。
- 将该笔记本关联到您的 Spark 集群。
- 使用您自己的 TiDB Cloud 集群信息替换样例中的 JDBC 配置。
- 按照笔记本中的步骤,通过 Databricks 使用 TiDB Cloud。
总结
边栏推荐
- Cloud native - runtime environment
- Kotlin是如何帮助你避免内存泄漏的?
- SQL most commonly used basic operation syntax
- [graduation design teaching] ultrasonic ranging system based on single chip microcomputer - Internet of things embedded stm32
- Connected Block & food chain - (summary of parallel search set)
- Uncover why devaxpress WinForms, an interface control, discards the popular maskbox property
- Datanode data block missing problem finding
- Low code: reduce technical capability requirements and improve software development efficiency
- Leetcode: array
- 01 pyechars 特性、版本、安装介绍
猜你喜欢

Science heavyweight: AI design protein has made another breakthrough, and it can design specific functional proteins

What if win11 cannot recognize Ethernet

快速读入

01 pyechars 特性、版本、安装介绍

Leetcode 42. rainwater connection

Flexpro software: measurement data analysis in production, research and development

2020-12-27

线性分类器(CCF20200901)

Multiple items on a computer share a public-private key pair to pull the Gerrit server code

Vs code is not in its original position after being updated
随机推荐
LeetCode94. 二叉树的中序遍历
Code layered management of interface testing based on RF framework
STM32 loopback structure receives and processes serial port data
Ruan Bonan of Green Alliance Technology: cloud native security from the open source shooting range
[Bi design teaching] STM32 and FreeRTOS realize low power consumption
Machine learning practice - decision tree-22
Quick read in
区块反转(暑假每日一题 7)
FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be depreca
[embedded explanation] key scanning based on finite state machine and stm32
Jetpack 全家桶之 LiveData 使用及源码篇
Linear classifier (ccf20200901)
CCF201912-2 回收站选址
Flexpro software: measurement data analysis in production, research and development
04 pyechars 地理图表(示例代码+效果图)
Unity loads GLB model
Shenwenbo, researcher of the Hundred Talents Program of Zhejiang University: kernel security in the container scenario
Multiple items on a computer share a public-private key pair to pull the Gerrit server code
Problem solving during copilot trial
Fundamentals of machine learning - principal component analysis pca-16