Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Use with Spark","local":"use-with-spark","sections":[{"title":"Load from Spark","local":"load-from-spark","sections":[{"title":"Caching","local":"caching","sections":[],"depth":3},{"title":"Feature types","local":"feature-types","sections":[],"depth":3}],"depth":2}],"depth":1}"> | |
| <link href="/docs/datasets/main/en/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload"> | |
| <link rel="modulepreload" href="/docs/datasets/main/en/_app/immutable/entry/start.4d44eea4.js"> | |
| <link rel="modulepreload" href="/docs/datasets/main/en/_app/immutable/chunks/scheduler.bdbef820.js"> | |
| <link rel="modulepreload" href="/docs/datasets/main/en/_app/immutable/chunks/singletons.36b689ad.js"> | |
| <link rel="modulepreload" href="/docs/datasets/main/en/_app/immutable/chunks/index.8a885b74.js"> | |
| <link rel="modulepreload" href="/docs/datasets/main/en/_app/immutable/chunks/paths.27092e28.js"> | |
| <link rel="modulepreload" href="/docs/datasets/main/en/_app/immutable/entry/app.d83067e8.js"> | |
| <link rel="modulepreload" href="/docs/datasets/main/en/_app/immutable/chunks/index.c0aea24a.js"> | |
| <link rel="modulepreload" href="/docs/datasets/main/en/_app/immutable/nodes/0.bfb01985.js"> | |
| <link rel="modulepreload" href="/docs/datasets/main/en/_app/immutable/chunks/each.e59479a4.js"> | |
| <link rel="modulepreload" href="/docs/datasets/main/en/_app/immutable/nodes/50.f6e0733d.js"> | |
| <link rel="modulepreload" href="/docs/datasets/main/en/_app/immutable/chunks/Tip.31005f7d.js"> | |
| <link rel="modulepreload" href="/docs/datasets/main/en/_app/immutable/chunks/CodeBlock.6ccca92e.js"> | |
| <link rel="modulepreload" href="/docs/datasets/main/en/_app/immutable/chunks/EditOnGithub.725ee0c1.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{"title":"Use with Spark","local":"use-with-spark","sections":[{"title":"Load from Spark","local":"load-from-spark","sections":[{"title":"Caching","local":"caching","sections":[],"depth":3},{"title":"Feature types","local":"feature-types","sections":[],"depth":3}],"depth":2}],"depth":1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <h1 class="relative group"><a id="use-with-spark" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#use-with-spark"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Use with Spark</span></h1> <p data-svelte-h="svelte-n01qzp">This document is a quick introduction to using 🤗 Datasets with Spark, with a particular focus on how to load a Spark DataFrame into a <a href="/docs/datasets/main/en/package_reference/main_classes#datasets.Dataset">Dataset</a> object.</p> <p data-svelte-h="svelte-1pvlf1d">From there, you have fast access to any element and you can use it as a data loader to train models.</p> <h2 class="relative group"><a id="load-from-spark" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#load-from-spark"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Load from Spark</span></h2> <p data-svelte-h="svelte-1qkrgz7">A <a href="/docs/datasets/main/en/package_reference/main_classes#datasets.Dataset">Dataset</a> object is a wrapper of an Arrow table, which allows fast reads from arrays in the dataset to PyTorch, TensorFlow and JAX tensors. | |
| The Arrow table is memory mapped from disk, which can load datasets bigger than your available RAM.</p> <p data-svelte-h="svelte-1r572x2">You can get a <a href="/docs/datasets/main/en/package_reference/main_classes#datasets.Dataset">Dataset</a> from a Spark DataFrame using <code>Dataset.from_spark()</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> Dataset | |
| <span class="hljs-meta">>>> </span>df = spark.createDataFrame( | |
| <span class="hljs-meta">... </span> data=[[<span class="hljs-number">1</span>, <span class="hljs-string">"Elia"</span>], [<span class="hljs-number">2</span>, <span class="hljs-string">"Teo"</span>], [<span class="hljs-number">3</span>, <span class="hljs-string">"Fang"</span>]], | |
| <span class="hljs-meta">... </span> columns=[<span class="hljs-string">"id"</span>, <span class="hljs-string">"name"</span>], | |
| <span class="hljs-meta">... </span>) | |
| <span class="hljs-meta">>>> </span>ds = Dataset.from_spark(df)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-kc65ui">The Spark workers write the dataset on disk in a cache directory as Arrow files, and the <a href="/docs/datasets/main/en/package_reference/main_classes#datasets.Dataset">Dataset</a> is loaded from there.</p> <p data-svelte-h="svelte-eu4ytu">Alternatively, you can skip materialization by using <code>IterableDataset.from_spark()</code>, which returns an <a href="/docs/datasets/main/en/package_reference/main_classes#datasets.IterableDataset">IterableDataset</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> IterableDataset | |
| <span class="hljs-meta">>>> </span>df = spark.createDataFrame( | |
| <span class="hljs-meta">... </span> data=[[<span class="hljs-number">1</span>, <span class="hljs-string">"Elia"</span>], [<span class="hljs-number">2</span>, <span class="hljs-string">"Teo"</span>], [<span class="hljs-number">3</span>, <span class="hljs-string">"Fang"</span>]], | |
| <span class="hljs-meta">... </span> columns=[<span class="hljs-string">"id"</span>, <span class="hljs-string">"name"</span>], | |
| <span class="hljs-meta">... </span>) | |
| <span class="hljs-meta">>>> </span>ds = IterableDataset.from_spark(df) | |
| <span class="hljs-meta">>>> </span><span class="hljs-built_in">print</span>(<span class="hljs-built_in">next</span>(<span class="hljs-built_in">iter</span>(ds))) | |
| {<span class="hljs-string">"id"</span>: <span class="hljs-number">1</span>, <span class="hljs-string">"name"</span>: <span class="hljs-string">"Elia"</span>}<!-- HTML_TAG_END --></pre></div> <h3 class="relative group"><a id="caching" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#caching"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Caching</span></h3> <p data-svelte-h="svelte-176k11x">When using <code>Dataset.from_spark()</code>, the resulting <a href="/docs/datasets/main/en/package_reference/main_classes#datasets.Dataset">Dataset</a> is cached; if you call <code>Dataset.from_spark()</code> multiple | |
| times on the same DataFrame it won’t re-run the Spark job that writes the dataset as Arrow files on disk.</p> <p data-svelte-h="svelte-n6wfk7">You can set the cache location by passing <code>cache_dir=</code> to <code>Dataset.from_spark()</code>. | |
| Make sure to use a disk that is available to both your workers and your current machine (the driver).</p> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p data-svelte-h="svelte-1m6gjz4">In a different session, a Spark DataFrame doesn’t have the same <a href="https://spark.apache.org/docs/3.2.0/api/python/reference/api/pyspark.sql.DataFrame.semanticHash.html" rel="nofollow">semantic hash</a>, and it will rerun a Spark job and store it in a new cache.</p></div> <h3 class="relative group"><a id="feature-types" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#feature-types"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Feature types</span></h3> <p data-svelte-h="svelte-9b4dmt">If your dataset is made of images, audio data or N-dimensional arrays, you can specify the <code>features=</code> argument in | |
| <code>Dataset.from_spark()</code> (or <code>IterableDataset.from_spark()</code>):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> Dataset, Features, Image, Value | |
| <span class="hljs-meta">>>> </span>data = [(<span class="hljs-number">0</span>, <span class="hljs-built_in">open</span>(<span class="hljs-string">"image.png"</span>, <span class="hljs-string">"rb"</span>).read())] | |
| <span class="hljs-meta">>>> </span>df = spark.createDataFrame(data, <span class="hljs-string">"idx: int, image: binary"</span>) | |
| <span class="hljs-meta">>>> </span><span class="hljs-comment"># Also works if you have arrays</span> | |
| <span class="hljs-meta">>>> </span><span class="hljs-comment"># data = [(0, np.zeros(shape=(32, 32, 3), dtype=np.int32).tolist())]</span> | |
| <span class="hljs-meta">>>> </span><span class="hljs-comment"># df = spark.createDataFrame(data, "idx: int, image: array<array<array<int>>>")</span> | |
| <span class="hljs-meta">>>> </span>features = Features({<span class="hljs-string">"idx"</span>: Value(<span class="hljs-string">"int64"</span>), <span class="hljs-string">"image"</span>: Image()}) | |
| <span class="hljs-meta">>>> </span>dataset = Dataset.from_spark(df, features=features) | |
| <span class="hljs-meta">>>> </span>dataset[<span class="hljs-number">0</span>] | |
| {<span class="hljs-string">'idx'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'image'</span>: <PIL.PngImagePlugin.PngImageFile image mode=RGB size=32x32>}<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-iwja1a">You can check the <a href="/docs/datasets/main/en/package_reference/main_classes#datasets.Features">Features</a> documentation to know about all the feature types available.</p> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/datasets/blob/main/docs/source/use_with_spark.mdx" target="_blank"><span data-svelte-h="svelte-1kd6by1"><</span> <span data-svelte-h="svelte-x0xyl0">></span> <span data-svelte-h="svelte-1dajgef"><span class="underline ml-1.5">Update</span> on GitHub</span></a> <p></p> | |
| <script> | |
| { | |
| __sveltekit_w3org2 = { | |
| assets: "/docs/datasets/main/en", | |
| base: "/docs/datasets/main/en", | |
| env: {} | |
| }; | |
| const element = document.currentScript.parentElement; | |
| const data = [null,null]; | |
| Promise.all([ | |
| import("/docs/datasets/main/en/_app/immutable/entry/start.4d44eea4.js"), | |
| import("/docs/datasets/main/en/_app/immutable/entry/app.d83067e8.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 50], | |
| data, | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 18.2 kB
- Xet hash:
- 02b7392b2f4f49a867128e68f804a13686f569ff17ce2f113448e9970774a9f9
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.