spark2.2jdbc写入mysql 的两种方法(append,Overriedwrite)-不用Mysql建表

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.{SQLContext, SaveMode}
import org.apache.spark.sql.hive.HiveContext


//spark-shell --driver-class-path /home/hadoop/hive/lib/mysql-connector-java-5.1.46.jar
object playuser {
  def main(args: Array[String]): Unit = {
    val cf = new SparkConf().setMaster("master").setAppName("NetworkWordCount")
    val sc = new SparkContext(cf)
    val sqlContext = new SQLContext(sc)
    val hc = new HiveContext(sc)
    val format = new java.text.SimpleDateFormat("yyyy-MM-dd")
    val date = format.format(new java.util.Date().getTime - 20 * 24 * 60 * 60 * 1000)
   // val lg = sc.textFile("hdfs://master:9000/data/" + date + "*/01/*.gz")
   val lg = sc.textFile("hdfs://master:9000/data/2018-05-1*/21/*.gz")

    //val date1 = format.format(("27648000000").toLong)
    val url ="jdbc:mysql://196.168.100.88:3306/sharpbi?user=biadmin&password=bi_12345"
    //val url2 = "jdbc:mysql://rds3dabp9v2v7v596tai.mysql.rds.aliyuncs.com/r2d2?user=r2d2_admin&password=Vj0kHdve3"



    // insert into mysql
    import sqlContext.implicits._



    val filed2=lg.map(l=>(
      l.split("modeType\":\"").last.split("\"").head.replace("{","null"),
      l.split("packageName\":\"").last.split("\"").head.replace("{","null"),
      l.split("siteName\":\"").last.split("\"").head.replace("{","null"),
      l.split("playType\":\"").last.split("\"").head.replace("{","null"),
      format.format(l.split("rectime\":").last.split(",").head.replace("{","27648000000").toLong),
      format.format(l.split("time\":\"").last.split("\"").head.replace("{","27648000000").toLong),
      l.split("playtime\":\"").last.split("\"").head.replace("{","null").toString,
      l.split("custom_uuid\":\"").last.split("\"").head.replace("{","null").toString
    )).toDF("modeType","packageName","siteName","playType","rectimedate","timedate","playtime","custom_uuid").registerTempTable("playuser")

    val playuser = sqlContext.sql("select modeType,packageName,siteName,playType,rectimedate,timedate,sum(playtime) as playtime,count(custom_uuid) as playstotal,count(distinct custom_uuid) customtotal from  playuser group  by modeType,packageName,siteName,playType,rectimedate,timedate")

    val prop = new java.util.Properties
//append 是增 playuser.write.mode(
"append").jdbc(url, "sharpbi.playuser", prop) // F1.write.mode("Overwrite").jdbc(url, "sharpbi.test", prop) 重新建表,覆盖原数据 // F1.insertIntoJDBC(url, "day_uv", false) val stud_scoreDF = sqlContext.read.jdbc(url,"sharpbi.playuser",prop) stud_scoreDF.count() } }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大数据学习一:大数据(离线分析)-spark写入hbase、mysql过程 Spark是一个速、通用、可扩展的大数据处理引擎,可以用于离线批处理、交互式查询和流处理等多种场景。在Spark中,我们可以使用Spark SQL、DataFrame和Dataset等API来进行数据处理和分析。 在Spark中,我们可以将数据写入到HBase和MySQL数据库中。下面是写入HBase和MySQL的过程: 1. 写入HBase (1)创建HBase表 在HBase中,我们需要先创建表,然后才能将数据写入到表中。可以使用HBase Shell或Java API来创建表。下面是使用HBase Shell创建表的示例: create 'mytable', 'cf' 其中,mytable是表名,cf是列族名。 (2)编写Spark程序 在Spark程序中,我们需要使用HBase API来将数据写入到HBase表中。下面是一个简单的示例: val conf = HBaseConfiguration.create() val table = new HTable(conf, "mytable") val put = new Put(Bytes.toBytes("rowkey")) put.add(Bytes.toBytes("cf"), Bytes.toBytes("column"), Bytes.toBytes("value")) table.put(put) 其中,conf是HBase配置对象,table是HBase表对象,put是HBase数据对象。我们可以使用put.add方法来添加数据,然后使用table.put方法将数据写入到HBase表中。 (3)运行Spark程序 在运行Spark程序之前,我们需要将HBase的相关jar包添加到Spark的classpath中。可以使用--jars参数来指定jar包的路径。下面是一个示例: spark-submit --class com.example.MyApp --jars /path/to/hbase.jar /path/to/myapp.jar 其中,MyApp是Spark程序的入口类,/path/to/hbase.jar是HBase的jar包路径,/path/to/myapp.jar是Spark程序的jar包路径。 2. 写入MySQL (1)创建MySQL表 在MySQL中,我们需要先创建表,然后才能将数据写入到表中。可以使用MySQL命令行或GUI工具来创建表。下面是使用MySQL命令行创建表的示例: CREATE TABLE mytable ( id INT PRIMARY KEY, name VARCHAR(50), age INT ); 其中,mytable是表名,id、name和age是列名。 (2)编写Spark程序 在Spark程序中,我们需要使用JDBC API来将数据写入MySQL表中。下面是一个简单的示例: val url = "jdbc:mysql://localhost:3306/mydb" val props = new Properties() props.setProperty("user", "root") props.setProperty("password", "password") val df = spark.read.format("csv").load("/path/to/data.csv") df.write.mode("append").jdbc(url, "mytable", props) 其中,url是MySQL连接字符串,props是连接属性对象,df是数据集对象。我们可以使用spark.read方法来读取数据,然后使用df.write方法将数据写入MySQL表中。 (3)运行Spark程序 在运行Spark程序之前,我们需要将MySQL的相关jar包添加到Spark的classpath中。可以使用--jars参数来指定jar包的路径。下面是一个示例: spark-submit --class com.example.MyApp --jars /path/to/mysql.jar /path/to/myapp.jar 其中,MyApp是Spark程序的入口类,/path/to/mysql.jar是MySQL的jar包路径,/path/to/myapp.jar是Spark程序的jar包路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值