Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 12 additions & 4 deletions python/pyspark/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,16 @@ 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
RDD of Strings.

If use_unicode is False, the strings will be kept as `str` (encoding
as `utf-8`), which is faster and smaller than unicode. (Added in
Spark 1.2)

>>> path = os.path.join(tempdir, "sample-text.txt")
>>> with open(path, "w") as testFile:
... testFile.write("Hello world!")
Expand All @@ -329,16 +333,20 @@ 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
URI. Each file is read as a single record and returned in a
key-value pair, where the key is the path of each file, the
value is the content of each file.

If use_unicode is False, the strings will be kept as `str` (encoding
as `utf-8`), which is faster and smaller than unicode. (Added in
Spark 1.2)

For example, if you have the following files::

hdfs://a-hdfs-path/part-00000
Expand Down Expand Up @@ -369,7 +377,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
18 changes: 11 additions & 7 deletions python/pyspark/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,18 +409,22 @@ 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')
s = stream.read(length)
return s.decode("utf-8") if self.use_unicode else s

def load_stream(self, stream):
while True:
try:
try:
while True:
yield self.loads(stream)
except struct.error:
return
except EOFError:
return
except struct.error:
return
except EOFError:
return


def read_long(stream):
Expand Down