本文主要内容:
Spark对PostgreSQL数据源数据的处理,通过Spark SQL对结构化数据进行数据分析。
compile group: 'org.apache.spark', name: 'spark-core_2.11', version: '2.3.1'
compile group: 'org.apache.spark', name: 'spark-sql_2.11', version: '2.3.1'
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.2'
SparkSession spark = SparkSession
.builder()
.appName("Java Spark SQL basic example")
// .config("spark.some.config.option", "some-value")
.getOrCreate();
Properties connectionProperties = new Properties();
connectionProperties.put("user", "busuanzi");
connectionProperties.put("password", "wxl123");
Dataset<Row> jdbcDF2 = spark.read()
.jdbc("jdbc:postgresql://192.168.56.110:5432/busuanzidb", "public.top_projects", connectionProperties);
jdbcDF2.show();
myDF2.write()
.mode(SaveMode.Append)
.jdbc("jdbc:postgresql://192.168.56.110:5432/busuanzidb", "public.top_projects", connectionProperties);
查询数据库中,可见已经写入成功了。
spark.sql("SELECT * FROM onev r JOIN twov s ON r.username = s.username")
.show();
源码获取 https://github.com/lycheeman/big-data
内容来源于网络,如有侵权,请联系作者删除!