RDD DataFrame Dataset相互轉化

2021-10-03 20:12:44 字數 1642 閱讀 7669

初始化sparksession,底層初始化sparkcontext,相當於spark任務的入口。

val sparksession = sparksession.

builder()

.("sparktest").

master

("local[2]").

getorcreate()

val sparkcontext = sparksession.sparkcontext

val log = sparkcontext.

textfile

("/users/mike/desktop/test.txt"

)

case class
case

class

person

(name:string,age:int)

//轉化為rdd[person]

val rowrdd = log.

map(_.

split

(" "))

.map

(x =

>

person(x

(0),

x(1)

.toint)

)import sparksession.implicits._

val df = rowrdd.

todf()

df.show()

sparksession.

stop

()

schema(structtype的方式)
val schemafiled =

"name,age"

val schemastring = schemafiled.

split

(","

)val schema =

structtype

(array

(structfield

(schemastring(0

), stringtype, nullable =

true),

structfield

(schemastring(1

), integertype, nullable =

true))

)//生成rdd[row]

val rowrdd = log.

map(_.

split

(" "))

.map

(x =

>

row(x(

0),x

(1).toint)

)//格式和rdd 構造dataframe

val df = sparksession.

createdataframe

(rowrdd, schema)

df.show()

sparksession.

stop

()

tods()方法將rdd轉化為dataset

todf()將dataset轉化為dataframe

df.as[person]將dataframe轉化為dataset

***.rdd()將其他格式的資料轉化為rdd

latex word相互轉換

有時還是不得不把 latex 寫的文件轉成 word。一些可行的辦法有 1 先用 tth 或 latex2html 之類的工具把 latex 轉成 html,再把 html 轉成word。2 先生成 pdf,再用 pdf2word 轉成word。3 安裝 tex2word 用 word 開啟 lat...

型別相互轉化

1.short int exp short shortvar 0 int intvar 0 shortvar short intvar 2.int short exp short shortvar 0 int intvar 0 intvar shortvar 3.int string exp int...

IplImag HImage相互轉換

iplimage himagetoiplimage hobject hobj if htchannels 0 i 3 cvmerge pimageblue,pimagegreen,pimagered,null,pimage cvreleaseimage pimagered cvreleaseimag...