Когда я запускаю приведенный ниже код, у меня возникла ошибка, которая говорит о том, что количество столбцов не совпадает. \ NСтарые имена столбцов (1): _c0 \ nНовые имена столбцов (4): Поскольку я новичок в python,Я хочу знать, как решить эту проблему:
from pyspark import SparkContext
from pyspark.sql import SparkSession
from pyspark.sql import functions as F
# create Spark context and session with necessary configuration
sc = SparkContext('local[1]', '2_program_2a_avg')
spark = SparkSession(sc)
# read as dataframe with using delimiter
df = spark.read \
.format('csv') \
.options(header='false', delimiter=' ') \
.load('/home/hduser/Desktop/sample1/avg/input_avg.csv') \
.toDF('ownerid','houseid','zipcode','value')
# select the necessary columns
df = df.select('zipcode', 'value')
# compute the average house value of each zipcode
df = df.groupBy('zipcode').agg(F.mean('value'), F.count('value')).orderBy('avg(value)', ascending=True)
# show the result
df.show()
# change it to rdd so that we can save the output as text file
df = df.rdd.saveAsTextFile('/home/hduser/Desktop/sample1/avg/')
# stop spark context
sc.stop()
ВЫХОД
*/home/hduser/PycharmProjects/Assignment2/venv/bin/python /home/hduser/PycharmProjects/HelloWorld/avg_value.py
19/11/12 10:47:49 WARN Utils: Your hostname, hadoop resolves to a loopback address: 127.0.1.1; using 10.0.2.15 instead (on interface enp0s3)
19/11/12 10:47:49 WARN Utils: Set SPARK_LOCAL_IP if you need to bind to another address
19/11/12 10:47:51 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
Traceback (most recent call last):
File "/opt/spark/python/pyspark/sql/utils.py", line 63, in deco
return f(*a, **kw)
File "/home/hduser/PycharmProjects/Assignment2/venv/lib/python3.6/site-packages/py4j/protocol.py", line 328, in get_return_value
format(target_id, ".", name), value)
py4j.protocol.Py4JJavaError: An error occurred while calling o28.toDF.
: java.lang.IllegalArgumentException: requirement failed: The number of columns doesn't match.
Old column names (1): _c0
New column names (4): ownerid, houseid, zipcode, value
at scala.Predef$.require(Predef.scala:224)
at org.apache.spark.sql.Dataset.toDF(Dataset.scala:447)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
at py4j.Gateway.invoke(Gateway.java:282)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:238)
at java.lang.Thread.run(Thread.java:748)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/hduser/PycharmProjects/HelloWorld/avg_value.py", line 15, in <module>
.toDF(*columns)
File "/opt/spark/python/pyspark/sql/dataframe.py", line 2055, in toDF
jdf = self._jdf.toDF(self._jseq(cols))
File "/home/hduser/PycharmProjects/Assignment2/venv/lib/python3.6/site-packages/py4j/java_gateway.py", line 1257, in __call__
answer, self.gateway_client, self.target_id, self.name)
File "/opt/spark/python/pyspark/sql/utils.py", line 79, in deco
raise IllegalArgumentException(s.split(': ', 1)[1], stackTrace)
pyspark.sql.utils.IllegalArgumentException: "requirement failed: The number of columns doesn't match.\nOld column names (1): _c0\nNew column names (4): ownerid, houseid, zipcode, value"
Process finished with exit code 1*