python - 如何使用 pyspark 从 CSV 中使用 Spark 在镶木地板中设置正确的数据类型
问题描述
我有一个 csv 文件,看起来像:
39813458,13451345,14513,SomeText,344564,Some other text,328984,"[{""field_int_one"":""16784832510"",""second_int_field"":""84017"",""third_int_field"":""245"",""some_timestamp_one"":""2018-04-17T23:54:34.000Z"",""some_other_timestamp"":""2018-03-03T15:34:04.000Z"",""one_more_int_field"":0,},{""field_int_one"":""18447548326"",""second_int_field"":""04965"",""third_int_field"":""679"",""some_timestamp_one"":""2018-02-06T03:39:12.000Z"",""some_other_timestamp"":""2018-03-01T09:19:12.000Z"",""one_more_int_field"":0}]"
我正在将其转换为镶木地板
from pyspark import SparkContext
from pyspark.sql import SQLContext
from pyspark.sql.types import *
from pyspark.sql.session import SparkSession
sc = SparkContext('local')
spark = SparkSession(sc)
if __name__ == "__main__":
sqlContext = SQLContext(sc)
schema = StructType([
StructField("first_int", IntegerType(), True),
StructField("second_int", IntegerType(), True),
StructField("third_int", IntegerType(), True),
StructField("first_string_field", StringType(), True),
StructField("fourth_int", IntegerType(), True),
StructField("second_string_field", StringType(), True),
StructField("last_int_field", StringType(), True),
StructField("json_field", StringType(), True)])
rdd = spark.read.schema(schema).csv("source_file.csv")
rdd.write.parquet('parquet_output')
它可以工作并对其进行转换,但是如果您.printSchema
在查询它时执行一次,它显然会将其定义打印为字符串。如何正确地将最后一个字段声明为 Json?
解决方案
我认为嵌套ArrayType
适用于这种模式
schema = StructType([
StructField("first_int", IntegerType(), True),
StructField("second_int", IntegerType(), True),
StructField("third_int", IntegerType(), True),
StructField("first_string_field", StringType(), True),
StructField("fourth_int", IntegerType(), True),
StructField("second_string_field", StringType(), True),
StructField("last_int_field", StringType(), True),
StructField("json_field", ArrayType(
StructType() \
.add("field_int_one", IntegerType()) \
.add("field_string_one", StringType()) \
.addMoreFieldsHere),
True)])
推荐阅读
- python - 计算 Python 中元素的出现次数
- regex - 提取电话号码
- typescript - 无法使 tsconfig 路径正常工作 (TS2307)
- typescript - Docker 中的打字稿:Visual Studio 代码主机打字稿编译器中的“找不到模块”
- java - PhoneAuthProvider.OnVerificationStateChangedCallbacks 没有被解雇
- python - 在新的内部窗口 django、python 中打开链接
- python - 我很困惑为什么这不会打印我的文本文件
- java - 以另一种方法返回更新值
- algorithm - Minizinc 错误:无效的类型说明:预期的 `float',实际的 `var float'
- python - 如何在 Python 中为每个日期绘制图表