-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
257 lines (229 loc) · 10.4 KB
/
index.html
File metadata and controls
257 lines (229 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="description" content="None" />
<link rel="shortcut icon" href="img/favicon.ico" />
<title>PySpark data mocker</title>
<link rel="stylesheet" href="css/theme.css" />
<link rel="stylesheet" href="css/theme_extra.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css" />
<link href="css/extra.css" rel="stylesheet" />
<script>
// Current page data
var mkdocs_page_name = "Home";
var mkdocs_page_input_path = "index.md";
var mkdocs_page_url = null;
</script>
<!--[if lt IE 9]>
<script src="js/html5shiv.min.js"></script>
<![endif]-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/languages/python.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/languages/bash.min.js"></script>
<script>hljs.highlightAll();</script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="." class="icon icon-home"> PySpark data mocker
</a><div role="search">
<form id ="rtd-search-form" class="wy-form" action="./search.html" method="get">
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" title="Type search term here" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal current" href=".">Home</a>
<ul class="current">
<li class="toctree-l2"><a class="reference internal" href="#install">Install</a>
</li>
<li class="toctree-l2"><a class="reference internal" href="#basic-usage">Basic usage</a>
<ul>
<li class="toctree-l3"><a class="reference internal" href="#cleanup">Cleanup</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p class="caption"><span class="caption-text">Documentation</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="documentation/configuration/">Configuration</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="documentation/schema_infering/">Schema infering</a>
</li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="Mobile navigation menu">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href=".">PySpark data mocker</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content"><div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="." class="icon icon-home" aria-label="Docs"></a> »</li>
<li class="breadcrumb-item active">Home</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div class="section" itemprop="articleBody">
<h1 id="home">Home</h1>
<!--
# To improve the naming of the datalake and avoid refactor the project, move the basic datalake temporally
$ mv tests/data/basic_datalake/bar tests/data/basic_datalake/school
$ mv tests/data/basic_datalake/foo tests/data/basic_datalake/grades
-->
<p><code>pyspark-data-mocker</code> is a testing tool that facilitates the burden of setting up a desired datalake, so you can test
easily the behavior of your data application. It configures also the spark session to optimize it for testing
purpose.</p>
<h2 id="install">Install</h2>
<pre><code>pip install pyspark-data-mocker
</code></pre>
<h2 id="basic-usage">Basic usage</h2>
<p><code>pyspark-data-mocker</code> searches the directory you provide in order to seek and load files that can be interpreted as
tables, storing them inside the datalake. That datalake will contain certain databases depending on the folders
inside the root directory. For example, let's take a look into the <code>basic_datalake</code></p>
<pre><code class="language-bash">$ tree tests/data/basic_datalake -n --charset=ascii # byexample: +rm=~
tests/data/basic_datalake
|-- grades
| `-- exams.csv
`-- school
|-- courses.csv
`-- students.csv
~
2 directories, 3 files
</code></pre>
<p>This file hierarchy will be respected in the further datalake when loaded: each sub-folder will be considered as
spark database, and each file will be loaded as table, using the filename to name the table.</p>
<p>How can we load them using <code>pyspark-data-mocker</code>? Really simple!</p>
<pre><code class="language-python">>>> from pyspark_data_mocker import DataLakeBuilder
>>> builder = DataLakeBuilder.load_from_dir("./tests/data/basic_datalake") # byexample: +timeout=20 +pass
</code></pre>
<p>And that's it! you will now have in that execution context a datalake with the structure defined in the folder
<code>basic_datalake</code>. Let's take a closer look by running some queries.</p>
<pre><code class="language-python">>>> from pyspark.sql import SparkSession
>>> spark = SparkSession.builder.getOrCreate()
>>> spark.sql("SHOW DATABASES").show()
+---------+
|namespace|
+---------+
| default|
| grades|
| school|
+---------+
</code></pre>
<p>We have the <code>default</code> database (which came for free when instantiating spark), and the two folders inside
<code>tests/data/basic_datalake</code>: <code>school</code> and <code>grades</code>.</p>
<pre><code class="language-python">>>> spark.sql("SHOW TABLES IN school").show()
+---------+---------+-----------+
|namespace|tableName|isTemporary|
+---------+---------+-----------+
| school| courses| false|
| school| students| false|
+---------+---------+-----------+
>>> spark.sql("SELECT * FROM school.courses").show()
+---+------------+
| id| course_name|
+---+------------+
| 1|Algorithms 1|
| 2|Algorithms 2|
| 3| Calculus 1|
+---+------------+
>>> spark.table("school.students").show()
+---+----------+---------+--------------------+------+----------+
| id|first_name|last_name| email|gender|birth_date|
+---+----------+---------+--------------------+------+----------+
| 1| Shirleen| Dunford|sdunford0@amazona...|Female|1978-08-01|
| 2| Niko| Puckrin|npuckrin1@shinyst...| Male|2000-11-28|
| 3| Sergei| Barukh|sbarukh2@bizjourn...| Male|1992-01-20|
| 4| Sal| Maidens|smaidens3@senate.gov| Male|2003-12-14|
| 5| Cooper|MacGuffie| cmacguffie4@ibm.com| Male|2000-03-07|
+---+----------+---------+--------------------+------+----------+
</code></pre>
<p>Note how it is already filled with the data each CSV file has! The tool supports all kind of files: <code>csv</code>, <code>parquet</code>,
<code>json</code>. The application will infer which format to use by looking the file extension.</p>
<pre><code class="language-python">>>> spark.sql("SHOW TABLES IN grades").show()
+---------+---------+-----------+
|namespace|tableName|isTemporary|
+---------+---------+-----------+
| grades| exams| false|
+---------+---------+-----------+
>>> spark.table("grades.exams").show()
+---+----------+---------+----------+----+
| id|student_id|course_id| date|note|
+---+----------+---------+----------+----+
| 1| 1| 1|2022-05-01| 9|
| 2| 2| 1|2022-05-08| 7|
| 3| 3| 1|2022-06-17| 4|
| 4| 1| 3|2023-05-12| 9|
| 5| 2| 3|2023-05-12| 10|
| 6| 3| 3|2022-12-07| 7|
| 7| 4| 3|2022-12-07| 4|
| 8| 5| 3|2022-12-07| 2|
| 9| 1| 2|2023-05-01| 5|
| 10| 2| 2|2023-05-07| 8|
+---+----------+---------+----------+----+
</code></pre>
<h3 id="cleanup">Cleanup</h3>
<p>Once you finish with your test, you can easily clean the datalake by using the <code>cleanup</code> function and assure that
the next test will use a clean environment.</p>
<pre><code class="language-python">>>> builder.cleanup()
>>> spark.sql("SHOW DATABASES").show()
+---------+
|namespace|
+---------+
| default|
+---------+
</code></pre>
<!--
# Restore the previous state
$ mv tests/data/basic_datalake/school tests/data/basic_datalake/bar
$ mv tests/data/basic_datalake/grades tests/data/basic_datalake/foo
-->
</div>
</div><footer>
<div class="rst-footer-buttons" role="navigation" aria-label="Footer Navigation">
<a href="documentation/configuration/" class="btn btn-neutral float-right" title="Configuration">Next <span class="icon icon-circle-arrow-right"></span></a>
</div>
<hr/>
<div role="contentinfo">
<!-- Copyright etc -->
</div>
Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<div class="rst-versions" role="note" aria-label="Versions">
<span class="rst-current-version" data-toggle="rst-current-version">
<span><a href="documentation/configuration/" style="color: #fcfcfc">Next »</a></span>
</span>
</div>
<script src="js/jquery-3.6.0.min.js"></script>
<script>var base_url = ".";</script>
<script src="js/theme_extra.js"></script>
<script src="js/theme.js"></script>
<script src="search/main.js"></script>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>
<!--
MkDocs version : 1.5.2
Build Date UTC : 2024-01-09 23:56:23.653103+00:00
-->