sparksql数据类型转换_SparkSql数据类型转换
前⾔
数据类型转换这个在任何语⾔框架中都会涉及到,看起来⾮常简单,不过要把所有的数据类型都掌握还是需要⼀定的时间历练的
SparkSql数据类型
数字类型
ByteType:代表⼀个字节的整数。范围是-128到127
ShortType:代表两个字节的整数。范围是-32768到32767
IntegerType:代表4个字节的整数。范围是-2147483648到2147483647
LongType:代表8个字节的整数。范围是-9223372036854775808到9223372036854775807
FloatType:代表4字节的单精度浮点数
DoubleType:代表8字节的双精度浮点数
DecimalType:代表任意精度的10进制数据。通过内部的java.math.BigDecimal⽀持。BigDecimal由⼀个任意精度的整型⾮标度值和⼀个32位整数组成
StringType:代表⼀个字符串值
BinaryType:代表⼀个byte序列值
BooleanType:代表boolean值
Datetime类型
TimestampType:代表包含字段年,⽉,⽇,时,分,秒的值
DateType:代表包含字段年,⽉,⽇的值
复杂类型
ArrayType(elementType, containsNull):代表由elementType类型元素组成的序列值。containsNull⽤来指明ArrayType中的值是否有null值
MapType(keyType, valueType, valueContainsNull):表⽰包括⼀组键 - 值对的值。通过keyType表⽰k
ey数据的类型,通过valueType 表⽰value数据的类型。valueContainsNull⽤来指明MapType中的值是否有null值
StructType(fields):表⽰⼀个拥有StructFields (fields)序列结构的值
StructField(name, dataType, nullable):代表StructType中的⼀个字段,字段的名字通过name指定,dataType指定field的数据类
型,nullable表⽰字段的值是否有null值。
Spark Sql数据类型和Scala数据类型对⽐
sparksql 数据类型
scala数据类型
ByteType
Byte
ShortType
Short
IntegerType
Int
LongType
Long
FloatType
Float
DoubleType
Double
DecimalType
scala.math.BigDecimal
StringType
String
BinaryType
Array[Byte]
BooleanType
Boolean
TimestampType
java.sql.Timestamp
DateType
java.sql.Datehtml文件怎么制作
ArrayType
MapType
StructType
org.apache.spark.sql.Row
StructField
The value type in Scala of the data type of this field (For example, Int for a StructField with the data type IntegerType) Spark Sql数据类型转换案例
⼀句话描述:调⽤Column类的cast⽅法
如何获取Column类
这个之前写过
df("columnName") // On a specific `df` DataFrame.
col("columnName") // A generic column not yet associated with a DataFrame.
col("columnName.field") // Extracting a struct field
col("`a.column.with.dots`") // Escape `.` in column names. $"columnName" // Scala short hand for a named column.测试数据准备
1,tom,23
2,jack,24
3,lily,18
4,lucy,19db数据库文件损坏如何修复
spark⼊⼝代码
val spark = SparkSession
.builder()
.appName("test")
.master("local[*]")
.getOrCreate()
测试默认数据类型
textFile("./data/user")
.map(_.split(","))
.map(x => (x(0), x(1), x(2)))
.toDF("id", "name", "age")
.dtypes
.foreach(println)
声母c的教学视频结果:
文件格式转换app
(id,StringType)
(name,StringType)
(age,StringType)
说明默认都是StringType类型
system pause头文件把数值型的列转为IntegerType
import spark.implicits._
textFile("./data/user")
.map(_.split(","))
.map(x => (x(0), x(1), x(2)))
.toDF("id", "name", "age")
.
select($"id".cast("int"), $"name", $"age".cast("int"))
.dtypes
.foreach(println)
结果:
(id,IntegerType)
(name,StringType)
(age,IntegerType)
Column类cast⽅法的两种重载
第⼀种杭电oj没做出来的题怎么办
def cast(to: String): Column
Casts the column to a different data type, using the canonical string representation of the type. The supported types are: string, boolean, byte, short, int, long, float, double, decimal, date, timestamp.
// Casts colA to integer.
df.select(df("colA").cast("int"))
Since
1.3.0
第⼆种
def cast(to: DataType): Column
Casts the column to a different data type.
// Casts colA to IntegerType.
import org.apache.pes.IntegerType
df.select(df("colA").cast(IntegerType))
// equivalent to
df.select(df("colA").cast("int"))

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。