Skip to content

databricks_utils

DatabricksUtils

Description

| This Class provides methods to automate different scenarios related to spark, delta lake executed through Databricks Rest API

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
  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
 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
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
class DatabricksUtils:
    """
    Description:
        |  This Class provides methods to automate different scenarios related to spark, delta lake executed through
        Databricks Rest API
    """

    def __init__(self, str_databricks_url, str_access_token=None):
        self.__obj_db_exception = CoreExceptions()
        self.__databricks_api_version = "2.0"
        self.__databricks_api_path = str_databricks_url + "/api/" + self.__databricks_api_version
        self.databricks_access_token = str_access_token
        self.hive_connection = None
        self.context_id = None
        self.logger = CoreLogger(name=__name__).get_logger()
        self.security = Security()

    def list_dbfs_files(self, str_dbfs_path):
        """
        Description:
                |  This method is used to get the list of files at given dbfs path.

        :param str_dbfs_path: DBFS path in databricks
        :type str_dbfs_path: String

        :return: boolean, list - Tuple without parentheses indicating 'execution_result' and list object which contains
        the list of files at given dbfs path
        Examples:
                |  list_dbfs_files("/FileStore/mdata-1/")

        """
        try:
            str_api_method = "GET"
            dict_api_header = {"Content-Type": "application/json"}
            dict_api_header["Authorization"] = "Bearer " + self.databricks_access_token
            str_resource = "/dbfs/list"
            str_uri = self.__databricks_api_path + str_resource
            str_payload = {"path": str_dbfs_path}
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
            )
            if obj_response.status_code == 200:
                return True, obj_response.json()
            return False, None
        except Exception as e:  # pylint: disable=broad-exception-caught
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def upload_files_in_dbfs(self, str_src_path, str_dbfs_path):
        """
        Description:
                |  This method is used to upload files from the source path to the given dbfs path.

        :param str_src_path: Source Path for the File to upload
        :type str_src_path: String
        :param str_dbfs_path: DBFS path
        :type str_dbfs_path: String

        :return: boolean - Indicating the result whether file is uploaded successully or not.
        Examples:
                |  upload_files_in_dbfs( "\queries\data_ingestion_pipeline\DatabricksApps-0.0.7-SNAPSHOT.jar", "/FileStore/mdata-1/")
                |  upload_files_in_dbfs( "\testdata\data_ingestion_pipeline\student.csv", "/FileStore/mdata-1/")

        .. note::
                |  Please don't include the name of file in 'str_dbfs_path' parameter. Refer Examples

        """
        try:
            str_api_method = "POST"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/dbfs/put"
            str_uri = self.__databricks_api_path + str_resource
            file_name = os.path.basename(str_src_path)
            if file_name != "":
                with open(str_src_path, "rb") as fp:
                    file_data = fp.read()
                str_payload = {"path": str_dbfs_path + file_name, "overwrite": "true"}
                str_files = {"filefield": (file_name, file_data)}
                obj_response = self.call_request(
                    str_api_method,
                    str_uri,
                    dict_api_header,
                    str_payload=str_payload,
                    str_files=str_files,
                )
                if obj_response.status_code == 200:
                    return True
                self.logger.info("Not a valid response code.")
                return False
            else:
                self.logger.info("FileName not present in source path")
                return False
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def create_notebook(self, str_file_path, str_workspace_path, str_language="PYTHON"):
        """
        Description:
                |  This method is used to create a notebook in databricks workspace.

        :param str_file_path: Source file path for databricks notebook
        :type str_file_path: String
        :param str_workspace_path: Target Workspace path where notebook will be created
        :type str_workspace_path: String
        :param str_language: Coding Language used (Optional parameter with default value 'PYTHON')
        :type str_language: String

        :return: boolean - Indicating the result whether notebook is successully created or not.
        Examples:
                |  create_notebook("\queries\data_ingestion_pipeline\sample.py", "/Shared/mdata/sample")
                |  create_notebook("\queries\data_ingestion_pipeline\sample.py", "/Shared/mdata/sample", str_language="PYTHON")

        """
        try:
            str_api_method = "POST"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/workspace/import"
            str_uri = self.__databricks_api_path + str_resource
            with open(str_file_path, "rb") as fp:
                file_data = fp.read()
                encoded_data = base64.b64encode(file_data)
            decoded_data = encoded_data.decode("utf-8")
            str_payload = {
                "path": str_workspace_path,
                "format": "SOURCE",
                "language": str_language,
                "content": decoded_data,
                "overwrite": "false",
            }
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
            )
            if obj_response.status_code == 200:
                return True
            return False
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def check_notebook(self, str_workspace_path):
        """
        Description:
                |  This method is to check if the notebook exists at the given workspace path

        :param str_workspace_path: Databricks Workspace Path
        :type str_workspace_path: String

        :return: boolean - Indicating the result whether notebook actually exists at the given databricks workspace path
        Examples:
                |  check_notebook("/Shared/mdata/sample")

        """
        try:
            str_api_method = "GET"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/workspace/get-status"
            str_uri = self.__databricks_api_path + str_resource
            str_payload = {"path": str_workspace_path}
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
            )
            if obj_response.status_code == 200:
                if obj_response.json()["object_type"] == "NOTEBOOK":
                    return True
            return False
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def delete_notebook(self, str_workspace_path, bln_recursive=False):
        """
        Description:
                |  This method is to delete the notebook exists at the given workspace path

        :param str_workspace_path: Databricks Workspace Path
        :type str_workspace_path: String
        :param bln_recursive: An optional boolean parameter to delete recursively all the files at given workspace path
         (default Value: False)
        :type bln_recursive: Boolean

        :return: boolean - Indicating the result whether notebook actually deleted at the given databricks workspace
        path or not.
        Examples:
                |  delete_notebook("/Shared/mdata/sample")
                |  delete_notebook("/Shared/mdata/", True)

        """
        try:
            str_api_method = "POST"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/workspace/delete"
            str_uri = self.__databricks_api_path + str_resource
            str_payload = {"path": str_workspace_path, "recursive": bln_recursive}
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
            )
            if obj_response.status_code == 200:
                return True
            return False
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def list_clusters(self):
        """
        Description:
                |  This method is to get the list of clusters in the Databricks

        :return: boolean, list - Tuple without parentheses indicating 'execution_result' and list object which contains
         the list of clusters in databricks
        Examples:
                |  list_clusters()

        """
        try:
            str_api_method = "GET"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/clusters/list"
            str_uri = self.__databricks_api_path + str_resource
            obj_response = self.call_request(str_api_method, str_uri, dict_api_header)
            if obj_response.status_code == 200:
                return True, obj_response.json()
            return False, None
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def get_cluster_id(self, str_cluster_name):
        """
        Description:
                |  This method is used to get the cluster id with the name

        :param str_cluster_name: Databricks Cluster Name
        :type str_cluster_name: String

        :return: String - Cluster Id
        Examples:
                |  get_cluster_id("databricks-dev-training-spark2")

        """
        try:
            str_cluster_id = None
            execution, response_cluster_list = self.list_clusters()
            if execution:
                data = next(
                    (
                        cluster
                        for cluster in response_cluster_list["clusters"]
                        if cluster["cluster_name"].lower() == str_cluster_name.lower()
                    ),
                    None,
                )
                if data is not None:
                    str_cluster_id = data["cluster_id"]
            return str_cluster_id
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def get_cluster(self, str_cluster_id):
        """
        Description:
                |  This method is used to get the cluster

        :param str_cluster_id: Databricks Cluster Id
        :type str_cluster_id: String

        :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object
        which contains the details of cluster
        Examples:
                |  get_cluster("0715-113640-giddy195")

        """
        try:
            str_api_method = "GET"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/clusters/get"
            str_uri = self.__databricks_api_path + str_resource
            str_payload = {"cluster_id": str_cluster_id}
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
            )
            if obj_response.status_code == 200:
                return True, obj_response.json()
            return False, None
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def start_cluster(self, str_cluster_id):
        """
        Description:
                |  This method is used to start the cluster

        :param str_cluster_id: Databricks Cluster Id
        :type str_cluster_id: String

        :return: boolean - True/False indicating 'execution_result' from the api response
        Examples:
                |  start_cluster("0715-113640-giddy195")

        """
        try:
            str_api_method = "POST"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/clusters/start"
            str_uri = self.__databricks_api_path + str_resource
            str_payload = {"cluster_id": str_cluster_id}
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
            )
            if obj_response.status_code == 200:
                return True
            return False
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def restart_cluster(self, str_cluster_id):
        """
        Description:
                |  This method is used to restart cluster in databricks

        :param str_cluster_id: Databricks Cluster Id
        :type str_cluster_id: String

        :return: boolean - True/False indicating 'execution_result' from the api response
        Examples:
                |  restart_cluster("0715-113640-giddy195")

        """
        try:
            str_api_method = "POST"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/clusters/restart"
            str_uri = self.__databricks_api_path + str_resource
            str_payload = {"cluster_id": str_cluster_id}
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
            )
            if obj_response.status_code == 200:
                return True
            return False
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def stop_cluster(self, str_cluster_id):
        """
        Description:
                |  This method is used to stop cluster in databricks

        :param str_cluster_id: Databricks Cluster Id
        :type str_cluster_id: String

        :return: boolean - True/False indicating 'execution_result' from the api response
        Examples:
                |  stop_cluster("0715-113640-giddy195")

        """
        try:
            str_api_method = "POST"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/clusters/delete"
            str_uri = self.__databricks_api_path + str_resource
            str_payload = {"cluster_id": str_cluster_id}
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
            )
            if obj_response.status_code == 200:
                return True
            return False
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def delete_cluster(self, str_cluster_id):
        """
        Description:
                |  This method is used to permanently delete cluster in databricks

        :param str_cluster_id: Databricks Cluster Id
        :type str_cluster_id: String

        :return: boolean - True/False indicating 'execution_result' from the api response
        Examples:
                |  delete_cluster("0715-113640-giddy195")

        """
        try:
            str_api_method = "POST"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/clusters/permanent-delete"
            str_uri = self.__databricks_api_path + str_resource
            str_payload = {"cluster_id": str_cluster_id}
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
            )
            if obj_response.status_code == 200:
                return True, obj_response.json()
            return False, None
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def create_cluster(self, pdict_payload):
        """
        Description:
                |  This method is used to create cluster in databricks

        :param pdict_payload: dictionary payload to create a cluster
        :type pdict_payload: dictionary

        :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object
        .. note::
                |  For creating a cluster, the user needs to have create permissions on databricks.

        """
        try:
            str_api_method = "POST"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/clusters/create"
            str_uri = self.__databricks_api_path + str_resource
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(pdict_payload)
            )
            if obj_response.status_code == 200:
                return True, obj_response.json()
            return False, None
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def delete_dbfs_path(self, str_dbfs_path, bln_recursive=False):
        """
        Description:
                |  This method is used to delete file at dbfs path

        :param str_dbfs_path: dbfs path
        :type str_dbfs_path: String
        :param bln_recursive: Boolean parameter for deleting the files recursively at given dbfs path
        (default value: False)
        :type bln_recursive: Boolean

        :return: boolean - True/False indicating 'execution_result' from the api response
        Examples:
                |  delete_dbfs_path("/FileStore/mdata-1/users-11.csv")
                |  delete_dbfs_path("/FileStore/mdata-1", True)

        """
        try:
            str_api_method = "POST"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/dbfs/delete"
            str_uri = self.__databricks_api_path + str_resource
            str_payload = {"path": str_dbfs_path, "recursive": bln_recursive}
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
            )
            if obj_response.status_code == 200:
                return True
            return False
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def create_job(self, pdict_payload):
        """
        Description:
                |  This method is used to create a spark job in databricks

        :param pdict_payload: dictionary payload to create a data processing job
        :type pdict_payload: dictionary

        :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object
        """
        try:
            str_api_method = "POST"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/jobs/create"
            str_uri = self.__databricks_api_path + str_resource
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(pdict_payload)
            )
            if obj_response.status_code == 200:
                return True, obj_response.json()
            return False, None
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def delete_job(self, pint_job_id):
        """
        Description:
                |  This method is used to delete a spark job in databricks

        :param pint_job_id: spark job id
        :type pint_job_id: integer

        :return: boolean - True/False indicating 'execution_result' from the api response
        Examples:
                |  delete_job(34343)

        """
        try:
            str_api_method = "POST"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/jobs/delete"
            str_uri = self.__databricks_api_path + str_resource
            dict_payload = {"job_id": pint_job_id}
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(dict_payload)
            )
            if obj_response.status_code == 200:
                return True
            return False
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def list_jobs(self):
        """
        Description:
                |  This method is used to all the spark jobs in databricks

        :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object which
         contains the details of all the jobs
        Examples:
                |  list_jobs()

        """
        try:
            str_api_method = "GET"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/jobs/list"
            str_uri = self.__databricks_api_path + str_resource
            obj_response = self.call_request(str_api_method, str_uri, dict_api_header)
            if obj_response.status_code == 200:
                return True, obj_response.json()
            return False, None
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def get_job_id(self, str_job_name):
        """
        Description:
                |  This method is used to get the spark job id with it's name.

        :param str_job_name: Spark Job name
        :type str_job_name: String

        :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and spark job id.
        Examples:
                |  get_job_id("Student_analysis_job")

        """
        try:
            job_id = None
            execution, response_jobs_list = self.list_jobs()
            if execution:
                data = next(
                    (
                        job
                        for job in response_jobs_list["jobs"]
                        if job["settings"]["name"].lower() == str_job_name.lower()
                    ),
                    None,
                )
                if data is not None:
                    job_id = data["job_id"]
                    return True, job_id
            return False, job_id
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def get_job(self, pint_job_id):
        """
        Description:
                |  This method is used to get the job details with the id

        :param pint_job_id: Spark Job id
        :type pint_job_id: integer

        :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and spark job dictionary object.
        Examples:
                |  get_job(476474)

        """
        try:
            str_api_method = "GET"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/jobs/get"
            str_uri = self.__databricks_api_path + str_resource
            dict_payload = {"job_id": pint_job_id}
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(dict_payload)
            )
            if obj_response.status_code == 200:
                return True, obj_response.json()
            return False, None
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def execute_job(self, pint_job_id, pdict_params=None):
        """
        Description:
                |  This method is used to execute the spark job

        :param pint_job_id: Spark Job id
        :type pint_job_id: integer
        :param pdict_params: dictionary object which contains all the optional parameters to execute spark job with
        :type pdict_params: dictionary

        :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object.
        Examples:
                |  parameter_dict = {
            |    "notebook_params": {
            |        "fileLocation": "/FileStore/mdata-1/users-11.csv"
            |        }
            |    }
                |  execute_job(65656)
                |  execute_job(65656, pdict_params=parameter_dict )

        """
        try:
            str_api_method = "POST"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/jobs/run-now"
            str_uri = self.__databricks_api_path + str_resource
            dict_payload = {"job_id": pint_job_id}
            if pdict_params is not None:
                for key, value in pdict_params.items():
                    dict_payload[key] = value
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(dict_payload)
            )
            if obj_response.status_code == 200:
                return True, obj_response.json()
            return False, None
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def get_job_output(self, pint_run_id):
        """
        Description:
                |  This method is used to get the job output

        :param pint_job_id: Spark Job id
        :type pint_job_id: integer

        :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object.
        Examples:
                |  get_job_output(65655)

        """
        try:
            str_api_method = "GET"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/jobs/runs/get-output"
            str_uri = self.__databricks_api_path + str_resource
            dict_payload = {"run_id": pint_run_id}
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(dict_payload)
            )
            if obj_response.status_code == 200:
                return True, obj_response.json()
            return False, None
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def read_dbfs_file(self, str_dbfs_path, pint_offset=0, pint_length=1000):
        """
        Description:
                |  This method is used to read file from the dbfs path

        :param str_dbfs_path: dbfs path
        :type str_dbfs_path: String
        :param pint_offset: offset: the point from where to start reading the content
        :type pint_offset: integer
        :param pint_length: length of the content to read in terms of bytes
        :type pint_length: integer

        :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object.
        Examples:
                |  read_dbfs_file("/FileStore/mdata-1/users-11.csv", pint_offset=0, pint_length=10000)

        """
        try:
            str_api_method = "GET"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/dbfs/read"
            str_uri = self.__databricks_api_path + str_resource
            str_payload = {"path": str_dbfs_path, "offset": pint_offset, "length": pint_length}
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
            )
            if obj_response.status_code == 200:
                return True, obj_response.json()
            return False, None
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def __create_context(self, str_language, str_cluster_id, str_version=None):
        """
        Description:
                |  This private method is used to create context id

        :param str_language: coding language to be used for e.g. java/python/shell scripting
        :type str_language: String
        :param str_cluster_id: Cluster id
        :type str_cluster_id: String
        :param str_version: api version
        :type str_version: String

        :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object.
        Examples:
                |  __create_context("python", "0107-192239-lobed1", str_version = "1.2"):

        .. note::
                |  As of now till date 06/01/2021, this method is executable with api version 1.2 only

        """
        try:
            str_api_path = self.__databricks_api_path
            if str_version is not None:
                str_api_path = str_api_path[0 : str_api_path.rfind("/") + 1] + str(str_version)
            str_api_method = "POST"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/contexts/create"
            str_uri = str_api_path + str_resource
            str_payload = {"language": str_language, "clusterId": str_cluster_id}
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
            )
            if obj_response.status_code == 200:
                return True, obj_response.json()
            return False, None
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def destroy_context(self, str_cluster_id, str_version=None):
        """
        Description:
                |  This method is used to destroy context id

        :param str_cluster_id: Cluster id
        :type str_cluster_id: String
        :param str_version: api version
        :type str_version: String

        :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object.
        Examples:
                |  destroy_context("0107-192239-lobed1", str_version = "1.2"):

        .. note::
                |  As of now till date 06/01/2021, this method is executable with api version 1.2 only

        """
        try:
            if self.context_id is not None:
                str_api_path = self.__databricks_api_path
                if str_version is not None:
                    str_api_path = str_api_path[0 : str_api_path.rfind("/") + 1] + str(str_version)
                str_api_method = "POST"
                dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
                str_resource = "/contexts/destroy"
                str_uri = str_api_path + str_resource
                str_payload = {"contextId": self.context_id, "clusterId": str_cluster_id}
                obj_response = self.call_request(
                    str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
                )
                if obj_response.status_code == 200:
                    return True, obj_response.json()
            return False, None
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def execute_command(self, str_language, str_cluster_id, str_command, str_version=None):
        """
        Description:
                |  This method is used to execute command

        :param str_language: coding language used
        :type str_language: String
        :param str_cluster_id: Cluster id
        :type str_cluster_id: String
        :param str_command: Command to be execute in databricks notebook
        :type str_command: String
        :param str_version: api version
        :type str_version: String

        :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object.
        Examples:
                |  execute_command("sql", "0107-192239-lobed1", "Select * from users_11_csv limit 1", str_version = "1.2")
                |  execute_command("python", "0107-192239-lobed1", "dbutils.fs.ls('/FileStore/mdata-1')", str_version = "1.2"):

        .. note::
                |  As of now till date 06/01/2021, this method is executable with api version 1.2 only

        """
        try:
            if self.context_id is None:
                execution, response = self.__create_context(
                    str_language, str_cluster_id, str_version=str_version
                )
                if execution:
                    self.context_id = response["id"]
            str_api_path = self.__databricks_api_path
            if str_version is not None:
                str_api_path = str_api_path[0 : str_api_path.rfind("/") + 1] + str_version
            str_api_method = "POST"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/commands/execute"
            str_uri = str_api_path + str_resource
            str_payload = {
                "language": str_language,
                "contextId": self.context_id,
                "clusterId": str_cluster_id,
                "command": str_command,
            }
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
            )
            if obj_response.status_code == 200:
                return True, obj_response.json()
            return False, None
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def command_status(self, str_cluster_id, str_command_id, str_version=None):
        """
        Description:
                |  This method is used to get the status of the command

        :param str_cluster_id: Cluster id
        :type str_cluster_id: String
        :param str_command_id: Command to be execute in databricks notebook
        :type str_command_id: String
        :param str_version: api version
        :type str_version: String

        :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object.
        Examples:
                |  command_status( "0107-192239-lobed1", "4KASJSHJ545SAEE", str_version = "1.2")
                |

        .. note::
                |  As of now till date 06/01/2021, this method is executable with api version 1.2 only

        """
        try:
            if self.context_id is not None:
                str_api_path = self.__databricks_api_path
                if str_version is not None:
                    str_api_path = str_api_path[0 : str_api_path.rfind("/") + 1] + str(str_version)
                str_api_method = "GET"
                dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
                str_resource = "/commands/status?clusterId={}&contextId={}&commandId={}".format(
                    str_cluster_id, self.context_id, str_command_id
                )
                str_uri = str_api_path + str_resource
                obj_response = self.call_request(str_api_method, str_uri, dict_api_header)
                if obj_response.status_code == 200:
                    return True, obj_response.json()
            return False, None
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def __create_hive_connection(self, pint_workspace_id, str_cluster_id):
        """
        Description:
                |  This private method is used to create the connection in order to execute hive queries

        :param pint_workspace_id: workspace id
        :type pint_workspace_id: integer
        :param str_cluster_id: Cluster id
        :type str_cluster_id: String

        :return: hive connection - This private function will return the hive connection object
        Examples:
                |  __create_hive_connection(7372722, "0107-192239-lobed1")
                |
        """
        try:

            str_api_path = self.__databricks_api_path
            str_api_path = str_api_path[0 : str_api_path.rfind("api") - 1]
            connection_url = "%s/sql/protocolv1/o/%s/%s" % (
                str_api_path,
                pint_workspace_id,
                str_cluster_id,
            )
            transport = THttpClient.THttpClient(connection_url)
            transport.setCustomHeaders(
                {"Authorization": "Bearer %s" % self.databricks_access_token}
            )
            hive_connection = hive.connect(thrift_transport=transport).cursor()
            return hive_connection

        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def execute_hive_query(self, pint_workspace_id, str_cluster_id, str_query):
        """
        Description:
                |  This method is used to execute the hive query

        :param pint_workspace_id: workspace id
        :type pint_workspace_id: integer
        :param str_cluster_id: Cluster id
        :type str_cluster_id: String
        :param str_query: Hive query
        :type str_query: String

        :return: lst_data_items - List of data items to be returned from this hive sql query
        Examples:
                |  execute_hive_query(7372722, "0107-192239-lobed1", "Select * from users_11_csv limit 1")
                |

        """
        lst_data_items = []
        try:
            if self.hive_connection is None:
                self.hive_connection = self.__create_hive_connection(
                    pint_workspace_id, str_cluster_id
                )
            self.hive_connection.execute(str_query)
            for data in self.hive_connection.fetchall():
                lst_data_items.append(list(data))
            return lst_data_items
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def create_access_token(
        self,
        str_username,
        str_password,
        str_auth_type="Basic",
        pint_token_lifetime_seconds=7200,
        str_comment="Data Test Automation authorization",
    ):
        """
        Description:
                |  This method is used to create the access token

        :param str_username: Databricks username
        :type str_username: String
        :param str_password: Databricks password
        :type str_password: String
        :param str_auth_type: User Authorization type
        :type str_auth_type: String
        :param pint_token_lifetime_seconds: tokekn lifetime in seconds
        :type pint_token_lifetime_seconds: integer
        :param str_comment: Comment for access tokken
        :type str_comment: String

        :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object.
        Examples:
                |  create_access_token("abc@spglobal.com", "12345678", str_auth_type = "Basic",
                |  pint_token_lifetime_seconds = 7200, str_comment = "Data Test Automation authorization")
                |

        """
        try:
            str_api_method = "POST"
            # dict_api_header = {'Authorization': 'Bearer ' + self.databricks_access_token}
            str_resource = "/token/create"
            str_uri = self.__databricks_api_path + str_resource
            str_payload = {
                "lifetime_seconds": pint_token_lifetime_seconds,
                "comment": str_comment,
            }
            """obj_response = self.call_request(str_api_method, str_uri,
            dict_api_header, str_payload=json.dumps(str_payload))"""
            obj_response = requests.post(
                str_uri,
                auth=HTTPBasicAuth(str_username, str_password),
                data=json.dumps(str_payload),
            )
            if obj_response.status_code == 200:
                return True, obj_response.json()
            return False, None
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def delete_access_token(self, str_token_id):
        """
        Description:
                |  This method is used to delete the access token

        :param str_token_id: Access Token Id
        :type str_token_id: String

        :return: boolean - Boolean value indicating 'execution_result'
        Examples:
                |  delete_access_token("33REE45455Y5H55565")
                |

        """
        try:
            str_api_method = "POST"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/token/delete"
            str_uri = self.__databricks_api_path + str_resource
            str_payload = {"token_id": str_token_id}
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
            )
            if obj_response.status_code == 200:
                return True
            return False
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def get_token_list(self):
        """
        Description:
                |  This method is used to get the list of tokens

        :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object
        containing the list of tokens.
        Examples:
                |  get_token_list()
                |
        """
        try:
            str_api_method = "GET"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/token/list"
            str_uri = self.__databricks_api_path + str_resource
            obj_response = self.call_request(str_api_method, str_uri, dict_api_header)
            if obj_response.status_code == 200:
                return True, obj_response.json()
            return False, None
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def check_job_status_and_wait(self, pint_run_id, pint_retry_count, pint_retry_interval):
        """
        Description: Check the status of a Databricks Job based on a schedule. For e.g. if pint_retry_count = 10 and
        pint_retry_interval = 30, then there will be a total 10*30 = 300 seconds wait time before the methods return
        false.
        :param pint_run_id: Databricks Job Run ID
        :type pint_run_id: String
        :param pint_retry_count: Number of times to retry to check the status
        :type pint_retry_count: int
        :param pint_retry_interval: Time interval between every retry
        :type pint_retry_interval: int
        :return: If the job status is SUCCESS method returns true.
        """
        try:
            bln_job_completed = False
            for int_cnt in range(pint_retry_count):
                bool_job_output_exists, dict_job_output = self.get_job_output(pint_run_id)
                if bool_job_output_exists and (
                    dict_job_output["metadata"]["state"]["life_cycle_state"] == "TERMINATED"
                    or dict_job_output["metadata"]["state"]["life_cycle_state"] == "INTERNAL_ERROR"
                ):
                    str_job_result_state = dict_job_output["metadata"]["state"]["result_state"]
                    if str_job_result_state is not None and str_job_result_state == "FAILED":
                        bln_job_completed = False
                        break
                    if str_job_result_state is not None and str_job_result_state == "SUCCESS":
                        bln_job_completed = True
                        break
                elif not bool_job_output_exists:
                    self.logger.info(
                        "The databricks Job Run ID do not exits or has some issue."
                        + str(pint_run_id)
                    )
                    break
                else:
                    time.sleep(pint_retry_interval)
            return bln_job_completed
        except Exception as ex:
            self.__obj_generic_exception.raise_custom_exception(str(ex))

    def get_list_of_job_runs(self, pint_job_id, pdict_payload=None):
        """
        Description:
           |  This method is used to list the databriks job runs

        :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object which
         contains the details of databricks job runs
        Examples:
           |  dict_payload = {'active_only': 'true', 'job_id': 1234567}
           |  list_job_runs(job_id, dict_payload)
        """
        if pdict_payload is None:
            pdict_payload = {}
        try:
            str_api_method = "GET"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/jobs/runs/list"
            str_uri = self.__databricks_api_path + str_resource
            if "job_id" not in pdict_payload:
                pdict_payload["job_id"] = pint_job_id
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(pdict_payload)
            )
            if obj_response.status_code == 200:
                return True, obj_response.json()
            return False
        except Exception as e:
            self.__obj_db_exception.raise_generic_exception(
                message=f"Error -> DB not connected properly: {str(e)}",
                trim_log=True,
                fail_test=False,
            )

    def call_request(self, str_method, str_url, pdict_headers, **kwargs):
        """Performs various HTTP requests (GET, POST, PUT, PATCH, DELETE).

        Args:
            str_method (str): The HTTP method (e.g., 'GET', 'POST').
            str_url (str): The request URL.
            pdict_headers (dict): Request headers.

        Kwargs:
            str_json (str): JSON data for the request body.
            str_payload (dict): Data for the request body.
            pdict_cookies (dict): Cookies for the request.
            bln_allow_redirects (bool): Allow or disallow redirects.
            str_files (str): File path for file uploads.
            bln_verify (bool): Verify SSL certificates.
            str_auth_type (str): Authentication type (e.g., 'basic', 'digest').
            str_auth_username (str): Username for authentication.
            str_auth_password (str): Password for authentication.
            ptimeout (float or tuple): Timeout in seconds for the request.

        Returns:
            requests.Response: The response object from the request.

        Examples:
            call_request("GET", "https://www.samplesite.com/param1/param2", headers={"Accept":
            "application/json"})
            call_request("POST", "https://www.samplesite.com/param1/param2", headers={"Accept":
            "application/json"}, str_payload='{"KOU": "123456"}', bln_allow_redirects=True)
        """
        try:
            if str_url == "":
                raise ValueError("URL cannot be null")
            str_json = kwargs.get("str_json", None)
            str_payload = kwargs.get("str_payload", None)
            pdict_cookies = kwargs.get("pdict_cookies", {})
            bln_allow_redirects = kwargs.get("bln_allow_redirects", False)
            str_files = kwargs.get("str_files", None)
            bln_verify = kwargs.get("bln_verify", False)
            str_auth_type = kwargs.get("str_auth_type", None)
            str_auth_username = kwargs.get("str_auth_username", None)
            str_auth_password = kwargs.get("str_auth_password", None)
            ptimeout = kwargs.get("ptimeout", None)
            pdict_proxies = kwargs.get("pdict_proxies", None)

            str_auth_string = ""
            if str_auth_type is not None:
                str_auth_string = self.security.get_auth_string(
                    str_auth_type, str_auth_username, str_auth_password
                )
            method = str_method.upper()
            if method == "GET":
                response = requests.get(
                    str_url,
                    headers=pdict_headers,
                    verify=bln_verify,
                    allow_redirects=bln_allow_redirects,
                    cookies=pdict_cookies,
                    auth=str_auth_string,
                    data=str_payload,
                    json=str_json,
                    timeout=ptimeout,
                    proxies=pdict_proxies,
                )
            elif method == "POST":
                if str_payload is not None:
                    response = requests.post(
                        str_url,
                        headers=pdict_headers,
                        data=str_payload,
                        json=str_json,
                        verify=bln_verify,
                        allow_redirects=bln_allow_redirects,
                        cookies=pdict_cookies,
                        files=str_files,
                        auth=str_auth_string,
                        timeout=ptimeout,
                        proxies=pdict_proxies,
                    )
                else:
                    raise Exception("Error-->Payload is missing")
            elif method == "PUT":
                if str_payload is not None:
                    response = requests.put(
                        str_url,
                        headers=pdict_headers,
                        data=str_payload,
                        verify=bln_verify,
                        allow_redirects=bln_allow_redirects,
                        cookies=pdict_cookies,
                        files=str_files,
                        auth=str_auth_string,
                        timeout=ptimeout,
                        proxies=pdict_proxies,
                    )
                else:
                    raise Exception("Error-->Payload is missing")
            elif method == "PATCH":
                if str_payload is not None:
                    response = requests.patch(
                        str_url,
                        headers=pdict_headers,
                        data=str_payload,
                        verify=bln_verify,
                        allow_redirects=bln_allow_redirects,
                        cookies=pdict_cookies,
                        files=str_files,
                        auth=str_auth_string,
                        timeout=ptimeout,
                        proxies=pdict_proxies,
                    )
                else:
                    raise Exception("Error-->Payload is missing")
            elif method == "DELETE":
                response = requests.delete(
                    str_url,
                    headers=pdict_headers,
                    verify=bln_verify,
                    allow_redirects=bln_allow_redirects,
                    cookies=pdict_cookies,
                    auth=str_auth_string,
                    data=str_payload,
                    json=str_json,
                    timeout=ptimeout,
                    proxies=pdict_proxies,
                )
            else:
                raise ValueError(
                    f"Invalid HTTP method: {method}. Valid options are: GET, POST, PUT, PATCH, DELETE"
                )
            return response
        except Exception as e:
            self.logger.exception(f"Error in API Request: {e}")
            raise e

__create_context(str_language, str_cluster_id, str_version=None)

Description

| This private method is used to create context id

:param str_language: coding language to be used for e.g. java/python/shell scripting :type str_language: String :param str_cluster_id: Cluster id :type str_cluster_id: String :param str_version: api version :type str_version: String

:return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object. Examples: | __create_context("python", "0107-192239-lobed1", str_version = "1.2"):

.. note:: | As of now till date 06/01/2021, this method is executable with api version 1.2 only

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def __create_context(self, str_language, str_cluster_id, str_version=None):
    """
    Description:
            |  This private method is used to create context id

    :param str_language: coding language to be used for e.g. java/python/shell scripting
    :type str_language: String
    :param str_cluster_id: Cluster id
    :type str_cluster_id: String
    :param str_version: api version
    :type str_version: String

    :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object.
    Examples:
            |  __create_context("python", "0107-192239-lobed1", str_version = "1.2"):

    .. note::
            |  As of now till date 06/01/2021, this method is executable with api version 1.2 only

    """
    try:
        str_api_path = self.__databricks_api_path
        if str_version is not None:
            str_api_path = str_api_path[0 : str_api_path.rfind("/") + 1] + str(str_version)
        str_api_method = "POST"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/contexts/create"
        str_uri = str_api_path + str_resource
        str_payload = {"language": str_language, "clusterId": str_cluster_id}
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
        )
        if obj_response.status_code == 200:
            return True, obj_response.json()
        return False, None
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

__create_hive_connection(pint_workspace_id, str_cluster_id)

Description

| This private method is used to create the connection in order to execute hive queries

:param pint_workspace_id: workspace id :type pint_workspace_id: integer :param str_cluster_id: Cluster id :type str_cluster_id: String

:return: hive connection - This private function will return the hive connection object Examples: | __create_hive_connection(7372722, "0107-192239-lobed1") |

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
 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
def __create_hive_connection(self, pint_workspace_id, str_cluster_id):
    """
    Description:
            |  This private method is used to create the connection in order to execute hive queries

    :param pint_workspace_id: workspace id
    :type pint_workspace_id: integer
    :param str_cluster_id: Cluster id
    :type str_cluster_id: String

    :return: hive connection - This private function will return the hive connection object
    Examples:
            |  __create_hive_connection(7372722, "0107-192239-lobed1")
            |
    """
    try:

        str_api_path = self.__databricks_api_path
        str_api_path = str_api_path[0 : str_api_path.rfind("api") - 1]
        connection_url = "%s/sql/protocolv1/o/%s/%s" % (
            str_api_path,
            pint_workspace_id,
            str_cluster_id,
        )
        transport = THttpClient.THttpClient(connection_url)
        transport.setCustomHeaders(
            {"Authorization": "Bearer %s" % self.databricks_access_token}
        )
        hive_connection = hive.connect(thrift_transport=transport).cursor()
        return hive_connection

    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

call_request(str_method, str_url, pdict_headers, **kwargs)

Performs various HTTP requests (GET, POST, PUT, PATCH, DELETE).

Parameters:

Name Type Description Default
str_method str

The HTTP method (e.g., 'GET', 'POST').

required
str_url str

The request URL.

required
pdict_headers dict

Request headers.

required
Kwargs

str_json (str): JSON data for the request body. str_payload (dict): Data for the request body. pdict_cookies (dict): Cookies for the request. bln_allow_redirects (bool): Allow or disallow redirects. str_files (str): File path for file uploads. bln_verify (bool): Verify SSL certificates. str_auth_type (str): Authentication type (e.g., 'basic', 'digest'). str_auth_username (str): Username for authentication. str_auth_password (str): Password for authentication. ptimeout (float or tuple): Timeout in seconds for the request.

Returns:

Type Description

requests.Response: The response object from the request.

Examples:

call_request("GET", "https://www.samplesite.com/param1/param2", headers={"Accept": "application/json"}) call_request("POST", "https://www.samplesite.com/param1/param2", headers={"Accept": "application/json"}, str_payload='{"KOU": "123456"}', bln_allow_redirects=True)

Source code in libs\cafex_db\src\cafex_db\databricks_utils.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
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
def call_request(self, str_method, str_url, pdict_headers, **kwargs):
    """Performs various HTTP requests (GET, POST, PUT, PATCH, DELETE).

    Args:
        str_method (str): The HTTP method (e.g., 'GET', 'POST').
        str_url (str): The request URL.
        pdict_headers (dict): Request headers.

    Kwargs:
        str_json (str): JSON data for the request body.
        str_payload (dict): Data for the request body.
        pdict_cookies (dict): Cookies for the request.
        bln_allow_redirects (bool): Allow or disallow redirects.
        str_files (str): File path for file uploads.
        bln_verify (bool): Verify SSL certificates.
        str_auth_type (str): Authentication type (e.g., 'basic', 'digest').
        str_auth_username (str): Username for authentication.
        str_auth_password (str): Password for authentication.
        ptimeout (float or tuple): Timeout in seconds for the request.

    Returns:
        requests.Response: The response object from the request.

    Examples:
        call_request("GET", "https://www.samplesite.com/param1/param2", headers={"Accept":
        "application/json"})
        call_request("POST", "https://www.samplesite.com/param1/param2", headers={"Accept":
        "application/json"}, str_payload='{"KOU": "123456"}', bln_allow_redirects=True)
    """
    try:
        if str_url == "":
            raise ValueError("URL cannot be null")
        str_json = kwargs.get("str_json", None)
        str_payload = kwargs.get("str_payload", None)
        pdict_cookies = kwargs.get("pdict_cookies", {})
        bln_allow_redirects = kwargs.get("bln_allow_redirects", False)
        str_files = kwargs.get("str_files", None)
        bln_verify = kwargs.get("bln_verify", False)
        str_auth_type = kwargs.get("str_auth_type", None)
        str_auth_username = kwargs.get("str_auth_username", None)
        str_auth_password = kwargs.get("str_auth_password", None)
        ptimeout = kwargs.get("ptimeout", None)
        pdict_proxies = kwargs.get("pdict_proxies", None)

        str_auth_string = ""
        if str_auth_type is not None:
            str_auth_string = self.security.get_auth_string(
                str_auth_type, str_auth_username, str_auth_password
            )
        method = str_method.upper()
        if method == "GET":
            response = requests.get(
                str_url,
                headers=pdict_headers,
                verify=bln_verify,
                allow_redirects=bln_allow_redirects,
                cookies=pdict_cookies,
                auth=str_auth_string,
                data=str_payload,
                json=str_json,
                timeout=ptimeout,
                proxies=pdict_proxies,
            )
        elif method == "POST":
            if str_payload is not None:
                response = requests.post(
                    str_url,
                    headers=pdict_headers,
                    data=str_payload,
                    json=str_json,
                    verify=bln_verify,
                    allow_redirects=bln_allow_redirects,
                    cookies=pdict_cookies,
                    files=str_files,
                    auth=str_auth_string,
                    timeout=ptimeout,
                    proxies=pdict_proxies,
                )
            else:
                raise Exception("Error-->Payload is missing")
        elif method == "PUT":
            if str_payload is not None:
                response = requests.put(
                    str_url,
                    headers=pdict_headers,
                    data=str_payload,
                    verify=bln_verify,
                    allow_redirects=bln_allow_redirects,
                    cookies=pdict_cookies,
                    files=str_files,
                    auth=str_auth_string,
                    timeout=ptimeout,
                    proxies=pdict_proxies,
                )
            else:
                raise Exception("Error-->Payload is missing")
        elif method == "PATCH":
            if str_payload is not None:
                response = requests.patch(
                    str_url,
                    headers=pdict_headers,
                    data=str_payload,
                    verify=bln_verify,
                    allow_redirects=bln_allow_redirects,
                    cookies=pdict_cookies,
                    files=str_files,
                    auth=str_auth_string,
                    timeout=ptimeout,
                    proxies=pdict_proxies,
                )
            else:
                raise Exception("Error-->Payload is missing")
        elif method == "DELETE":
            response = requests.delete(
                str_url,
                headers=pdict_headers,
                verify=bln_verify,
                allow_redirects=bln_allow_redirects,
                cookies=pdict_cookies,
                auth=str_auth_string,
                data=str_payload,
                json=str_json,
                timeout=ptimeout,
                proxies=pdict_proxies,
            )
        else:
            raise ValueError(
                f"Invalid HTTP method: {method}. Valid options are: GET, POST, PUT, PATCH, DELETE"
            )
        return response
    except Exception as e:
        self.logger.exception(f"Error in API Request: {e}")
        raise e

check_job_status_and_wait(pint_run_id, pint_retry_count, pint_retry_interval)

Description: Check the status of a Databricks Job based on a schedule. For e.g. if pint_retry_count = 10 and pint_retry_interval = 30, then there will be a total 10*30 = 300 seconds wait time before the methods return false. :param pint_run_id: Databricks Job Run ID :type pint_run_id: String :param pint_retry_count: Number of times to retry to check the status :type pint_retry_count: int :param pint_retry_interval: Time interval between every retry :type pint_retry_interval: int :return: If the job status is SUCCESS method returns true.

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def check_job_status_and_wait(self, pint_run_id, pint_retry_count, pint_retry_interval):
    """
    Description: Check the status of a Databricks Job based on a schedule. For e.g. if pint_retry_count = 10 and
    pint_retry_interval = 30, then there will be a total 10*30 = 300 seconds wait time before the methods return
    false.
    :param pint_run_id: Databricks Job Run ID
    :type pint_run_id: String
    :param pint_retry_count: Number of times to retry to check the status
    :type pint_retry_count: int
    :param pint_retry_interval: Time interval between every retry
    :type pint_retry_interval: int
    :return: If the job status is SUCCESS method returns true.
    """
    try:
        bln_job_completed = False
        for int_cnt in range(pint_retry_count):
            bool_job_output_exists, dict_job_output = self.get_job_output(pint_run_id)
            if bool_job_output_exists and (
                dict_job_output["metadata"]["state"]["life_cycle_state"] == "TERMINATED"
                or dict_job_output["metadata"]["state"]["life_cycle_state"] == "INTERNAL_ERROR"
            ):
                str_job_result_state = dict_job_output["metadata"]["state"]["result_state"]
                if str_job_result_state is not None and str_job_result_state == "FAILED":
                    bln_job_completed = False
                    break
                if str_job_result_state is not None and str_job_result_state == "SUCCESS":
                    bln_job_completed = True
                    break
            elif not bool_job_output_exists:
                self.logger.info(
                    "The databricks Job Run ID do not exits or has some issue."
                    + str(pint_run_id)
                )
                break
            else:
                time.sleep(pint_retry_interval)
        return bln_job_completed
    except Exception as ex:
        self.__obj_generic_exception.raise_custom_exception(str(ex))

check_notebook(str_workspace_path)

Description

| This method is to check if the notebook exists at the given workspace path

:param str_workspace_path: Databricks Workspace Path :type str_workspace_path: String

:return: boolean - Indicating the result whether notebook actually exists at the given databricks workspace path Examples: | check_notebook("/Shared/mdata/sample")

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def check_notebook(self, str_workspace_path):
    """
    Description:
            |  This method is to check if the notebook exists at the given workspace path

    :param str_workspace_path: Databricks Workspace Path
    :type str_workspace_path: String

    :return: boolean - Indicating the result whether notebook actually exists at the given databricks workspace path
    Examples:
            |  check_notebook("/Shared/mdata/sample")

    """
    try:
        str_api_method = "GET"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/workspace/get-status"
        str_uri = self.__databricks_api_path + str_resource
        str_payload = {"path": str_workspace_path}
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
        )
        if obj_response.status_code == 200:
            if obj_response.json()["object_type"] == "NOTEBOOK":
                return True
        return False
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

command_status(str_cluster_id, str_command_id, str_version=None)

Description

| This method is used to get the status of the command

:param str_cluster_id: Cluster id :type str_cluster_id: String :param str_command_id: Command to be execute in databricks notebook :type str_command_id: String :param str_version: api version :type str_version: String

:return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object. Examples: | command_status( "0107-192239-lobed1", "4KASJSHJ545SAEE", str_version = "1.2") |

.. note:: | As of now till date 06/01/2021, this method is executable with api version 1.2 only

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def command_status(self, str_cluster_id, str_command_id, str_version=None):
    """
    Description:
            |  This method is used to get the status of the command

    :param str_cluster_id: Cluster id
    :type str_cluster_id: String
    :param str_command_id: Command to be execute in databricks notebook
    :type str_command_id: String
    :param str_version: api version
    :type str_version: String

    :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object.
    Examples:
            |  command_status( "0107-192239-lobed1", "4KASJSHJ545SAEE", str_version = "1.2")
            |

    .. note::
            |  As of now till date 06/01/2021, this method is executable with api version 1.2 only

    """
    try:
        if self.context_id is not None:
            str_api_path = self.__databricks_api_path
            if str_version is not None:
                str_api_path = str_api_path[0 : str_api_path.rfind("/") + 1] + str(str_version)
            str_api_method = "GET"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/commands/status?clusterId={}&contextId={}&commandId={}".format(
                str_cluster_id, self.context_id, str_command_id
            )
            str_uri = str_api_path + str_resource
            obj_response = self.call_request(str_api_method, str_uri, dict_api_header)
            if obj_response.status_code == 200:
                return True, obj_response.json()
        return False, None
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

create_access_token(str_username, str_password, str_auth_type='Basic', pint_token_lifetime_seconds=7200, str_comment='Data Test Automation authorization')

Description

| This method is used to create the access token

:param str_username: Databricks username :type str_username: String :param str_password: Databricks password :type str_password: String :param str_auth_type: User Authorization type :type str_auth_type: String :param pint_token_lifetime_seconds: tokekn lifetime in seconds :type pint_token_lifetime_seconds: integer :param str_comment: Comment for access tokken :type str_comment: String

:return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object. Examples: | create_access_token("abc@spglobal.com", "12345678", str_auth_type = "Basic", | pint_token_lifetime_seconds = 7200, str_comment = "Data Test Automation authorization") |

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def create_access_token(
    self,
    str_username,
    str_password,
    str_auth_type="Basic",
    pint_token_lifetime_seconds=7200,
    str_comment="Data Test Automation authorization",
):
    """
    Description:
            |  This method is used to create the access token

    :param str_username: Databricks username
    :type str_username: String
    :param str_password: Databricks password
    :type str_password: String
    :param str_auth_type: User Authorization type
    :type str_auth_type: String
    :param pint_token_lifetime_seconds: tokekn lifetime in seconds
    :type pint_token_lifetime_seconds: integer
    :param str_comment: Comment for access tokken
    :type str_comment: String

    :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object.
    Examples:
            |  create_access_token("abc@spglobal.com", "12345678", str_auth_type = "Basic",
            |  pint_token_lifetime_seconds = 7200, str_comment = "Data Test Automation authorization")
            |

    """
    try:
        str_api_method = "POST"
        # dict_api_header = {'Authorization': 'Bearer ' + self.databricks_access_token}
        str_resource = "/token/create"
        str_uri = self.__databricks_api_path + str_resource
        str_payload = {
            "lifetime_seconds": pint_token_lifetime_seconds,
            "comment": str_comment,
        }
        """obj_response = self.call_request(str_api_method, str_uri,
        dict_api_header, str_payload=json.dumps(str_payload))"""
        obj_response = requests.post(
            str_uri,
            auth=HTTPBasicAuth(str_username, str_password),
            data=json.dumps(str_payload),
        )
        if obj_response.status_code == 200:
            return True, obj_response.json()
        return False, None
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

create_cluster(pdict_payload)

Description

| This method is used to create cluster in databricks

:param pdict_payload: dictionary payload to create a cluster :type pdict_payload: dictionary

:return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object .. note:: | For creating a cluster, the user needs to have create permissions on databricks.

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def create_cluster(self, pdict_payload):
    """
    Description:
            |  This method is used to create cluster in databricks

    :param pdict_payload: dictionary payload to create a cluster
    :type pdict_payload: dictionary

    :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object
    .. note::
            |  For creating a cluster, the user needs to have create permissions on databricks.

    """
    try:
        str_api_method = "POST"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/clusters/create"
        str_uri = self.__databricks_api_path + str_resource
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(pdict_payload)
        )
        if obj_response.status_code == 200:
            return True, obj_response.json()
        return False, None
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

create_job(pdict_payload)

Description

| This method is used to create a spark job in databricks

:param pdict_payload: dictionary payload to create a data processing job :type pdict_payload: dictionary

:return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def create_job(self, pdict_payload):
    """
    Description:
            |  This method is used to create a spark job in databricks

    :param pdict_payload: dictionary payload to create a data processing job
    :type pdict_payload: dictionary

    :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object
    """
    try:
        str_api_method = "POST"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/jobs/create"
        str_uri = self.__databricks_api_path + str_resource
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(pdict_payload)
        )
        if obj_response.status_code == 200:
            return True, obj_response.json()
        return False, None
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

create_notebook(str_file_path, str_workspace_path, str_language='PYTHON')

Description

| This method is used to create a notebook in databricks workspace.

:param str_file_path: Source file path for databricks notebook :type str_file_path: String :param str_workspace_path: Target Workspace path where notebook will be created :type str_workspace_path: String :param str_language: Coding Language used (Optional parameter with default value 'PYTHON') :type str_language: String

:return: boolean - Indicating the result whether notebook is successully created or not. Examples: | create_notebook("\queries\data_ingestion_pipeline\sample.py", "/Shared/mdata/sample") | create_notebook("\queries\data_ingestion_pipeline\sample.py", "/Shared/mdata/sample", str_language="PYTHON")

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def create_notebook(self, str_file_path, str_workspace_path, str_language="PYTHON"):
    """
    Description:
            |  This method is used to create a notebook in databricks workspace.

    :param str_file_path: Source file path for databricks notebook
    :type str_file_path: String
    :param str_workspace_path: Target Workspace path where notebook will be created
    :type str_workspace_path: String
    :param str_language: Coding Language used (Optional parameter with default value 'PYTHON')
    :type str_language: String

    :return: boolean - Indicating the result whether notebook is successully created or not.
    Examples:
            |  create_notebook("\queries\data_ingestion_pipeline\sample.py", "/Shared/mdata/sample")
            |  create_notebook("\queries\data_ingestion_pipeline\sample.py", "/Shared/mdata/sample", str_language="PYTHON")

    """
    try:
        str_api_method = "POST"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/workspace/import"
        str_uri = self.__databricks_api_path + str_resource
        with open(str_file_path, "rb") as fp:
            file_data = fp.read()
            encoded_data = base64.b64encode(file_data)
        decoded_data = encoded_data.decode("utf-8")
        str_payload = {
            "path": str_workspace_path,
            "format": "SOURCE",
            "language": str_language,
            "content": decoded_data,
            "overwrite": "false",
        }
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
        )
        if obj_response.status_code == 200:
            return True
        return False
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

delete_access_token(str_token_id)

Description

| This method is used to delete the access token

:param str_token_id: Access Token Id :type str_token_id: String

:return: boolean - Boolean value indicating 'execution_result' Examples: | delete_access_token("33REE45455Y5H55565") |

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def delete_access_token(self, str_token_id):
    """
    Description:
            |  This method is used to delete the access token

    :param str_token_id: Access Token Id
    :type str_token_id: String

    :return: boolean - Boolean value indicating 'execution_result'
    Examples:
            |  delete_access_token("33REE45455Y5H55565")
            |

    """
    try:
        str_api_method = "POST"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/token/delete"
        str_uri = self.__databricks_api_path + str_resource
        str_payload = {"token_id": str_token_id}
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
        )
        if obj_response.status_code == 200:
            return True
        return False
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

delete_cluster(str_cluster_id)

Description

| This method is used to permanently delete cluster in databricks

:param str_cluster_id: Databricks Cluster Id :type str_cluster_id: String

:return: boolean - True/False indicating 'execution_result' from the api response Examples: | delete_cluster("0715-113640-giddy195")

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def delete_cluster(self, str_cluster_id):
    """
    Description:
            |  This method is used to permanently delete cluster in databricks

    :param str_cluster_id: Databricks Cluster Id
    :type str_cluster_id: String

    :return: boolean - True/False indicating 'execution_result' from the api response
    Examples:
            |  delete_cluster("0715-113640-giddy195")

    """
    try:
        str_api_method = "POST"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/clusters/permanent-delete"
        str_uri = self.__databricks_api_path + str_resource
        str_payload = {"cluster_id": str_cluster_id}
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
        )
        if obj_response.status_code == 200:
            return True, obj_response.json()
        return False, None
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

delete_dbfs_path(str_dbfs_path, bln_recursive=False)

Description

| This method is used to delete file at dbfs path

:param str_dbfs_path: dbfs path :type str_dbfs_path: String :param bln_recursive: Boolean parameter for deleting the files recursively at given dbfs path (default value: False) :type bln_recursive: Boolean

:return: boolean - True/False indicating 'execution_result' from the api response Examples: | delete_dbfs_path("/FileStore/mdata-1/users-11.csv") | delete_dbfs_path("/FileStore/mdata-1", True)

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def delete_dbfs_path(self, str_dbfs_path, bln_recursive=False):
    """
    Description:
            |  This method is used to delete file at dbfs path

    :param str_dbfs_path: dbfs path
    :type str_dbfs_path: String
    :param bln_recursive: Boolean parameter for deleting the files recursively at given dbfs path
    (default value: False)
    :type bln_recursive: Boolean

    :return: boolean - True/False indicating 'execution_result' from the api response
    Examples:
            |  delete_dbfs_path("/FileStore/mdata-1/users-11.csv")
            |  delete_dbfs_path("/FileStore/mdata-1", True)

    """
    try:
        str_api_method = "POST"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/dbfs/delete"
        str_uri = self.__databricks_api_path + str_resource
        str_payload = {"path": str_dbfs_path, "recursive": bln_recursive}
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
        )
        if obj_response.status_code == 200:
            return True
        return False
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

delete_job(pint_job_id)

Description

| This method is used to delete a spark job in databricks

:param pint_job_id: spark job id :type pint_job_id: integer

:return: boolean - True/False indicating 'execution_result' from the api response Examples: | delete_job(34343)

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def delete_job(self, pint_job_id):
    """
    Description:
            |  This method is used to delete a spark job in databricks

    :param pint_job_id: spark job id
    :type pint_job_id: integer

    :return: boolean - True/False indicating 'execution_result' from the api response
    Examples:
            |  delete_job(34343)

    """
    try:
        str_api_method = "POST"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/jobs/delete"
        str_uri = self.__databricks_api_path + str_resource
        dict_payload = {"job_id": pint_job_id}
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(dict_payload)
        )
        if obj_response.status_code == 200:
            return True
        return False
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

delete_notebook(str_workspace_path, bln_recursive=False)

Description

| This method is to delete the notebook exists at the given workspace path

:param str_workspace_path: Databricks Workspace Path :type str_workspace_path: String :param bln_recursive: An optional boolean parameter to delete recursively all the files at given workspace path (default Value: False) :type bln_recursive: Boolean

:return: boolean - Indicating the result whether notebook actually deleted at the given databricks workspace path or not. Examples: | delete_notebook("/Shared/mdata/sample") | delete_notebook("/Shared/mdata/", True)

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def delete_notebook(self, str_workspace_path, bln_recursive=False):
    """
    Description:
            |  This method is to delete the notebook exists at the given workspace path

    :param str_workspace_path: Databricks Workspace Path
    :type str_workspace_path: String
    :param bln_recursive: An optional boolean parameter to delete recursively all the files at given workspace path
     (default Value: False)
    :type bln_recursive: Boolean

    :return: boolean - Indicating the result whether notebook actually deleted at the given databricks workspace
    path or not.
    Examples:
            |  delete_notebook("/Shared/mdata/sample")
            |  delete_notebook("/Shared/mdata/", True)

    """
    try:
        str_api_method = "POST"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/workspace/delete"
        str_uri = self.__databricks_api_path + str_resource
        str_payload = {"path": str_workspace_path, "recursive": bln_recursive}
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
        )
        if obj_response.status_code == 200:
            return True
        return False
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

destroy_context(str_cluster_id, str_version=None)

Description

| This method is used to destroy context id

:param str_cluster_id: Cluster id :type str_cluster_id: String :param str_version: api version :type str_version: String

:return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object. Examples: | destroy_context("0107-192239-lobed1", str_version = "1.2"):

.. note:: | As of now till date 06/01/2021, this method is executable with api version 1.2 only

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def destroy_context(self, str_cluster_id, str_version=None):
    """
    Description:
            |  This method is used to destroy context id

    :param str_cluster_id: Cluster id
    :type str_cluster_id: String
    :param str_version: api version
    :type str_version: String

    :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object.
    Examples:
            |  destroy_context("0107-192239-lobed1", str_version = "1.2"):

    .. note::
            |  As of now till date 06/01/2021, this method is executable with api version 1.2 only

    """
    try:
        if self.context_id is not None:
            str_api_path = self.__databricks_api_path
            if str_version is not None:
                str_api_path = str_api_path[0 : str_api_path.rfind("/") + 1] + str(str_version)
            str_api_method = "POST"
            dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
            str_resource = "/contexts/destroy"
            str_uri = str_api_path + str_resource
            str_payload = {"contextId": self.context_id, "clusterId": str_cluster_id}
            obj_response = self.call_request(
                str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
            )
            if obj_response.status_code == 200:
                return True, obj_response.json()
        return False, None
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

execute_command(str_language, str_cluster_id, str_command, str_version=None)

Description

| This method is used to execute command

:param str_language: coding language used :type str_language: String :param str_cluster_id: Cluster id :type str_cluster_id: String :param str_command: Command to be execute in databricks notebook :type str_command: String :param str_version: api version :type str_version: String

:return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object. Examples: | execute_command("sql", "0107-192239-lobed1", "Select * from users_11_csv limit 1", str_version = "1.2") | execute_command("python", "0107-192239-lobed1", "dbutils.fs.ls('/FileStore/mdata-1')", str_version = "1.2"):

.. note:: | As of now till date 06/01/2021, this method is executable with api version 1.2 only

Source code in libs\cafex_db\src\cafex_db\databricks_utils.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
def execute_command(self, str_language, str_cluster_id, str_command, str_version=None):
    """
    Description:
            |  This method is used to execute command

    :param str_language: coding language used
    :type str_language: String
    :param str_cluster_id: Cluster id
    :type str_cluster_id: String
    :param str_command: Command to be execute in databricks notebook
    :type str_command: String
    :param str_version: api version
    :type str_version: String

    :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object.
    Examples:
            |  execute_command("sql", "0107-192239-lobed1", "Select * from users_11_csv limit 1", str_version = "1.2")
            |  execute_command("python", "0107-192239-lobed1", "dbutils.fs.ls('/FileStore/mdata-1')", str_version = "1.2"):

    .. note::
            |  As of now till date 06/01/2021, this method is executable with api version 1.2 only

    """
    try:
        if self.context_id is None:
            execution, response = self.__create_context(
                str_language, str_cluster_id, str_version=str_version
            )
            if execution:
                self.context_id = response["id"]
        str_api_path = self.__databricks_api_path
        if str_version is not None:
            str_api_path = str_api_path[0 : str_api_path.rfind("/") + 1] + str_version
        str_api_method = "POST"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/commands/execute"
        str_uri = str_api_path + str_resource
        str_payload = {
            "language": str_language,
            "contextId": self.context_id,
            "clusterId": str_cluster_id,
            "command": str_command,
        }
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
        )
        if obj_response.status_code == 200:
            return True, obj_response.json()
        return False, None
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

execute_hive_query(pint_workspace_id, str_cluster_id, str_query)

Description

| This method is used to execute the hive query

:param pint_workspace_id: workspace id :type pint_workspace_id: integer :param str_cluster_id: Cluster id :type str_cluster_id: String :param str_query: Hive query :type str_query: String

:return: lst_data_items - List of data items to be returned from this hive sql query Examples: | execute_hive_query(7372722, "0107-192239-lobed1", "Select * from users_11_csv limit 1") |

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def execute_hive_query(self, pint_workspace_id, str_cluster_id, str_query):
    """
    Description:
            |  This method is used to execute the hive query

    :param pint_workspace_id: workspace id
    :type pint_workspace_id: integer
    :param str_cluster_id: Cluster id
    :type str_cluster_id: String
    :param str_query: Hive query
    :type str_query: String

    :return: lst_data_items - List of data items to be returned from this hive sql query
    Examples:
            |  execute_hive_query(7372722, "0107-192239-lobed1", "Select * from users_11_csv limit 1")
            |

    """
    lst_data_items = []
    try:
        if self.hive_connection is None:
            self.hive_connection = self.__create_hive_connection(
                pint_workspace_id, str_cluster_id
            )
        self.hive_connection.execute(str_query)
        for data in self.hive_connection.fetchall():
            lst_data_items.append(list(data))
        return lst_data_items
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

execute_job(pint_job_id, pdict_params=None)

Description

| This method is used to execute the spark job

:param pint_job_id: Spark Job id :type pint_job_id: integer :param pdict_params: dictionary object which contains all the optional parameters to execute spark job with :type pdict_params: dictionary

:return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object. Examples: | parameter_dict = { | "notebook_params": { | "fileLocation": "/FileStore/mdata-1/users-11.csv" | } | } | execute_job(65656) | execute_job(65656, pdict_params=parameter_dict )

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def execute_job(self, pint_job_id, pdict_params=None):
    """
    Description:
            |  This method is used to execute the spark job

    :param pint_job_id: Spark Job id
    :type pint_job_id: integer
    :param pdict_params: dictionary object which contains all the optional parameters to execute spark job with
    :type pdict_params: dictionary

    :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object.
    Examples:
            |  parameter_dict = {
        |    "notebook_params": {
        |        "fileLocation": "/FileStore/mdata-1/users-11.csv"
        |        }
        |    }
            |  execute_job(65656)
            |  execute_job(65656, pdict_params=parameter_dict )

    """
    try:
        str_api_method = "POST"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/jobs/run-now"
        str_uri = self.__databricks_api_path + str_resource
        dict_payload = {"job_id": pint_job_id}
        if pdict_params is not None:
            for key, value in pdict_params.items():
                dict_payload[key] = value
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(dict_payload)
        )
        if obj_response.status_code == 200:
            return True, obj_response.json()
        return False, None
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

get_cluster(str_cluster_id)

Description

| This method is used to get the cluster

:param str_cluster_id: Databricks Cluster Id :type str_cluster_id: String

:return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object which contains the details of cluster Examples: | get_cluster("0715-113640-giddy195")

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def get_cluster(self, str_cluster_id):
    """
    Description:
            |  This method is used to get the cluster

    :param str_cluster_id: Databricks Cluster Id
    :type str_cluster_id: String

    :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object
    which contains the details of cluster
    Examples:
            |  get_cluster("0715-113640-giddy195")

    """
    try:
        str_api_method = "GET"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/clusters/get"
        str_uri = self.__databricks_api_path + str_resource
        str_payload = {"cluster_id": str_cluster_id}
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
        )
        if obj_response.status_code == 200:
            return True, obj_response.json()
        return False, None
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

get_cluster_id(str_cluster_name)

Description

| This method is used to get the cluster id with the name

:param str_cluster_name: Databricks Cluster Name :type str_cluster_name: String

:return: String - Cluster Id Examples: | get_cluster_id("databricks-dev-training-spark2")

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def get_cluster_id(self, str_cluster_name):
    """
    Description:
            |  This method is used to get the cluster id with the name

    :param str_cluster_name: Databricks Cluster Name
    :type str_cluster_name: String

    :return: String - Cluster Id
    Examples:
            |  get_cluster_id("databricks-dev-training-spark2")

    """
    try:
        str_cluster_id = None
        execution, response_cluster_list = self.list_clusters()
        if execution:
            data = next(
                (
                    cluster
                    for cluster in response_cluster_list["clusters"]
                    if cluster["cluster_name"].lower() == str_cluster_name.lower()
                ),
                None,
            )
            if data is not None:
                str_cluster_id = data["cluster_id"]
        return str_cluster_id
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

get_job(pint_job_id)

Description

| This method is used to get the job details with the id

:param pint_job_id: Spark Job id :type pint_job_id: integer

:return: boolean, dict - Tuple without parentheses indicating 'execution_result' and spark job dictionary object. Examples: | get_job(476474)

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def get_job(self, pint_job_id):
    """
    Description:
            |  This method is used to get the job details with the id

    :param pint_job_id: Spark Job id
    :type pint_job_id: integer

    :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and spark job dictionary object.
    Examples:
            |  get_job(476474)

    """
    try:
        str_api_method = "GET"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/jobs/get"
        str_uri = self.__databricks_api_path + str_resource
        dict_payload = {"job_id": pint_job_id}
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(dict_payload)
        )
        if obj_response.status_code == 200:
            return True, obj_response.json()
        return False, None
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

get_job_id(str_job_name)

Description

| This method is used to get the spark job id with it's name.

:param str_job_name: Spark Job name :type str_job_name: String

:return: boolean, dict - Tuple without parentheses indicating 'execution_result' and spark job id. Examples: | get_job_id("Student_analysis_job")

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def get_job_id(self, str_job_name):
    """
    Description:
            |  This method is used to get the spark job id with it's name.

    :param str_job_name: Spark Job name
    :type str_job_name: String

    :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and spark job id.
    Examples:
            |  get_job_id("Student_analysis_job")

    """
    try:
        job_id = None
        execution, response_jobs_list = self.list_jobs()
        if execution:
            data = next(
                (
                    job
                    for job in response_jobs_list["jobs"]
                    if job["settings"]["name"].lower() == str_job_name.lower()
                ),
                None,
            )
            if data is not None:
                job_id = data["job_id"]
                return True, job_id
        return False, job_id
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

get_job_output(pint_run_id)

Description

| This method is used to get the job output

:param pint_job_id: Spark Job id :type pint_job_id: integer

:return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object. Examples: | get_job_output(65655)

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def get_job_output(self, pint_run_id):
    """
    Description:
            |  This method is used to get the job output

    :param pint_job_id: Spark Job id
    :type pint_job_id: integer

    :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object.
    Examples:
            |  get_job_output(65655)

    """
    try:
        str_api_method = "GET"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/jobs/runs/get-output"
        str_uri = self.__databricks_api_path + str_resource
        dict_payload = {"run_id": pint_run_id}
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(dict_payload)
        )
        if obj_response.status_code == 200:
            return True, obj_response.json()
        return False, None
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

get_list_of_job_runs(pint_job_id, pdict_payload=None)

Description

| This method is used to list the databriks job runs

:return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object which contains the details of databricks job runs Examples: | dict_payload = {'active_only': 'true', 'job_id': 1234567} | list_job_runs(job_id, dict_payload)

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def get_list_of_job_runs(self, pint_job_id, pdict_payload=None):
    """
    Description:
       |  This method is used to list the databriks job runs

    :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object which
     contains the details of databricks job runs
    Examples:
       |  dict_payload = {'active_only': 'true', 'job_id': 1234567}
       |  list_job_runs(job_id, dict_payload)
    """
    if pdict_payload is None:
        pdict_payload = {}
    try:
        str_api_method = "GET"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/jobs/runs/list"
        str_uri = self.__databricks_api_path + str_resource
        if "job_id" not in pdict_payload:
            pdict_payload["job_id"] = pint_job_id
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(pdict_payload)
        )
        if obj_response.status_code == 200:
            return True, obj_response.json()
        return False
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

get_token_list()

Description

| This method is used to get the list of tokens

:return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object containing the list of tokens. Examples: | get_token_list() |

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def get_token_list(self):
    """
    Description:
            |  This method is used to get the list of tokens

    :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object
    containing the list of tokens.
    Examples:
            |  get_token_list()
            |
    """
    try:
        str_api_method = "GET"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/token/list"
        str_uri = self.__databricks_api_path + str_resource
        obj_response = self.call_request(str_api_method, str_uri, dict_api_header)
        if obj_response.status_code == 200:
            return True, obj_response.json()
        return False, None
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

list_clusters()

Description

| This method is to get the list of clusters in the Databricks

:return: boolean, list - Tuple without parentheses indicating 'execution_result' and list object which contains the list of clusters in databricks Examples: | list_clusters()

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def list_clusters(self):
    """
    Description:
            |  This method is to get the list of clusters in the Databricks

    :return: boolean, list - Tuple without parentheses indicating 'execution_result' and list object which contains
     the list of clusters in databricks
    Examples:
            |  list_clusters()

    """
    try:
        str_api_method = "GET"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/clusters/list"
        str_uri = self.__databricks_api_path + str_resource
        obj_response = self.call_request(str_api_method, str_uri, dict_api_header)
        if obj_response.status_code == 200:
            return True, obj_response.json()
        return False, None
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

list_dbfs_files(str_dbfs_path)

Description

| This method is used to get the list of files at given dbfs path.

:param str_dbfs_path: DBFS path in databricks :type str_dbfs_path: String

:return: boolean, list - Tuple without parentheses indicating 'execution_result' and list object which contains the list of files at given dbfs path Examples: | list_dbfs_files("/FileStore/mdata-1/")

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def list_dbfs_files(self, str_dbfs_path):
    """
    Description:
            |  This method is used to get the list of files at given dbfs path.

    :param str_dbfs_path: DBFS path in databricks
    :type str_dbfs_path: String

    :return: boolean, list - Tuple without parentheses indicating 'execution_result' and list object which contains
    the list of files at given dbfs path
    Examples:
            |  list_dbfs_files("/FileStore/mdata-1/")

    """
    try:
        str_api_method = "GET"
        dict_api_header = {"Content-Type": "application/json"}
        dict_api_header["Authorization"] = "Bearer " + self.databricks_access_token
        str_resource = "/dbfs/list"
        str_uri = self.__databricks_api_path + str_resource
        str_payload = {"path": str_dbfs_path}
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
        )
        if obj_response.status_code == 200:
            return True, obj_response.json()
        return False, None
    except Exception as e:  # pylint: disable=broad-exception-caught
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

list_jobs()

Description

| This method is used to all the spark jobs in databricks

:return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object which contains the details of all the jobs Examples: | list_jobs()

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def list_jobs(self):
    """
    Description:
            |  This method is used to all the spark jobs in databricks

    :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object which
     contains the details of all the jobs
    Examples:
            |  list_jobs()

    """
    try:
        str_api_method = "GET"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/jobs/list"
        str_uri = self.__databricks_api_path + str_resource
        obj_response = self.call_request(str_api_method, str_uri, dict_api_header)
        if obj_response.status_code == 200:
            return True, obj_response.json()
        return False, None
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

read_dbfs_file(str_dbfs_path, pint_offset=0, pint_length=1000)

Description

| This method is used to read file from the dbfs path

:param str_dbfs_path: dbfs path :type str_dbfs_path: String :param pint_offset: offset: the point from where to start reading the content :type pint_offset: integer :param pint_length: length of the content to read in terms of bytes :type pint_length: integer

:return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object. Examples: | read_dbfs_file("/FileStore/mdata-1/users-11.csv", pint_offset=0, pint_length=10000)

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def read_dbfs_file(self, str_dbfs_path, pint_offset=0, pint_length=1000):
    """
    Description:
            |  This method is used to read file from the dbfs path

    :param str_dbfs_path: dbfs path
    :type str_dbfs_path: String
    :param pint_offset: offset: the point from where to start reading the content
    :type pint_offset: integer
    :param pint_length: length of the content to read in terms of bytes
    :type pint_length: integer

    :return: boolean, dict - Tuple without parentheses indicating 'execution_result' and dictionary object.
    Examples:
            |  read_dbfs_file("/FileStore/mdata-1/users-11.csv", pint_offset=0, pint_length=10000)

    """
    try:
        str_api_method = "GET"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/dbfs/read"
        str_uri = self.__databricks_api_path + str_resource
        str_payload = {"path": str_dbfs_path, "offset": pint_offset, "length": pint_length}
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
        )
        if obj_response.status_code == 200:
            return True, obj_response.json()
        return False, None
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

restart_cluster(str_cluster_id)

Description

| This method is used to restart cluster in databricks

:param str_cluster_id: Databricks Cluster Id :type str_cluster_id: String

:return: boolean - True/False indicating 'execution_result' from the api response Examples: | restart_cluster("0715-113640-giddy195")

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def restart_cluster(self, str_cluster_id):
    """
    Description:
            |  This method is used to restart cluster in databricks

    :param str_cluster_id: Databricks Cluster Id
    :type str_cluster_id: String

    :return: boolean - True/False indicating 'execution_result' from the api response
    Examples:
            |  restart_cluster("0715-113640-giddy195")

    """
    try:
        str_api_method = "POST"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/clusters/restart"
        str_uri = self.__databricks_api_path + str_resource
        str_payload = {"cluster_id": str_cluster_id}
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
        )
        if obj_response.status_code == 200:
            return True
        return False
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

start_cluster(str_cluster_id)

Description

| This method is used to start the cluster

:param str_cluster_id: Databricks Cluster Id :type str_cluster_id: String

:return: boolean - True/False indicating 'execution_result' from the api response Examples: | start_cluster("0715-113640-giddy195")

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def start_cluster(self, str_cluster_id):
    """
    Description:
            |  This method is used to start the cluster

    :param str_cluster_id: Databricks Cluster Id
    :type str_cluster_id: String

    :return: boolean - True/False indicating 'execution_result' from the api response
    Examples:
            |  start_cluster("0715-113640-giddy195")

    """
    try:
        str_api_method = "POST"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/clusters/start"
        str_uri = self.__databricks_api_path + str_resource
        str_payload = {"cluster_id": str_cluster_id}
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
        )
        if obj_response.status_code == 200:
            return True
        return False
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

stop_cluster(str_cluster_id)

Description

| This method is used to stop cluster in databricks

:param str_cluster_id: Databricks Cluster Id :type str_cluster_id: String

:return: boolean - True/False indicating 'execution_result' from the api response Examples: | stop_cluster("0715-113640-giddy195")

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
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
def stop_cluster(self, str_cluster_id):
    """
    Description:
            |  This method is used to stop cluster in databricks

    :param str_cluster_id: Databricks Cluster Id
    :type str_cluster_id: String

    :return: boolean - True/False indicating 'execution_result' from the api response
    Examples:
            |  stop_cluster("0715-113640-giddy195")

    """
    try:
        str_api_method = "POST"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/clusters/delete"
        str_uri = self.__databricks_api_path + str_resource
        str_payload = {"cluster_id": str_cluster_id}
        obj_response = self.call_request(
            str_api_method, str_uri, dict_api_header, str_payload=json.dumps(str_payload)
        )
        if obj_response.status_code == 200:
            return True
        return False
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )

upload_files_in_dbfs(str_src_path, str_dbfs_path)

Description

| This method is used to upload files from the source path to the given dbfs path.

:param str_src_path: Source Path for the File to upload :type str_src_path: String :param str_dbfs_path: DBFS path :type str_dbfs_path: String

:return: boolean - Indicating the result whether file is uploaded successully or not. Examples: | upload_files_in_dbfs( "\queries\data_ingestion_pipeline\DatabricksApps-0.0.7-SNAPSHOT.jar", "/FileStore/mdata-1/") | upload_files_in_dbfs( " estdata\data_ingestion_pipeline\student.csv", "/FileStore/mdata-1/")

.. note:: | Please don't include the name of file in 'str_dbfs_path' parameter. Refer Examples

Source code in libs\cafex_db\src\cafex_db\databricks_utils.py
 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
def upload_files_in_dbfs(self, str_src_path, str_dbfs_path):
    """
    Description:
            |  This method is used to upload files from the source path to the given dbfs path.

    :param str_src_path: Source Path for the File to upload
    :type str_src_path: String
    :param str_dbfs_path: DBFS path
    :type str_dbfs_path: String

    :return: boolean - Indicating the result whether file is uploaded successully or not.
    Examples:
            |  upload_files_in_dbfs( "\queries\data_ingestion_pipeline\DatabricksApps-0.0.7-SNAPSHOT.jar", "/FileStore/mdata-1/")
            |  upload_files_in_dbfs( "\testdata\data_ingestion_pipeline\student.csv", "/FileStore/mdata-1/")

    .. note::
            |  Please don't include the name of file in 'str_dbfs_path' parameter. Refer Examples

    """
    try:
        str_api_method = "POST"
        dict_api_header = {"Authorization": "Bearer " + self.databricks_access_token}
        str_resource = "/dbfs/put"
        str_uri = self.__databricks_api_path + str_resource
        file_name = os.path.basename(str_src_path)
        if file_name != "":
            with open(str_src_path, "rb") as fp:
                file_data = fp.read()
            str_payload = {"path": str_dbfs_path + file_name, "overwrite": "true"}
            str_files = {"filefield": (file_name, file_data)}
            obj_response = self.call_request(
                str_api_method,
                str_uri,
                dict_api_header,
                str_payload=str_payload,
                str_files=str_files,
            )
            if obj_response.status_code == 200:
                return True
            self.logger.info("Not a valid response code.")
            return False
        else:
            self.logger.info("FileName not present in source path")
            return False
    except Exception as e:
        self.__obj_db_exception.raise_generic_exception(
            message=f"Error -> DB not connected properly: {str(e)}",
            trim_log=True,
            fail_test=False,
        )