Reputation: 2881
I have the following folder of partitioned data-
my_folder
|--part-0000.gzip
|--part-0001.gzip
|--part-0002.gzip
|--part-0003.gzip
I try to read this data into a dataframe using-
>>> my_df = spark.read.csv("/path/to/my_folder/*")
>>> my_df.show(5)
+--------------------+
| _c0|
+--------------------+
|��[I���...|
|��RUu�[*Ք��g��T...|
|�t��� �qd��8~��...|
|�(���b4�:������I�...|
|���!y�)�PC��ќ\�...|
+--------------------+
only showing top 5 rows
Also tried using this to check the data-
>>> rdd = sc.textFile("/path/to/my_folder/*")
>>> rdd.take(4)
['\x1f�\x08\x00\x00\x00\x00\x00\x00\x00�͎\\ǖ�7�~�\x04�\x16��\'��"b�\x04�AR_<G��"u��\x06��L�*�7�J�N�\'�qa��\x07\x1ey��\x0b\\�\x13\x0f\x0c\x03\x1e�Q��ڏ�\x15Y_Yde��Y$��Q�JY;s�\x1d����[��\x15k}[B\x01��ˀ�PT��\x12\x07-�\x17\x12�\x0c#\t���T۱\x01yf��\x14�S\x0bc)��\x1ex���axAO˓_\'��`+HM҈�\x12�\x17�@']
NOTE: When I do a zcat part-0000.gzip | head -1
to read the file content, it shows the data is tab separated and in plain readable English.
How do I read these files properly into a dataframe?
Upvotes: 3
Views: 2449
Reputation: 2881
For some reason, Spark does not recognize the .gzip
file extension. So I had to change the file extensions before reading the partitioned data-
import os
# go to my_folder
os.chdir("/path/to/my_folder")
# renaming all `.gzip` extensions to `.gz` within my_folder
cmd = 'rename "s/gzip/gz/" *.gzip'
result_code = os.system(cmd)
if result_code == 0:
print("Successfully renamed the file extensions!")
# finally reading the data into a dataframe
my_df = spark.read.csv("/path/to/my_folder/*", sep="\t")
else:
print("Could not rename the file extensions!")
Upvotes: 2