我将 Spark 与 python 一起使用。上传 csv 文件后,我需要解析 csv 文件中的一列,其中包含 22 位数字长的数字。为了解析该列,我使用了LongType()。我使用 map() 函数来定义列。以下是我在 pyspark 中的命令。
>>> test=sc.textFile("test.csv")
>>> header=test.first()
>>> schemaString = header.replace('"','')
>>> testfields = [StructField(field_name, StringType(), True) for field_name in schemaString.split(',')]
>>> testfields[5].dataType = LongType()
>>> testschema = StructType(testfields)
>>> testHeader = test.filter(lambda l: "test_date" in l)
>>> testNoHeader = test.subtract(testHeader)
>>> test_temp = testNoHeader.map(lambda k: k.split(",")).map(lambda
p:(p[0],p[1],p[2],p[3],p[4],***float(p[5].strip('"'))***,p[6],p[7]))
>>> test_temp.top(2)
Run Code Online (Sandbox Code Playgroud)
注意:我还尝试在变量test_temp中使用“long”和“bigint”代替“float” ,但 Spark 中的错误是“找不到关键字”,以下是输出
[('2012-03-14', '7', '1698.00', 'XYZ02abc008793060653', 'II93', ***8.27370028700801e+21*** , 'W0W0000000000007', '879870080088815007'), ('2002-03-14', '1', …Run Code Online (Sandbox Code Playgroud)