2
我現在正在學習Spark,並且我知道如何使用特定的表格。通過Spark獲取MySQL中的所有表格
但我想加載數據庫中的所有表。問題是如何去做。
這裏是我的代碼與特定的表加載:
package gt.example.spark;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.DataFrame;
import org.apache.spark.sql.SQLContext;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class App {
public static final String DRIVER = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://localhost:3306/example";
public static final String USERNAME = "root";
public static final String PASSWORD = "pass";
private static final JavaSparkContext sc =
new JavaSparkContext(new SparkConf().setAppName("Spark Example").setMaster("local[*]"));
private static final SQLContext sqlContext = new SQLContext(sc);
public static void main(String[] args) {
Map<String, String> options = new HashMap<String, String>();
options.put("driver", DRIVER);
options.put("url", URL + "?user=" + USERNAME + "&password=" + PASSWORD);
options.put("dbtable", "user");
DataFrame jdbcDF = sqlContext.read().format("jdbc").options(options).load();
jdbcDF.show();
}
}