目标
使用hive或spark时可以直接使用mysql的数据,免去同步的烦恼。
方案
在hive值有JDBC storeage handler,可以直接使用,具体参考 JDBC Storage Handler
环境准备
需要两个jar包,本人为
- mysql-connector-java-8.0.16.jar
- /hive-jdbc-handler-3.1.3.jar
将该表上传至spark,本人为 spark.yarn.jars
配置的hdfs目录
基础语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| CREATE EXTERNAL TABLE student_jdbc ( name string, age int, gpa double ) STORED BY 'org.apache.hive.storage.jdbc.JdbcStorageHandler' TBLPROPERTIES ( "hive.sql.database.type" = "MYSQL", "hive.sql.jdbc.driver" = "com.mysql.jdbc.Driver", "hive.sql.jdbc.url" = "jdbc:mysql://localhost/sample", "hive.sql.dbcp.username" = "hive", "hive.sql.dbcp.password" = "hive", "hive.sql.table" = "STUDENT", "hive.sql.dbcp.maxActive" = "1" );
|
修改属性
1
| ALTER TABLE student_jdbc SET TBLPROPERTIES ("hive.sql.dbcp.password" = "passwd");
|
参考
JDBC Storage Handler