|
35 | 35 | "outputs": [],
|
36 | 36 | "source": [
|
37 | 37 | "import pyspark\n",
|
38 |
| - "from pyspark import SparkContext as sc\n", |
39 |
| - "from pyspark.sql import Row" |
| 38 | + "from pyspark import SparkContext as sc" |
40 | 39 | ]
|
41 | 40 | },
|
42 | 41 | {
|
|
68 | 67 | "cell_type": "markdown",
|
69 | 68 | "metadata": {},
|
70 | 69 | "source": [
|
71 |
| - "### Read in a JSON file" |
| 70 | + "### Read in a JSON file and examine the data" |
72 | 71 | ]
|
73 | 72 | },
|
74 | 73 | {
|
|
142 | 141 | "cell_type": "markdown",
|
143 | 142 | "metadata": {},
|
144 | 143 | "source": [
|
145 |
| - "#### Use `printSchema()` to show he schema of the data. Note, how tightly it is integrated to the SQL-like framework. You can even see that the schema accepts `null` values because nullable property is set `True`." |
| 144 | + "### The data schema\n", |
| 145 | + "Use `printSchema()` to show he schema of the data. Note, how tightly it is integrated to the SQL-like framework. You can even see that the schema accepts `null` values because nullable property is set `True`." |
146 | 146 | ]
|
147 | 147 | },
|
148 | 148 | {
|
|
174 | 174 | },
|
175 | 175 | {
|
176 | 176 | "cell_type": "code",
|
177 |
| - "execution_count": 9, |
| 177 | + "execution_count": 8, |
178 | 178 | "metadata": {},
|
179 | 179 | "outputs": [
|
180 | 180 | {
|
|
183 | 183 | "['age', 'name']"
|
184 | 184 | ]
|
185 | 185 | },
|
186 |
| - "execution_count": 9, |
| 186 | + "execution_count": 8, |
187 | 187 | "metadata": {},
|
188 | 188 | "output_type": "execute_result"
|
189 | 189 | }
|
|
201 | 201 | },
|
202 | 202 | {
|
203 | 203 | "cell_type": "code",
|
204 |
| - "execution_count": 10, |
| 204 | + "execution_count": 9, |
205 | 205 | "metadata": {},
|
206 | 206 | "outputs": [
|
207 | 207 | {
|
|
210 | 210 | "DataFrame[summary: string, age: string, name: string]"
|
211 | 211 | ]
|
212 | 212 | },
|
213 |
| - "execution_count": 10, |
| 213 | + "execution_count": 9, |
214 | 214 | "metadata": {},
|
215 | 215 | "output_type": "execute_result"
|
216 | 216 | }
|
|
228 | 228 | },
|
229 | 229 | {
|
230 | 230 | "cell_type": "code",
|
231 |
| - "execution_count": 11, |
| 231 | + "execution_count": 10, |
232 | 232 | "metadata": {},
|
233 | 233 | "outputs": [
|
234 | 234 | {
|
|
252 | 252 | "df.describe().show()"
|
253 | 253 | ]
|
254 | 254 | },
|
| 255 | + { |
| 256 | + "cell_type": "markdown", |
| 257 | + "metadata": {}, |
| 258 | + "source": [ |
| 259 | + "#### There is `summary` method for more stats" |
| 260 | + ] |
| 261 | + }, |
| 262 | + { |
| 263 | + "cell_type": "code", |
| 264 | + "execution_count": 11, |
| 265 | + "metadata": {}, |
| 266 | + "outputs": [ |
| 267 | + { |
| 268 | + "name": "stdout", |
| 269 | + "output_type": "stream", |
| 270 | + "text": [ |
| 271 | + "+-------+------------------+-------+\n", |
| 272 | + "|summary| age| name|\n", |
| 273 | + "+-------+------------------+-------+\n", |
| 274 | + "| count| 2| 3|\n", |
| 275 | + "| mean| 24.5| null|\n", |
| 276 | + "| stddev|7.7781745930520225| null|\n", |
| 277 | + "| min| 19| Andy|\n", |
| 278 | + "| 25%| 19| null|\n", |
| 279 | + "| 50%| 19| null|\n", |
| 280 | + "| 75%| 30| null|\n", |
| 281 | + "| max| 30|Michael|\n", |
| 282 | + "+-------+------------------+-------+\n", |
| 283 | + "\n" |
| 284 | + ] |
| 285 | + } |
| 286 | + ], |
| 287 | + "source": [ |
| 288 | + "df.summary().show()" |
| 289 | + ] |
| 290 | + }, |
| 291 | + { |
| 292 | + "cell_type": "markdown", |
| 293 | + "metadata": {}, |
| 294 | + "source": [ |
| 295 | + "### How you can define your own Data Schema\n", |
| 296 | + "#### Import data types and structure types to build the data schema yourself" |
| 297 | + ] |
| 298 | + }, |
255 | 299 | {
|
256 | 300 | "cell_type": "code",
|
257 |
| - "execution_count": null, |
| 301 | + "execution_count": 12, |
258 | 302 | "metadata": {},
|
259 | 303 | "outputs": [],
|
260 |
| - "source": [] |
| 304 | + "source": [ |
| 305 | + "from pyspark.sql.types import StructField, IntegerType, StringType, StructType" |
| 306 | + ] |
| 307 | + }, |
| 308 | + { |
| 309 | + "cell_type": "markdown", |
| 310 | + "metadata": {}, |
| 311 | + "source": [ |
| 312 | + "#### Define your data schema by supplying name and data types to the structure fields you will be importing" |
| 313 | + ] |
| 314 | + }, |
| 315 | + { |
| 316 | + "cell_type": "code", |
| 317 | + "execution_count": 13, |
| 318 | + "metadata": {}, |
| 319 | + "outputs": [], |
| 320 | + "source": [ |
| 321 | + "data_schema = [StructField('age',IntegerType(),True),\n", |
| 322 | + "StructField('name',StringType(),True)]" |
| 323 | + ] |
| 324 | + }, |
| 325 | + { |
| 326 | + "cell_type": "markdown", |
| 327 | + "metadata": {}, |
| 328 | + "source": [ |
| 329 | + "#### Now create a `StrucType` with this schema as field" |
| 330 | + ] |
| 331 | + }, |
| 332 | + { |
| 333 | + "cell_type": "code", |
| 334 | + "execution_count": 14, |
| 335 | + "metadata": {}, |
| 336 | + "outputs": [], |
| 337 | + "source": [ |
| 338 | + "final_struc = StructType(fields=data_schema)" |
| 339 | + ] |
| 340 | + }, |
| 341 | + { |
| 342 | + "cell_type": "markdown", |
| 343 | + "metadata": {}, |
| 344 | + "source": [ |
| 345 | + "#### Now read in the same old JSON with this new schema" |
| 346 | + ] |
| 347 | + }, |
| 348 | + { |
| 349 | + "cell_type": "code", |
| 350 | + "execution_count": 15, |
| 351 | + "metadata": {}, |
| 352 | + "outputs": [ |
| 353 | + { |
| 354 | + "name": "stdout", |
| 355 | + "output_type": "stream", |
| 356 | + "text": [ |
| 357 | + "+----+-------+\n", |
| 358 | + "| age| name|\n", |
| 359 | + "+----+-------+\n", |
| 360 | + "|null|Michael|\n", |
| 361 | + "| 30| Andy|\n", |
| 362 | + "| 19| Justin|\n", |
| 363 | + "+----+-------+\n", |
| 364 | + "\n" |
| 365 | + ] |
| 366 | + } |
| 367 | + ], |
| 368 | + "source": [ |
| 369 | + "df = spark1.read.json('Data/people.json',schema=final_struc)\n", |
| 370 | + "df.show()" |
| 371 | + ] |
| 372 | + }, |
| 373 | + { |
| 374 | + "cell_type": "markdown", |
| 375 | + "metadata": {}, |
| 376 | + "source": [ |
| 377 | + "Now when you print the schema, you will see that the `age` is read as int and not long. By default Spark could not figure out for this column the exact data type that you wanted, so it went with long. But this is how you can build your own schema and instruct Spark to read the data accoridngly." |
| 378 | + ] |
261 | 379 | }
|
262 | 380 | ],
|
263 | 381 | "metadata": {
|
|
0 commit comments