加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – java.lang.RuntimeException:java.lang.String不是big

发布时间:2020-12-16 19:21:25 所属栏目:安全 来源:网络整理
导读:我正在从文本文件中读取数据框架的模式.该文件看起来像 id,1,bigintprice,2,bigintsqft,3,bigintzip_id,4,intname,5,string 我将解析后的数据类型映射到Spark Sql数据类型.创建数据框的代码是 – var schemaSt = new ListBuffer[(String,String)]()// read s
我正在从文本文件中读取数据框架的模式.该文件看起来像

id,1,bigint
price,2,bigint
sqft,3,bigint
zip_id,4,int
name,5,string

我将解析后的数据类型映射到Spark Sql数据类型.创建数据框的代码是 –

var schemaSt = new ListBuffer[(String,String)]()
// read schema from file
for (line <- Source.fromFile("meta.txt").getLines()) {
  val word = line.split(",")
  schemaSt += ((word(0),word(2)))
}

// map datatypes
val types = Map("int" -> IntegerType,"bigint" -> LongType)
      .withDefault(_ => StringType)
val schemaChanged = schemaSt.map(x => (x._1,types(x._2))

// read data source
val lines = spark.sparkContext.textFile("data source path")

val fields = schemaChanged.map(x => StructField(x._1,x._2,nullable = true)).toList

val schema = StructType(fields)

val rowRDD = lines
  .map(_.split("t"))
  .map(attributes => Row.fromSeq(attributes))

// Apply the schema to the RDD
val new_df = spark.createDataFrame(rowRDD,schema)
new_df.show(5)
new_df.printSchema()

但以上只适用于StringType.对于IntegerType和LongType,它抛出异常 –

java.lang.RuntimeException: java.lang.String is not a valid external type for schema of int

java.lang.RuntimeException: java.lang.String is not a valid external type for schema of bigint.

提前致谢!

解决方法

您正在尝试将字符串存储在数字类型的列中.

您需要在解析时将字符串编码的数字数据转换为适当的数字类型.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读