本文介紹了使用 SparkSQL 刪除 MySQL 表的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我知道我們可以通過以下命令從 SparkSQL 查詢或創(chuàng)建一個 Mysql 表.
I know that we can query or create a Mysql table from SparkSQL through the below commands.
val data = sqlContext.read.jdbc(urlstring, tablename, properties)
data.write.format("com.databricks.spark.csv").save(result_location)
val dataframe = sqlContext.read.json("users.json")
dataframe.write.jdbc(urlstring, table, properties)
這樣有沒有辦法刪除一個表?
Like that is there any way to drop a table ?
推薦答案
您可以嘗試使用 JDBC
驅(qū)動程序進行基本的 DROP
操作:
You can try a basic DROP
operation with the JDBC
driver :
val DB_URL: String = ???
val USER: String = ???
val PASS: String = ???
def dropTable(tableName: String) = {
import java.sql._;
var conn: Connection = null;
var stmt: Statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
println("Connected database successfully...");
println("Deleting table in given database...");
stmt = conn.createStatement();
val sql: String = s"DROP TABLE ${tableName} ";
stmt.executeUpdate(sql);
println(s"Table ${tableName} deleted in given database...");
} catch {
case e: Exception => println("exception caught: " + e);
} finally {
???
}
}
dropTable("test")
您可以使用 JDBCUtils
在 Spark 中做到這一點,但這非常簡單.
You can do that with Spark using JDBCUtils
but this is quite straightforward.
這篇關(guān)于使用 SparkSQL 刪除 MySQL 表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!