Unable to save the file in sequence file format


#1

Hello,

I am trying to save an RDD in the sequence file format but i am getting error message:

below is the code:
data_mapped = data.map(lambda i : (str(i[0])+"\t"+str(i[1])+"\t"+str(i[2])+"\t"+str(i[3])))

data_mapped.saveAsSequenceFile("/user/pramod882/problem5/sequence")

below is the error message:
19/01/12 21:07:31 INFO DAGScheduler: Job 1 finished: take at SerDeUtil.scala:231, took 3.052491 s
Traceback (most recent call last):
File “”, line 1, in
File “/usr/hdp/current/spark-client/python/pyspark/rdd.py”, line 1437, in saveAsSequenceFile
path, compressionCodecClass)
File “/usr/hdp/current/spark-client/python/lib/py4j-0.9-src.zip/py4j/java_gateway.py”, line 813, in call
File “/usr/hdp/current/spark-client/python/pyspark/sql/utils.py”, line 45, in deco
return f(*a, **kw)
File “/usr/hdp/current/spark-client/python/lib/py4j-0.9-src.zip/py4j/protocol.py”, line 308, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling z:org.apache.spark.api.python.PythonRDD.saveAsSequenceFile.
: org.apache.spark.SparkException: RDD element of type java.lang.String cannot be used
at org.apache.spark.api.python.SerDeUtil$.pythonToPairRDD(SerDeUtil.scala:236)
at org.apache.spark.api.python.PythonRDD$.saveAsHadoopFile(PythonRDD.scala:745)
at org.apache.spark.api.python.PythonRDD$.saveAsSequenceFile(PythonRDD.scala:720)
at org.apache.spark.api.python.PythonRDD.saveAsSequenceFile(PythonRDD.scala)
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:231)
at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:381)
at py4j.Gateway.invoke(Gateway.java:259)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:133)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:209)
at java.lang.Thread.run(Thread.java:745)


#2

@hpramodn Sequence files are used to store key-value pairs so you cannot simply store RDD[String]

If you want to keep whole strings just use None keys and Try:

data_mapped.map(lambda x: (None, x)).saveAsSequenceFile("/user/pramod882/problem5/sequence")