0001 ---
0002 layout: global
0003 title: Binary File Data Source
0004 displayTitle: Binary File Data Source
0005 license: |
0006 Licensed to the Apache Software Foundation (ASF) under one or more
0007 contributor license agreements. See the NOTICE file distributed with
0008 this work for additional information regarding copyright ownership.
0009 The ASF licenses this file to You under the Apache License, Version 2.0
0010 (the "License"); you may not use this file except in compliance with
0011 the License. You may obtain a copy of the License at
0012
0013 http://www.apache.org/licenses/LICENSE-2.0
0014
0015 Unless required by applicable law or agreed to in writing, software
0016 distributed under the License is distributed on an "AS IS" BASIS,
0017 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0018 See the License for the specific language governing permissions and
0019 limitations under the License.
0020 ---
0021
0022 Since Spark 3.0, Spark supports binary file data source,
0023 which reads binary files and converts each file into a single record that contains the raw content
0024 and metadata of the file.
0025 It produces a DataFrame with the following columns and possibly partition columns:
0026 * `path`: StringType
0027 * `modificationTime`: TimestampType
0028 * `length`: LongType
0029 * `content`: BinaryType
0030
0031 To read whole binary files, you need to specify the data source `format` as `binaryFile`.
0032 To load files with paths matching a given glob pattern while keeping the behavior of partition discovery,
0033 you can use the general data source option `pathGlobFilter`.
0034 For example, the following code reads all PNG files from the input directory:
0035
0036 <div class="codetabs">
0037 <div data-lang="scala" markdown="1">
0038 {% highlight scala %}
0039
0040 spark.read.format("binaryFile").option("pathGlobFilter", "*.png").load("/path/to/data")
0041
0042 {% endhighlight %}
0043 </div>
0044
0045 <div data-lang="java" markdown="1">
0046 {% highlight java %}
0047
0048 spark.read().format("binaryFile").option("pathGlobFilter", "*.png").load("/path/to/data");
0049
0050 {% endhighlight %}
0051 </div>
0052 <div data-lang="python" markdown="1">
0053 {% highlight python %}
0054
0055 spark.read.format("binaryFile").option("pathGlobFilter", "*.png").load("/path/to/data")
0056
0057 {% endhighlight %}
0058 </div>
0059 <div data-lang="r" markdown="1">
0060 {% highlight r %}
0061
0062 read.df("/path/to/data", source = "binaryFile", pathGlobFilter = "*.png")
0063
0064 {% endhighlight %}
0065 </div>
0066 </div>
0067
0068 Binary file data source does not support writing a DataFrame back to the original files.