spark sql 讀取mysql 數(shù)據(jù)庫和寫入mysql數(shù)據(jù)庫時伸头,
dataframe 字段比表字段多一個 直接寫入會報錯;
將表添加一個字段后 讓DataFrame 與表的字段形式統(tǒng)一后面哼,則可以統(tǒng)一插入精绎;
dataframe 字段比表字段少一個 其余的一樣 仍然可以寫入成功锌妻。
udf函數(shù)寫的時候 首先注冊 ?即便傳參為空 注冊的時候仍然使用 _ ?使用的時候依據(jù)參數(shù)進行傳參仿粹,如果沒有參數(shù),仍然使用()
def getNowStr():String = {
val sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
sf.format(System.currentTimeMillis())
}
val nowStr = udf(DateTimeUtil.getNowStr _)
df.withColumn("receivetime",nowStr())
secure CRT ?
x shell
createtableselect 會將原表中的數(shù)據(jù)完整復(fù)制一份原茅,但表結(jié)構(gòu)中的索引會丟失吭历。
createtablelike只會完整復(fù)制原表的建表語句擂橘,但不會復(fù)制數(shù)據(jù)
create table if not exists t1 like t2
insert into t2(a,b) select a,b from t1 where t1.recordtime > '2017-04-12 00:00:00' 表存在
create table t2 as select * from t1 where t1.recordtime > '2017-04-12 00:00:00'
select * into t2 from? t1 where t1.recordtime > '2017-04-12 00:00:00' 表不存在
delete t1 from t1,t2 where t1.recordtime > t2.recordtime
delete t1 from t1,(select min(recordtime) as minrt from t1) t2 where t1.recordtime >= t2.minrt
and t1.recordtime <= TIMESTAMPADD(day,-7,CURRENT_DATE())
TIMESTAMP(REPLACE(recordtime,"/","-"))
rename tab1 to tb2