pyspark到spark scala转换

2izufjch  于 2021-05-19  发布在  Spark
关注(0)|答案(1)|浏览(565)

各位开发者,
我正在创建动态定长文件读取函数——模式将来自json文件:我的代码语言是:scala,因为大多数现有代码已经用scala编写。
在浏览时,我找到了我需要的用pyspark编写的精确代码。你能帮我把它转换成相应的spark scala代码吗?特别是字典部分和循环部分
主要参考:使用pyspark中json文件的模式读取固定宽度的文件

SchemaFile.json
===========================
{"Column":"id","From":"1","To":"3"}
{"Column":"date","From":"4","To":"8"}
{"Column":"name","From":"12","To":"3"}
{"Column":"salary","From":"15","To":"5"}

File = spark.read\
    .format("csv")\
    .option("header","false")\
    .load("C:\Temp\samplefile.txt")

SchemaFile = spark.read\
    .format("json")\
    .option("header","true")\
    .json('C:\Temp\schemaFile\schema.json')

sfDict = map(lambda x: x.asDict(), SchemaFile.collect())
print(sfDict)

# [{'Column': u'id', 'From': u'1', 'To': u'3'},

# {'Column': u'date', 'From': u'4', 'To': u'8'},

# {'Column': u'name', 'From': u'12', 'To': u'3'},

# {'Column': u'salary', 'From': u'15', 'To': u'5'}

from pyspark.sql.functions import substring
File.select(
    *[
        substring(
            str='_c0',
            pos=int(row['From']),
            len=int(row['To'])
        ).alias(row['Column']) 
        for row in sfDict
    ]
).show()
e4yzc0pl

e4yzc0pl1#

检查以下代码。

scala> df.show(false)
+--------------------+
|value               |
+--------------------+
|00120181120xyz12341 |
|00220180203abc56792 |
|00320181203pqr25483 |
+--------------------+
scala> schema.show(false)
+------+----+---+
|Column|From|To |
+------+----+---+
|id    |1   |3  |
|date  |4   |8  |
|name  |12  |3  |
|salary|15  |5  |
+------+----+---+
scala> :paste
// Entering paste mode (ctrl-D to finish)

val columns = schema
.withColumn("id",lit(1))
.groupBy($"id")
.agg(collect_list(concat(lit("substring(value,"),$"from",lit(","),$"to",lit(") as "),$"column")).as("data"))
.withColumn("data",explode($"data"))
.select($"data")
.map(_.getAs[String](0))
.collect

// Exiting paste mode, now interpreting.

columns: Array[String] = Array(substring(value,1,3) as id, substring(value,4,8) as date, substring(value,12,3) as name, substring(value,15,5) as salary)
scala> df.selectExpr(columns:_*).show(false)
+---+--------+----+------+
|id |date    |name|salary|
+---+--------+----+------+
|001|20181120|xyz |12341 |
|002|20180203|abc |56792 |
|003|20181203|pqr |25483 |
+---+--------+----+------+

相关问题