Column reshapers
Module with column reshaping transformers.
ColumnReshapers
¶
Bases: object
Class containing column reshaping transformers.
Source code in mkdocs/lakehouse_engine/packages/transformers/column_reshapers.py
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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
|
cast(cols)
classmethod
¶
Cast specific columns into the designated type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cols |
Dict[str, str]
|
dict with columns and respective target types. Target types need to have the exact name of spark types: https://spark.apache.org/docs/latest/sql-ref-datatypes.html |
required |
Returns:
Type | Description |
---|---|
Callable
|
A function to be called in .transform() spark function. |
View Example of cast (See full example here)
Source code in mkdocs/lakehouse_engine/packages/transformers/column_reshapers.py
column_selector(cols)
classmethod
¶
Select specific columns with specific output aliases.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cols |
OrderedDict
|
dict with columns to select and respective aliases. |
required |
Returns:
Type | Description |
---|---|
Callable
|
A function to be called in .transform() spark function. |
Source code in mkdocs/lakehouse_engine/packages/transformers/column_reshapers.py
explode_columns(explode_arrays=False, array_cols_to_explode=None, explode_maps=False, map_cols_to_explode=None)
classmethod
¶
Explode columns with types like ArrayType and MapType.
After it can be applied the flatten_schema transformation, if we desired for example to explode the map (as we explode a StructType) or to explode a StructType inside the array. We recommend you to specify always the columns desired to explode and not explode all columns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
explode_arrays |
bool
|
whether you want to explode array columns (True) or not (False). Default: False. |
False
|
array_cols_to_explode |
List[str]
|
array columns which you want to explode. If you don't specify it will get all array columns and explode them. Default: None. |
None
|
explode_maps |
bool
|
whether you want to explode map columns (True) or not (False). Default: False. |
False
|
map_cols_to_explode |
List[str]
|
map columns which you want to explode. If you don't specify it will get all map columns and explode them. Default: None. |
None
|
Returns:
Type | Description |
---|---|
Callable
|
A function to be called in .transform() spark function. |
View Example of explode_columns (See full example here)
Source code in mkdocs/lakehouse_engine/packages/transformers/column_reshapers.py
flatten_schema(max_level=None, shorten_names=False, alias=True, num_chars=7, ignore_cols=None)
classmethod
¶
Flatten the schema of the dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_level |
int
|
level until which you want to flatten the schema. Default: None. |
None
|
shorten_names |
bool
|
whether to shorten the names of the prefixes of the fields being flattened or not. Default: False. |
False
|
alias |
bool
|
whether to define alias for the columns being flattened or not. Default: True. |
True
|
num_chars |
int
|
number of characters to consider when shortening the names of the fields. Default: 7. |
7
|
ignore_cols |
List
|
columns which you don't want to flatten. Default: None. |
None
|
Returns:
Type | Description |
---|---|
Callable
|
A function to be called in .transform() spark function. |
View Example of flatten_schema (See full example here)
Source code in mkdocs/lakehouse_engine/packages/transformers/column_reshapers.py
from_avro(schema=None, key_col='key', value_col='value', options=None, expand_key=False, expand_value=True)
classmethod
¶
Select all attributes from avro.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
str
|
the schema string. |
None
|
key_col |
str
|
the name of the key column. |
'key'
|
value_col |
str
|
the name of the value column. |
'value'
|
options |
dict
|
extra options (e.g., mode: "PERMISSIVE"). |
None
|
expand_key |
bool
|
whether you want to expand the content inside the key column or not. Default: false. |
False
|
expand_value |
bool
|
whether you want to expand the content inside the value column or not. Default: true. |
True
|
Returns:
Type | Description |
---|---|
Callable
|
Function to be called in .transform() spark function. |
Source code in mkdocs/lakehouse_engine/packages/transformers/column_reshapers.py
from_avro_with_registry(schema_registry, value_schema, value_col='value', key_schema=None, key_col='key', expand_key=False, expand_value=True)
classmethod
¶
Select all attributes from avro using a schema registry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema_registry |
str
|
the url to the schema registry. |
required |
value_schema |
str
|
the name of the value schema entry in the schema registry. |
required |
value_col |
str
|
the name of the value column. |
'value'
|
key_schema |
str
|
the name of the key schema entry in the schema registry. Default: None. |
None
|
key_col |
str
|
the name of the key column. |
'key'
|
expand_key |
bool
|
whether you want to expand the content inside the key column or not. Default: false. |
False
|
expand_value |
bool
|
whether you want to expand the content inside the value column or not. Default: true. |
True
|
Returns:
Type | Description |
---|---|
Callable
|
Function to be called in .transform() spark function. |
Source code in mkdocs/lakehouse_engine/packages/transformers/column_reshapers.py
from_json(input_col, schema_path=None, schema=None, json_options=None, drop_all_cols=False, disable_dbfs_retry=False)
classmethod
¶
Convert a json string into a json column (struct).
The new json column can be added to the existing columns (default) or it can replace all the others, being the only one to output. The new column gets the same name as the original one suffixed with '_json'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_col |
str
|
dict with columns and respective target names. |
required |
schema_path |
Optional[str]
|
path to the StructType schema (spark schema). |
None
|
schema |
Optional[dict]
|
dict with the StructType schema (spark schema). |
None
|
json_options |
Optional[dict]
|
options to parse the json value. |
None
|
drop_all_cols |
bool
|
whether to drop all the input columns or not. Defaults to False. |
False
|
disable_dbfs_retry |
bool
|
optional flag to disable file storage dbfs. |
False
|
Returns:
Type | Description |
---|---|
Callable
|
A function to be called in .transform() spark function. |
View Example of from_json (See full example here)
34{
35 "function": "from_json",
36 "args": {
37 "input_col": "sample",
38 "schema": {
39 "type": "struct",
40 "fields": [
41 {
42 "name": "field1",
43 "type": "string",
44 "nullable": true,
45 "metadata": {}
46 },
47 {
48 "name": "field2",
49 "type": "string",
50 "nullable": true,
51 "metadata": {}
52 },
53 {
54 "name": "field3",
55 "type": "double",
56 "nullable": true,
57 "metadata": {}
58 },
59 {
60 "name": "field4",
61 "type": {
62 "type": "struct",
63 "fields": [
64 {
65 "name": "field1",
66 "type": "string",
67 "nullable": true,
68 "metadata": {}
69 },
70 {
71 "name": "field2",
72 "type": "string",
73 "nullable": true,
74 "metadata": {}
75 }
76 ]
77 },
78 "nullable": true,
79 "metadata": {}
80 }
81 ]
82 }
83 }
84}
Source code in mkdocs/lakehouse_engine/packages/transformers/column_reshapers.py
rename(cols, escape_col_names=True)
classmethod
¶
Rename specific columns into the designated name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cols |
Dict[str, str]
|
dict with columns and respective target names. |
required |
escape_col_names |
bool
|
whether to escape column names (e.g. |
True
|
Returns:
Type | Description |
---|---|
Callable
|
Function to be called in .transform() spark function. |
View Example of rename (See full example here)
Source code in mkdocs/lakehouse_engine/packages/transformers/column_reshapers.py
to_json(in_cols, out_col, json_options=None)
classmethod
¶
Convert dataframe columns into a json value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_cols |
List[str]
|
name(s) of the input column(s). Example values: "*" - all columns; "my_col" - one column named "my_col"; "my_col1, my_col2" - two columns. |
required |
out_col |
str
|
name of the output column. |
required |
json_options |
Optional[dict]
|
options to parse the json value. |
None
|
Returns:
Type | Description |
---|---|
Callable
|
A function to be called in .transform() spark function. |
View Example of to_json (See full example here)
Source code in mkdocs/lakehouse_engine/packages/transformers/column_reshapers.py
with_expressions(cols_and_exprs)
classmethod
¶
Execute Spark SQL expressions to create the specified columns.
This function uses the Spark expr function. Check here.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cols_and_exprs |
Dict[str, str]
|
dict with columns and respective expressions to compute (Spark SQL expressions). |
required |
Returns:
Type | Description |
---|---|
Callable
|
A function to be called in .transform() spark function. |