Facades
Facades to use Blip's Data Platform.
This module provides Facades that abstract Blip's Data Platform functions and features.
DataPlatform
An interface for communication with Blip's Data Platform.
This interface contains the most popular functions used by Data Engineers in ETL pipelines. The methods in this class encpasulate methods of other classes present in this library and are intended to facilitate the library usage.
Source code in blipdataforge/facades.py
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 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 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 | |
__init__()
Initializes the class with current execution context.
check_delta_update(tbl_list, date=None, raise_error=True)
Verify if one or more tables are up-to-date.
This function checks if tables from tbl_list were updated later than
date, using the expression last_update_date > date. If False, an
exception is raised unless the raise_error is set to False.
If date is not informed, this functions uses the today's date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tbl_list
|
List[str]
|
A list of string with table names using the format
|
required |
date
|
Optional[str]
|
A date using ISO format that will be compared against last
change date of each table in |
None
|
raise_error
|
Optional[bool]
|
If |
True
|
Raises:
| Type | Description |
|---|---|
Exception
|
When |
Source code in blipdataforge/facades.py
create_email_title_opts(include_filename=None, start_date=None, end_date=None, labels=None)
Create a EmailTitleOpts object.
You can use this function to create/build a EmailTitleOpts
object, which is used by the share_files() facade. With this
object, you can control/change the behaviour of the title of the
email that is sent by share_files().
The arguments in this function specify which specific information you want to add to (or include in) the title of the email. You can include the following types of information in the title of the email: the name of the file that is being shared; a set of dates to specify the start and end of the period of data that is being shared; a set of small labels that can be used to filter the email more easily.
All arguments in this function are optional, meaning that, you can use all arguments at once, to include as much information as possible in the title of the email. But you can also use only one specific argument to include just one single extra information in the email title.
For example, if you want to include just the name of the file that
you are being shared through share_files(), then, you need to set
just the file_name argument in this function, and nothing more.
But if you want to include the range of dates in the title of the
email, to describe the period of observations that are being
exposed in the shared file, then, you need to set both the
start_date and end_date arguments in this function.
For example, consider the following snippet of code:
from blipdataforge import DataPlatform
dp = DataPlatform()
email_title = dp.create_email_title_opts(
include_filename=True,
start_date=date(2024,12,9),
end_date=date(2024,12,10)
)
dp.share_files(
receiver_email="pedro.duarte@blip.ai",
cc_email="pedro.duarte@blip.ai",
catalog="dataplatform_sandbox",
database="blipdataforge",
volume_name="sales_files",
files=csvs,
folder="ingest/sales",
email_title_opts=email_title
)
With this snippet, the title of the email that is sent by share_files(),
will be rendered in a format similar to this:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_filename
|
Optional[bool]
|
A boolean (True/False) to indicate if you want (or not) to include in the title the name of the file that is being shared in the email. If multiple files are being shared in the email, then, the name of the first file is used. |
None
|
start_date
|
Optional[date]
|
A |
None
|
end_date
|
Optional[date]
|
A |
None
|
labels
|
Optional[List[str]]
|
A set of labels, or, in other words, a set of small strings to include in the email title. Each label (i.e. each string) in this list should have 20 characters max. If you include a string that have more than 20 characters in this list, an exception is raised. You can use these set of labels as a mechanism to quickly and easily filter a set of emails through the email search service of GMail. |
None
|
Source code in blipdataforge/facades.py
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 | |
delete(catalog, database, table, predicate='', dry_run=False)
Deletes the rows that match a predicate.
The query executed to delete the records follow the format
DELETE FROM database.table [WHERE predicate], where database,
table and predicate are set by the arguments passed to this
function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
catalog
|
str
|
The catalog where the target table is. |
required |
database
|
str
|
The database/schema where the target table is. |
required |
table
|
str
|
The target table from which records will be deleted. |
required |
predicate
|
str
|
The condition used to find the rows to delete. When no predicate is provided, deletes all rows. |
''
|
dry_run
|
bool
|
If True, performs a trial run without making actual changes. Defaults to False. |
False
|
Source code in blipdataforge/facades.py
get_data_contract(catalog, database, table, api_auth_token='')
Retrieve a data contract from Blip's Data Contract API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
catalog
|
str
|
The desired catalog |
required |
database
|
str
|
The desired database |
required |
table
|
str
|
The desired table |
required |
api_auth_token
|
Optional[str]
|
The token to authenticate on the API. It is possible to
specify the API token through environment variable
|
''
|
Returns:
| Type | Description |
|---|---|
Union[DataContract, None]
|
A DataContract object if data contract exists or |
Source code in blipdataforge/facades.py
get_latest_record_date(catalog, database, table, date_column, date_format='', offset=0)
Returns the date of the latest record in a table.
Optionally, an offset can be applied to the result to get an earlier date.
Disclaimer: This functions is an alternate for the old initial_info
from TakeSpark library.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
catalog
|
str
|
The target catalog to be used. |
required |
database
|
str
|
The target database to be used. |
required |
table
|
str
|
The target table to be used. |
required |
date_column
|
str
|
The column which contains the date info of the latest record. |
required |
date_format
|
str
|
The format of the date, according to the Example: if the dates on the |
''
|
offset
|
int
|
The amount of days to subtract from the latest date. If the
column that contains the date is from |
0
|
Returns:
| Type | Description |
|---|---|
Union[str, date, datetime, Any]
|
The latest date in the informed column, with the offset applied |
Union[str, date, datetime, Any]
|
if it's the case. The type of the return depends on the type of |
Union[str, date, datetime, Any]
|
the provided date column on the source table. |
Source code in blipdataforge/facades.py
get_streaming_engine()
Get streaming engine.
This method returns a StreaminEngine object, used create or update
streaming structures like tables and views.
Source code in blipdataforge/facades.py
ingest_data(payload, data_routing_id, data_routing_token=None)
Ingest data from various sources using Data Routing layer.
If the data_routing_token parameter is not set, the environment
variable DATA_ROUTING_TOKEN will be used instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
dict
|
The payload that will be sent to Data Routing API. |
required |
data_routing_id
|
str
|
The ID of the Data Routing pipeline created previously. |
required |
data_routing_token
|
Optional[str]
|
The Data Routing API token created previously. |
None
|
Source code in blipdataforge/facades.py
ingest_google_sheet(sheet_key, catalog, database, table, index='')
Ingest google sheet through Gaia Data Routing.
NOTE: To ingest data from a Google Sheet, you must share this Google Sheet with the following email: data-platform@datarouting-api.iam.gserviceaccount.com. If you don't do this, the service will have no access to the Sheet. For more details, read the docs: https://dataforgedocs.blip.tools/user_guide/ingest_google_sheet/
This function creates an ingestion job by calling the Gaia Data Routing service. The ingestion job executes in another job cluster, and the link to the job run is logged in the terminal.
Note that the ingestion job does not block the execution of the current job (i.e. the job that called this function), so the notebook will continue to execute right after this function is called.
By default, this function reads all the data from the first sheet (or "page") of
the Google Sheet. If you want to ingest data from another sheet/page of the Google
Sheet, or, from a very specific range/region of the Google Sheet, you can provide
a A1 notation value to the index argument, to select the specific range/region
of the Google Sheet that you want to ingest/read.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sheet_key
|
str
|
The key of the target sheet to be ingested. |
required |
catalog
|
str
|
The target catalog. |
required |
database
|
str
|
The target database. |
required |
table
|
str
|
The target table. |
required |
index
|
str
|
A string (following the A1 notation) that specifies the range, or, the region of the Google Sheet to read and ingest. |
''
|
Source code in blipdataforge/facades.py
persist_files(database, volume_name, destination_folder, files, catalog='')
Persist local files into domain's storage accont.
You can use this function to persist/save files into the
domain's storage account. This function always persist the files
that you provide in the files argument inside the landingzone container
of the domain's storage account.
These files can then be accessed later through a path like this:
/Volumes/{catalog}/{database}/{volume_name}/{destination_folder}.
For more details, you should read more about Databricks Volumes at:
https://learn.microsoft.com/pt-br/azure/databricks/volumes/
When defining the volume in which the files will be saved, you can
either provide a volume that already exists, or, a volume that
still doesn't exist. If the volume provided does not exist yet in
the environment, the persist_files() function will automatically create this
volume for you.
But if you want to provide a volume that already exist in the current environment, this volume must be connected to the landingzone container of your domain's storage account. If this volume is connected to a different container, then, this function will raise an exception.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database
|
str
|
The volume's database. The database will be created if does not exits. |
required |
volume_name
|
str
|
The name of the volume to persist the files. The volume will be created if does not exits. |
required |
destination_folder
|
str
|
The folder inside the volume where the files must be saved. The folder and its subfolders are created they don't exist. |
required |
files
|
List[str]
|
A list containing the absolute paths for the files to be
saved into domains landingzone. An example would be
|
required |
catalog
|
Optional[str]
|
The target catalog. If not informed, the function uses the default domain catalog. |
''
|
Source code in blipdataforge/facades.py
run_quality_checks(checks, scan_definition_name, df=None, api_key_id='', api_key_secret='')
Run quality checks on a table or dataframe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checks
|
str
|
A string with Soda quality checks. If the dataset name in the check string is not in the format
Example: to run a check over a table, define the check string as follows: checks for catalog.database.table: - row_count > 0 |
required |
scan_definition_name
|
str
|
This parameter is used to aggregate results of checks from the same context. This parameter is also used as partition column of checks result table on Data Lake. |
required |
df
|
Optional[DataFrame]
|
A Spark dataframe to be checked. |
None
|
api_key_id
|
str
|
The Soda Cloud API key id to send the results to the cloud. |
''
|
api_key_secret
|
str
|
The Soda Cloud API key secret to send the results to the cloud. |
''
|
Returns:
| Type | Description |
|---|---|
dict
|
This function returns a dict with all results reported by Soda in |
dict
|
the quality checks. |
Source code in blipdataforge/facades.py
send_data(payload, data_routing_id, data_routing_token=None)
Send data to various destinations using Data Routing layer.
The Data Routing layer uses the Delta Sharing as the default output method for sharing data.
The payload parameter must use the format:
{
"since_datetime_value": start_date,
"until_datetime_value": end_date
}
where since_datetime_value and until_datetime_value must be in
the format "%Y-%m-%d", like "2023-12-27".
If the data_routing_token parameter is not set, the environment
variable DATA_ROUTING_TOKEN will be used instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
dict
|
The configuration to be used in the date filter to select the records that will be sent to the client. |
required |
data_routing_id
|
str
|
The ID of the Data Routing pipeline created previously. |
required |
data_routing_token
|
Optional[str]
|
The Data Routing API token created previously. |
None
|
Source code in blipdataforge/facades.py
share_files(receiver_email, cc_email, database, volume_name, folder, files, catalog='', email_title_opts=None)
Share data lake files.
Currently this function only supports sharing files through download links sent via email.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
receiver_email
|
str
|
The e-mail of who will receive the files |
required |
cc_email
|
str
|
The email of who will get a copy of the email |
required |
database
|
str
|
The files database |
required |
volume_name
|
str
|
The volume name where the files are in. |
required |
folder
|
str
|
The full path inside the volume where the files are in. This parameter must not include the volume name nor the file name. |
required |
files
|
Union[str, List[str]]
|
A file or a list of file names to be sent. Must include the file extension. |
required |
catalog
|
Optional[str]
|
The files catalog. If not informed, the current domain catalog will be used. |
''
|
email_title_opts
|
Optional[EmailTitleOpts]
|
Optional. You can provide an |
None
|
Source code in blipdataforge/facades.py
write(df, write_mode, catalog, database, table, path=None, schema_mode=None, partition_by=None, merge_fields=None, merge_condition=None, merge_schema=False, table_comment=None, columns_comments=None)
Writes a spark DataFrame on data lake.
If there is a data contract registred for the table being written, this methods automatically performs an automated data quality check based on data contract information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
A spark dataframe to be written into data lake. |
required |
write_mode
|
str
|
Specifies the write behavior. Accepts values If using If using |
required |
catalog
|
str
|
The target catalog to be used. |
required |
database
|
str
|
The target database (also called schema) to save the dataframe. It creates the database if not exists. |
required |
table
|
str
|
The target table that will receive the dataframe. It creates the table if not exists. |
required |
table_comment
|
Optional[str]
|
The table's commentary, also called table description. The commentary should contain information regarding the meaning of the data for the business. |
None
|
columns_comments
|
Optional[Dict[str, str]]
|
The commentaries of columns, also called columns descriptions. A column description gives a brief explanation on the meaning of that column regarding the table context. |
None
|
path
|
Optional[str]
|
DISCLAIMER: This parameter exists only for the DSLab migration and will be deprecated soon. The output path to write the table data. If not defined, the path will be defined using governance rules. |
None
|
schema_mode
|
Optional[str]
|
The schema mode used to write the table schema. Accepts modes
|
None
|
partition_by
|
Optional[List[str]]
|
List of column names to be used as partitions. |
None
|
merge_fields
|
Optional[List[str]]
|
List of column names to insert or update on upsert operation. The columns on source dataframe must have the same name of the columns on target table. If using |
None
|
merge_condition
|
Optional[Union[str, List[str]]]
|
The condition that upsert operation will evaluate to decide
whether a source record should be updated or inserted on
target table. This parameter is considered mandatory if
using This parameter accepts a Example using Example using |
None
|
merge_schema
|
Optional[bool]
|
|
False
|
Source code in blipdataforge/facades.py
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 | |
write_elasticsearch(host, port, index, df, write_mode='append', op_type='index', chunk_size=1000, id_column=None, api_key=None, user=None, password=None)
Write a dataframe to an Elasticsearch database.
Note: This function depends on the 'org.elasticsearch:elasticsearch-spark-30_2.12:8.17.3' maven library, so please add it to your ADF pipeline activity or install it in your cluster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
A string containing a hostname, which identifies where your
Elasticsearch database is hosted. Usually in the form
|
required |
port
|
int
|
A port number to use in the connection with the Elasticsearch. |
required |
index
|
str
|
The name of the existing index in the database to use. |
required |
df
|
DataFrame
|
The PySpark dataframe with the data to be written to the Elasticsearch. |
required |
write_mode
|
str
|
The spark write mode. This parameter controls how to perform the write operation. Can be any of:
|
'append'
|
op_type
|
str
|
The operation elasticsearch should perform. Can be any of:
|
'index'
|
chunk_size
|
int
|
Size (in entries) for batch writes. This setting is per task instance; it gets multiplied at runtime by the total number of running tasks/workers. |
1000
|
id_column
|
Optional[str]
|
The column in your pyspark DataFrame that contains the document ID. |
None
|
api_key
|
Optional[str]
|
The API key to use in the connection with Elasticsearch. If both API key and user/pass are provided, the API key will be used. |
None
|
user
|
Optional[str]
|
The username to use in the connection with the Elasticsearch. |
None
|
password
|
Optional[str]
|
The password to use in conjunction with the username provided
at |
None
|
Source code in blipdataforge/facades.py
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 | |
write_eventhub(df, namespace, topic, conn_str, port=9093, batch_size=524288, request_timeout=300000, linger_ms=2, buffer_memory=33554432, max_block_ms=60000)
Write Spark DataFrame to EventHub topic.
You can use this method to write the data from a Spark DataFrame into an existing EventHub topic. For now, this method uses the normal/batch backend of spark (spark.write).
EventHubs events need to be in a specific format, so this method adds a column "value" to the dataframe, containing the dataframe data as a JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
A Spark DataFrame with the data that you want to write. |
required |
namespace
|
str
|
The EventHub namespace to be used. |
required |
topic
|
str
|
The name topic of the EventHub topic to write to. |
required |
conn_str
|
str
|
The connection string to be used to connect and send
data to eventhub. The connection string must be in the format:
|
required |
port
|
int
|
The port that will be used to connect with EventHub server. If not informed, use default port 9093. |
9093
|
batch_size
|
int
|
This configuration controls the default batch size in bytes,
and will be passed directly to the |
524288
|
request_timeout
|
int
|
Controls the maximum amount of time the EventHub producer will wait for the response of a request. |
300000
|
linger_ms
|
int
|
Controls how long the producer will wait for new records before sending the batch to the broker. This configuration can improve the transmission by reducing the amount of "uncompleted" batch requests sent to broker. |
2
|
buffer_memory
|
int
|
The total bytes of memory the producer can use to buffer records waiting to be sent to the server. If records are sent faster than they can be delivered to the server the producer will block for max.block.ms after which it will throw an exception. |
33554432
|
max_block_ms
|
int
|
It is used by producers and defines the maximum time (in milliseconds) that a synchronous send() call can be blocked while Kafka attempts to obtain metadata from the cluster or space in the internal buffer for the message. |
60000
|
Source code in blipdataforge/facades.py
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 | |
write_mongodb(conn_str, df, database, collection, write_mode, operation_type='replace', upsert_document=False, convert_json=False, ignore_null=False, id_field_list=['_id'], max_batch_size=512, comment='')
Write a dataframe to a MongoDB collection.
Note: This function works only in Single User clusters, otherwise an AnalysisException will raise. Also, this function depends on the 'org.mongodb.spark:mongo-spark-connector_2.12:10.4.1' maven library, so please add it to your ADF pipeline activity or install it in your Single User cluster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn_str
|
str
|
The connection string used to establish connection with MongoDB
server. Must be in the format
|
required |
df
|
DataFrame
|
The PySpark dataframe with the data to be written to the collection. |
required |
database
|
str
|
The database where the target collection resides. |
required |
collection
|
str
|
The target collection to write data to. |
required |
write_mode
|
str
|
Write modes supported: |
required |
operation_type
|
str
|
Specifies how the write operation must save the documents.
Accepts
|
'replace'
|
upsert_document
|
bool
|
When true, replace and update operations will insert the data
if no match exists. For time series collections, you must set
|
False
|
convert_json
|
Union[bool, str]
|
Specifies whether the connector parses the string and converts
extended JSON into BSON. The default value is False, which
means that the connector leaves all values as strings.
This function accepts
|
False
|
ignore_null
|
bool
|
When |
False
|
id_field_list
|
List[str]
|
List of fields by which to split the collection data.
Defaults to |
['_id']
|
max_batch_size
|
int
|
Specifies the maximum number of operations to batch in bulk
operations. Defaults to |
512
|
comment
|
str
|
The comment to append to the write operations. Comments appear in the output of the Database profiler. |
''
|
Source code in blipdataforge/facades.py
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 | |
write_to_opensearch(df, host, port, user, password, index, chunk_size, write_mode='append', op_type='upsert', id_column=None, use_spark_native_writer=True)
Write Spark DataFrame to Opensearch database.
You can use this function to write a Spark DataFrame to an Opensearch database. Opensearch is a NoSQL database (i.e. it's a database of JSON Documents). This function will basically: 1) collect all rows from your Spark DataFrame; 2) transform them into "dicts", which are the "JSON documents" equivalents in Python; 3) insert the necessary Opensearch operation directives into this list; 4) then, it will start sending this data to the Opensearch server in batches/chunks.
The user and password arguments are related to the authentication process in the
server, while the host and port arguments define where the Opensearch server is,
and where the connection is established.
The chunk_size argument defines the maximum number of rows from your Spark DataFrame
that are included in each chunk that is sent to the Opensearch server. For example,
if your DataFrame contains 10000 rows, and you set chunk_size to 1000, then, this
function will split your DataFrame into 10 chunks, and send each chunk sequentially
to the Opensearch server. By controlling the size of the chunk that is sent to the
server, you can also control how much data (or, how much "stuff") the server needs to
process in each request. This is useful if your Opensearch server have a rate limit, or,
a if it does not have much powerful resources to process huge amounts of data in a
single request.
This function have two different backends: one is the Opensearch Spark native driver, and the other one is the Opensearch Python SDK. By default, this function uses the Opensearch Spark native driver to write the data. Because this native driver is much more scalable and appropriate for our big data environment. However, in order to use this native driver backend you must have a specific Maven package installed in your environment/cluster.
https://central.sonatype.com/artifact/org.opensearch.client/opensearch-spark-30_2.12
The function will check for the presence of the native driver in your environment. If it
does not find this native driver, then, you likely does not have this Maven package installed
in your environment, and therefore, an AssertionError is raised by the function, warning
you about this problem.
On the other side, we have the Python SDK backend, which does not scale very well. But it handles moderate DataFrames (< 400k rows) decently well, and also, offers a much better visibility of "what is happenning" behind the scene. Because the Python SDK have much better and useful error messages that you can use to debug your application, and also, it returns a list containing the raw HTTP responses of the server, which gives you a lot of information about the write operations performed in the server. Therefore, you can easily see if any of your records was not succesfully recorded in the server, for whatever reason.
Unfortunately, you do not have such visibility in the Spark native driver backend. In fact, the Spark native driver, although it scales very well, it does not provide very useful/informative error messages for the users. Therefore, if there is something wrong in your code (e.g. you may forgot to change the hostname of your server in the code, or, you are using the wrong credentials, or, your data might have a different schema than the documents that are already present in index your index, etc.) you will probably have some hard time while debugging if you are using the Spark native driver backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
A Spark DataFrame with the data that you want to write. |
required |
host
|
str
|
A string containing a hostname, which identifies where your Opensearch database is hosted. |
required |
port
|
int
|
A port number to use in the connection with the database. |
required |
user
|
str
|
The username to use in the connection with the database. |
required |
password
|
str
|
The password to use in conjunction with the username provided at |
required |
index
|
str
|
The name of the existing index in the database to use. |
required |
chunk_size
|
int
|
Sets the size of each chunk of data that is sent to the Opensearch server. This chunk size should be the number of entries/rows to be included in each chunk. |
required |
write_mode
|
str
|
The Spark write mode to use when writing the data (e.g. append, overwrite, etc.). |
'append'
|
op_type
|
str
|
Default to "upsert". The name of the Opensearch operation to use while writing the data to the database. Supported values are: "upsert" (for an upsert operation) "index" (for an index operation). |
'upsert'
|
id_column
|
Optional[str]
|
Optional. The name of the column that contains the "document id" to use in the database. |
None
|
use_spark_native_writer
|
bool
|
Default to True. Whether to use or not the Spark native writer class to write the data. If False, the Python SDK for Opensearch is used instead to write the data. However, the Python SDK writer is much slower and it is not capable of handling huge amounts of data, so be careful with it. |
True
|
Returns:
| Type | Description |
|---|---|
List[dict]
|
A list of responses. In more details, each element in the response returned by the |
List[dict]
|
Opensearch server for each chunk/batch of data from your Spark DataFrame that was |
List[dict]
|
sent to the server. You can use this list of responses to investigate if some |
List[dict]
|
error occurred during the ingest process. |
Source code in blipdataforge/facades.py
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 | |