Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-3047] [PySpark] add an option to use str in textFileRDD #1951

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions python/pyspark/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def pickleFile(self, name, minPartitions=None):
return RDD(self._jsc.objectFile(name, minPartitions), self,
BatchedSerializer(PickleSerializer()))

def textFile(self, name, minPartitions=None):
def textFile(self, name, minPartitions=None, use_unicode=True):
"""
Read a text file from HDFS, a local file system (available on all
nodes), or any Hadoop-supported file system URI, and return it as an
Expand All @@ -329,9 +329,9 @@ def textFile(self, name, minPartitions=None):
"""
minPartitions = minPartitions or min(self.defaultParallelism, 2)
return RDD(self._jsc.textFile(name, minPartitions), self,
UTF8Deserializer())
UTF8Deserializer(use_unicode))

def wholeTextFiles(self, path, minPartitions=None):
def wholeTextFiles(self, path, minPartitions=None, use_unicode=True):
"""
Read a directory of text files from HDFS, a local file system
(available on all nodes), or any Hadoop-supported file system
Expand Down Expand Up @@ -369,7 +369,7 @@ def wholeTextFiles(self, path, minPartitions=None):
"""
minPartitions = minPartitions or self.defaultMinPartitions
return RDD(self._jsc.wholeTextFiles(path, minPartitions), self,
PairDeserializer(UTF8Deserializer(), UTF8Deserializer()))
PairDeserializer(UTF8Deserializer(use_unicode), UTF8Deserializer(use_unicode)))

def _dictToJavaMap(self, d):
jm = self._jvm.java.util.HashMap()
Expand Down
23 changes: 15 additions & 8 deletions python/pyspark/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,18 +409,25 @@ class UTF8Deserializer(Serializer):
Deserializes streams written by String.getBytes.
"""

def __init__(self, use_unicode=False):
self.use_unicode = use_unicode

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how we'll we've stuck to this convention in the existing code, but my original intention was that loads(loaded a single record and load_stream loaded a stream of records. If you wanted, we could conditionally define loads based on whether we've set use_unicode, which would allow the serializer to be used to deserialize an individual element or a stream.

def loads(self, stream):
length = read_int(stream)
return stream.read(length).decode('utf8')
return stream.read(length)

def load_stream(self, stream):
while True:
try:
yield self.loads(stream)
except struct.error:
return
except EOFError:
return
try:
if self.use_unicode:
while True:
yield self.loads(stream).decode("utf-8")
else:
while True:
yield self.loads(stream)
except struct.error:
return
except EOFError:
return


def read_long(stream):
Expand Down