当前位置:   article > 正文

【大数据笔记】- Spark-SQL读写MySQL_create temporary view using org.apache.spark.sql.j

create temporary view using org.apache.spark.sql.jdbc insert into

Spark-SQL很强大,可以读写各种JDBC的库,先来一弹MySQL的,超简单:

1.MySQL库建测试表和数据:

  1. CREATE TABLE t_realtime.test_spark2mysql (
  2. id bigint(11) NOT NULL AUTO_INCREMENT,
  3. name varchar(30) DEFAULT NULL,
  4. age int DEFAULT NULL,
  5. PRIMARY KEY (id)
  6. ) ENGINE=InnoDB;
  7. INSERT INTO t_realtime.test_spark2mysql
  8. (name, age)
  9. VALUES('张三', 23);
  10. INSERT INTO t_realtime.test_spark2mysql
  11. (name, age)
  12. VALUES('李四', 33);
  13. INSERT INTO t_realtime.test_spark2mysql
  14. (name, age)
  15. VALUES('王五', 66);

2.在Spark客户机上(有Spark客户端的),启动spark-sql:

  1. ## 注:如果集群机class-path上已经有了mysql jar就不需要提交了,否则要准备好mysql jdbc jar包
  2. ## 在当前提交的客户机上准备jar包mysql-connector-java-5.1.45-bin.jar
  3. ## --driver-class-path:指定Driver端所需jar
  4. ## --jars :指定executor所需要jar
  5. spark-sql --driver-class-path /home/dw/hubg/mysql-connector-java-5.1.45-bin.jar \
  6. --jars /home/dw/hubg/mysql-connector-java-5.1.45-bin.jar

3.执行Spark SQL语句:

  1. -- 创建spark mysql的临时映射表
  2. CREATE TEMPORARY VIEW test_spark2mysql
  3. USING org.apache.spark.sql.jdbc
  4. OPTIONS (
  5. url "jdbc:mysql://10.0.马赛克:5506",
  6. dbtable "t_realtime.test_spark2mysql",
  7. user '马赛克_writer',
  8. password '马赛克'
  9. );
  10. -- 写入一条数据
  11. INSERT into TABLE test_spark2mysql
  12. SELECT
  13. 101
  14. ,'hubg' as name
  15. ,26 as age ;
  16. -- 查询表中结果
  17. select * from test_spark2mysql limit 10;

 完成,收工!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/579710
推荐阅读
相关标签
  

闽ICP备14008679号