虚拟机spark中怎样导入数据代码

虚拟机spark中怎样导入数据代码,第1张

具体 *** 作步骤:

1、准备Spark程序目录结构。

2、编辑build.sbt配置文件添加依赖。

3、创建WriteToCk.scala数据写入程序文件。

4、编译打包。

5、运行。

参数说明:your-user-name:目标ClickHouse集群中创建的数据库账号名。

your-pasword:数据库账号名对应的密码。

your-url:目标ClickHouse集群地址。

/your/path/to/test/data/a.txt:要导入的数据文件的路径,包含文件地址和文件名。说明文件中的数据及schema,需要与ClickHouse中目标表的结构保持一致。

your-table-name:ClickHouse集群中的目标表名称。

实体表说明

测试环境只有一张学生成绩表:student_scores,表中没有数据。创建表的sql如下:

create table student_scores

(

id          varchar(20),

name        nvarchar(50),

chinese     decimal(4,1),

math        decimal(4,1),

english     decimal(4,1),

PRIMARY KEY (id)

)

go

创建临时表

执行如下Sql,创建一张和student_scores结构相同的临时表#temp。

select * into #temp from student_scores

go

select * from #temp

go

更新临时表

执行如下的Sql,插入5笔数据到临时表。

insert into #temp VALUES

('70601', N'沙龙逸', 123, 148, 137),

('70602', N'刘帅', 116, 143, 140),

('70603', N'王雪', 131, 135, 144),

('70604', N'韩雨萌', 129, 133, 138),

('70605', N'杨璐', 131, 143, 144)

go

将临时表的数据插入到正式表

将一张表的数据批量插入到另外一张表,需要用到insert into select语法,可以百度关键字:sql insert into select,了解更多关于insert into select语法的帮助信息。。执行如下的sql,将临时表的数据插入到正式表。

insert into student_scores select * from #temp

go

select * from student_scores

go

更新临时表

执行如下的sql,将临时表中的语文成绩小于125分的同学加2分。

update #temp set chinese=chinese+2 where chinese<125

go

select * from #temp

go

将临时表的数据更新到正式表

将一张表的数据批理更新另外一张表,需要用到update from语法,可以百度关键字:sql update from,了解更多关于update from语法的帮助信息。执行如下的sql,将临时表的数据插入到正式表。

update student_scores

set chinese=b.chinese

from #temp b

where student_scores.id=b.id

and student_scores.chinese<>b.chinese

go

select * from student_scores

go


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11890426.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-19
下一篇2023-05-19

发表评论

登录后才能评论

评论列表(0条)

    保存