session
Spark Session wrapper for gentropy¶
gentropy.common.session.Session
¶
This class provides a wrapper around SparkSession object with custom parameters.
The wrapper has a few default sets of configurations. See constructor for references.
Custom Spark Configuration
- Output configuration: write_mode and output_partitions, these set of parameters is stored respectively
under
spark.gentropy.writeModeandspark.gentropy.outputPartitions. Both parameters are used when writing datasets in gentropy steps. ThewriteModewill reflect on how Spark should handle existing data at the output path, whileoutputPartitionswill determine the number of partitions to use when writing out datasets (typically, excluding studyIndex datasets). For exact usage check the respective step implementation. - Hail configuration: If
start_hailis set to True, the Spark session will be configured with hail. By default the path to the Hail jar will be inferred from the installed Hail package location. Note that custom Hail configuration parameters can be passed through theextended_hail_confargument. - Dynamic allocation configuration: If
dynamic_allocationis set to True, the Spark session will includespark.dynamicAllocation.enabled,spark.dynamicAllocation.minExecutors,spark.dynamicAllocation.initialExecutorsandspark.shuffle.service.enabledconfigurations with 2 executors as minimum. - Enhanced BGZF codec configuration: If
use_enhanced_bgzip_codecis set to True, the Spark session will be configured to use theBGZFEnhancedGzipCodecfor reading block gzipped files.
Note
The custom configuration parameters for gentropy are prefixed with spark.gentropy. to avoid conflicts with other Spark applications.
Examples:
Create a new Spark Session on local machine with 4 executors, 4 cores and 8g of memory per executor
>>> from gentropy.common.session import Session
>>> session = Session(
... spark_uri="local[4]",
... extended_spark_conf={
... "spark.executor.instances": "4",
... "spark.executor.cores": "4",
... "spark.executor.memory": "8g",
... },
... )
Find existing session (if any exists)
>>> session = Session.find()
Create a new Spark Session with Hail support
>>> session = Session(start_hail=True)
Connect to running Spark cluster (yarn)
>>> session = Session(spark_uri="yarn")
Specify custom Hail configuration parameters
>>> session = Session(
... start_hail=True,
... extended_hail_conf={"min_block_size": "32MB"}
... )
Specify custom output parameters
>>> session = Session(
... output_partitions=100,
... write_mode=SparkWriteMode.OVERWRITE
... )
Specify via string (auto-converted to SparkWriteMode) if possible
>>> session = Session(
... output_partitions=100,
... write_mode="overwrite"
... )
Stop the session
>>> session.spark.stop()
View the path to spark ui
>>> session.spark.sparkContext.uiWebUrl
Example session with hadoop connector for S3 compatible storage
>>> session = Session(
... extended_spark_conf={
... # Executor
... 'spark.executor.memory': '32g',
... 'spark.executor.cores': '8',
... 'spark.excutor.memoryOverhead': '4g',
... 'spark.dynamicAllocation.enabled': 'true',
... 'spark.sql.files.maxPartitionBytes': '512m',
... # Driver
... 'spark.driver.memory': '25g',
... 'spark.executor.extraJavaOptions': '-XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:+ParallelRefProcEnabled -XX:+AlwaysPreTouch',
... 'spark.jars.packages': 'org.apache.hadoop:hadoop-aws:3.3.6,com.amazonaws:aws-java-sdk-bundle:1.12.367',
... 'spark.hadoop.fs.s3a.impl': 'org.apache.hadoop.fs.s3a.S3AFileSystem',
... 'spark.hadoop.fs.s3a.endpoint': f'https://{credentials.s3_host_url}:{credentials.s3_host_port}',
... 'spark.hadoop.fs.s3a.path.style.access': 'true',
... 'spark.hadoop.fs.s3a.connection.ssl.enabled': 'true',
... 'spark.hadoop.fs.s3a.access.key': f'{credentials.access_key_id}',
... 'spark.hadoop.fs.s3a.secret.key': f'{credentials.secret_access_key}',
... # Throughput tuning
... 'spark.hadoop.fs.s3a.connection.maximum': '1000',
... 'spark.hadoop.fs.s3a.threads.max': '1024',
... 'spark.hadoop.fs.s3a.attempts.maximum': '20',
... 'spark.hadoop.fs.s3a.connection.timeout': '600000', # 10min
... }
... )
Example session with hadoop connector for Google Cloud Storage
>>> session = Session(
... extended_spark_conf={
... 'spark.driver.maxResultSize': '0',
... 'spark.debug.maxToStringFields': '2000',
... 'spark.sql.broadcastTimeout': '3000',
... 'spark.sql.adaptive.enabled': 'true',
... 'spark.sql.adaptive.coalescePartitions.enabled': 'true',
... 'spark.serializer': 'org.apache.spark.serializer.KryoSerializer',
... # google cloud storage connector
... 'spark.jars.packages': 'com.google.cloud.bigdataoss:gcs-connector:hadoop3-2.2.21',
... 'spark.network.timeout': '10s',
... 'spark.network.timeoutInterval': '10s',
... 'spark.executor.heartbeatInterval': '6s',
... 'spark.hadoop.fs.gs.block.size': '134217728',
... 'spark.hadoop.fs.gs.inputstream.buffer.size': '8388608',
... 'spark.hadoop.fs.gs.outputstream.buffer.size': '8388608',
... 'spark.hadoop.fs.gs.outputstream.sync.min.interval.ms': '2000',
... 'spark.hadoop.fs.gs.status.parallel.enable': 'true',
... 'spark.hadoop.fs.gs.glob.algorithm': 'CONCURRENT',
... 'spark.hadoop.fs.gs.copy.with.rewrite.enable': 'true',
... 'spark.hadoop.fs.gs.metadata.cache.enable': 'false',
... 'spark.hadoop.fs.gs.auth.type': 'APPLICATION_DEFAULT',
... 'spark.hadoop.fs.gs.impl': 'com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystem',
... 'spark.hadoop.fs.AbstractFileSystem.gs.impl': 'com.google.cloud.hadoop.fs.gcs.GoogleHadoopFS',
... }
... )
Source code in src/gentropy/common/session.py
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 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 | |
output_partitions: int
property
¶
Get the number of output partitions.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of output partitions. |
use_enhanced_bgzip_codec: bool
property
¶
Check if the session is configured to use the BGZFEnhancedGzipCodec for reading block gzipped files.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the session is configured to use the BGZFEnhancedGzipCodec, False otherwise. |
write_mode: SparkWriteMode
property
¶
Get the Spark write mode.
Returns:
| Name | Type | Description |
|---|---|---|
SparkWriteMode |
SparkWriteMode
|
Spark write mode. |
__init__(spark_uri: str = 'local[*]', app_name: str = 'gentropy', write_mode: str = SparkWriteMode.ERROR_IF_EXISTS.value, hail_home: str | None = None, start_hail: bool = False, extended_spark_conf: dict[str, str] | None = None, extended_hail_conf: dict[str, Any] | None = None, output_partitions: int = 200, use_enhanced_bgzip_codec: bool = False, dynamic_allocation: bool = True, log_level: str | None = 'INFO') -> None
¶
Initialises spark session and logger.
The wrapper over SparkSession will either connect to an existing active Spark session or create a new one with the provided configuration.
If spark session already exists, the provided configuration will have no effect on the session. If any parameters will be different between existing session config and requested config, a warning will be logged to suggest rebuilding the session with the new configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spark_uri
|
str
|
Spark URI. Defaults to "local[*]". |
'local[*]'
|
app_name
|
str
|
Spark application name. Defaults to "gentropy". |
'gentropy'
|
write_mode
|
str
|
Spark write mode. Defaults to SparkWriteMode.ERROR_IF_EXISTS. |
value
|
hail_home
|
str | None
|
Path to Hail installation. Defaults to None. |
None
|
start_hail
|
bool
|
Whether to start Hail. Defaults to False. |
False
|
extended_spark_conf
|
dict[str, str] | None
|
Extended Spark configuration. Defaults to None. |
None
|
extended_hail_conf
|
dict[str, Any] | None
|
Extended Hail configuration. Defaults to None. |
None
|
output_partitions
|
int
|
Number of partitions for output datasets. Defaults to 200. |
200
|
use_enhanced_bgzip_codec
|
bool
|
Whether to use the BGZFEnhancedGzipCodec for reading block gzipped files. Defaults to False. |
False
|
dynamic_allocation
|
bool
|
Whether to enable Spark dynamic allocation. Defaults to True. |
True
|
log_level
|
str | None
|
Spark log level. Defaults to "INFO". |
'INFO'
|
Source code in src/gentropy/common/session.py
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 | |
find() -> Session
classmethod
¶
Finds the current active Spark session.
If no active Spark session is found, the method will raise an AttributeError.
Returns:
| Name | Type | Description |
|---|---|---|
Session |
Session
|
Current active Spark session. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If no active Spark session is found. |
Source code in src/gentropy/common/session.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |
load_data(path: str | list[str], fmt: str = 'parquet', schema: StructType | str | None = None, **kwargs: bool | float | int | str | None) -> DataFrame
¶
Generic function to read a file or folder into a Spark dataframe.
The recursiveFileLookup flag when set to True will skip all partition columns, but read files from all subdirectories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | list[str]
|
path to the dataset |
required |
fmt
|
str
|
file format. Defaults to parquet. |
'parquet'
|
schema
|
StructType | str | None
|
Schema to use when reading the data. |
None
|
**kwargs
|
bool | float | int | str | None
|
Additional arguments to pass to spark.read.load. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
DataFrame
|
Dataframe containing the loaded data. |
Default options for supported formats
By default:
- mergeSchema is set to True for parquet format.
- recursiveFileLookup is set to False.
- For tsv format sep and header options are set to tab and True respectively.
- For csv format header is set to True.
Loading data from URL
If the provided path is a URL (starting with http:// or https://), the method will attempt to load the data
and parallelize it for processing, this can be very slow it the file is large. Consider downloading the data
to a distributed file system and loading it from there instead. Only supported formats for loading from URL are csv and tsv.
Loading does not allow for recursive file lookup, nor supports multiple URLs.
Supported formats
Supported file formats are - parquet - csv - tsv - json (including jsonl/jsonlines)
Examples:
Load single tsv file from url, the header is expected at the 0-th row
>>> session.load_data('https://some_file.tsv', fmt='tsv')
Load single csv file from url, no header, expected schema
>>> session.load_data('https://some_file.csv', fmt='csv', header=False, schema="A int, B int")
Load the parquet dataset from google cloud storage, note that the Hadoop connector is required in Session
>>> session.load_data('gs://your_bucket/dataset')
Load multiple json files from s3 storage, note that the Hadoop connector is required in Session
>>> session.load_data(['s3a://some_bucket/file1.jsonl', 's3a://some_bucket/file2.jsonl'], fmt='json')
Source code in src/gentropy/common/session.py
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 | |
gentropy.common.session.SparkWriteMode
¶
Bases: StrEnum
Enum for Spark write modes.
Source code in src/gentropy/common/session.py
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 | |
ensure(v: str | None) -> str
classmethod
¶
Ensure the writeMode is correct.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
str | None
|
input value |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
mapping |
Raises:
| Type | Description |
|---|---|
ValueError
|
when the value is not found. |
Source code in src/gentropy/common/session.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |