dbx-api 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +14 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +65 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +12 -0
- data/lib/dbx/databricks/databricks.rb +12 -0
- data/lib/dbx/databricks/jobs.rb +141 -0
- data/lib/dbx/databricks/sql.rb +110 -0
- data/lib/dbx/gateway.rb +34 -0
- data/lib/dbx/version.rb +5 -0
- data/lib/dbx.rb +9 -0
- data/sig/dbx.rbs +4 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6f72e4b53309594553a8616e66bd27008585029c0b61d78b4e749c697c964064
|
4
|
+
data.tar.gz: 8bdd6b0e7025486de43a2951a33a0726e94374f3299f9d6e8526d70d424888c7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2ae01403a40e9688aea026bff939788e83615a017c8333e8c7b50c5e83b88c6f0b7fe0e8244186f07cb47f81757fc801d62980f6eb160e8c71059283c3b7879f
|
7
|
+
data.tar.gz: 131e3d2d072e05fcab4d456f767b87bc8871decea1abab8e11c15c92cfafe6afa5916c4637744553d3bef35eac3fb7b9395b12843bb8d6bdeeffe5e2509e9039
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at cmmiller.bio@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
dbx-api (0.1.0)
|
5
|
+
dotenv (~> 2.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ast (2.4.2)
|
11
|
+
base64 (0.1.1)
|
12
|
+
diff-lcs (1.5.0)
|
13
|
+
dotenv (2.8.1)
|
14
|
+
json (2.6.3)
|
15
|
+
language_server-protocol (3.17.0.3)
|
16
|
+
parallel (1.23.0)
|
17
|
+
parser (3.2.2.3)
|
18
|
+
ast (~> 2.4.1)
|
19
|
+
racc
|
20
|
+
racc (1.7.1)
|
21
|
+
rainbow (3.1.1)
|
22
|
+
rake (13.0.6)
|
23
|
+
regexp_parser (2.8.1)
|
24
|
+
rexml (3.2.6)
|
25
|
+
rspec (3.12.0)
|
26
|
+
rspec-core (~> 3.12.0)
|
27
|
+
rspec-expectations (~> 3.12.0)
|
28
|
+
rspec-mocks (~> 3.12.0)
|
29
|
+
rspec-core (3.12.2)
|
30
|
+
rspec-support (~> 3.12.0)
|
31
|
+
rspec-expectations (3.12.3)
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
33
|
+
rspec-support (~> 3.12.0)
|
34
|
+
rspec-mocks (3.12.6)
|
35
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
36
|
+
rspec-support (~> 3.12.0)
|
37
|
+
rspec-support (3.12.1)
|
38
|
+
rubocop (1.56.3)
|
39
|
+
base64 (~> 0.1.1)
|
40
|
+
json (~> 2.3)
|
41
|
+
language_server-protocol (>= 3.17.0)
|
42
|
+
parallel (~> 1.10)
|
43
|
+
parser (>= 3.2.2.3)
|
44
|
+
rainbow (>= 2.2.2, < 4.0)
|
45
|
+
regexp_parser (>= 1.8, < 3.0)
|
46
|
+
rexml (>= 3.2.5, < 4.0)
|
47
|
+
rubocop-ast (>= 1.28.1, < 2.0)
|
48
|
+
ruby-progressbar (~> 1.7)
|
49
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
50
|
+
rubocop-ast (1.29.0)
|
51
|
+
parser (>= 3.2.1.0)
|
52
|
+
ruby-progressbar (1.13.0)
|
53
|
+
unicode-display_width (2.4.2)
|
54
|
+
|
55
|
+
PLATFORMS
|
56
|
+
arm64-darwin-22
|
57
|
+
|
58
|
+
DEPENDENCIES
|
59
|
+
dbx-api!
|
60
|
+
rake (~> 13.0)
|
61
|
+
rspec (~> 3.0)
|
62
|
+
rubocop (~> 1.21)
|
63
|
+
|
64
|
+
BUNDLED WITH
|
65
|
+
2.4.6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 cmmille
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Databricks Gem
|
2
|
+
|
3
|
+
## Overview
|
4
|
+
This gem is designed to allow access to the DBX APIs (Jobs and SQL) from ruby applications.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
TODO: write this section
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
Set up your .env file (optional)
|
11
|
+
```bash
|
12
|
+
# .env
|
13
|
+
DBX_HOST=DBX_CONNECTION_URL
|
14
|
+
DBX_TOKEN=YOUR_TOKEN_HERE
|
15
|
+
DBX_WAREHOUSE_ID=WAREHOUSE_ID_HERE
|
16
|
+
```
|
17
|
+
|
18
|
+
running sql from a ruby script
|
19
|
+
```ruby
|
20
|
+
require 'dbx'
|
21
|
+
|
22
|
+
# If using a .env file
|
23
|
+
sql_runner = DatabricksGateway.new
|
24
|
+
# If not using .env file...
|
25
|
+
sql_runner = DatabricksGateway.new(host: 'DBX_CONNECTION_STRING', token: 'DBX_ACCESS_TOKEN', warehouse: 'DBX_SQL_WAREHOUSE_ID')
|
26
|
+
|
27
|
+
# Basic sql
|
28
|
+
result = sql_runner.run_sql("SELECT 1")
|
29
|
+
sql_runner.parse_result(result)
|
30
|
+
# => [{"1"=>"1"}]
|
31
|
+
|
32
|
+
# Dummy data in public DBX table
|
33
|
+
result = sql_runner.run_sql("SELECT * FROM samples.nyctaxi.trips LIMIT 1")
|
34
|
+
sql_runner.parse_result(result)
|
35
|
+
# => [{"tpep_pickup_datetime"=>"2016-02-14T16:52:13.000Z",
|
36
|
+
# "tpep_dropoff_datetime"=>"2016-02-14T17:16:04.000Z",
|
37
|
+
# "trip_distance"=>"4.94",
|
38
|
+
# "fare_amount"=>"19.0",
|
39
|
+
# "pickup_zip"=>"10282",
|
40
|
+
# "dropoff_zip"=>"10171"}]
|
41
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "sql"
|
4
|
+
require_relative "jobs"
|
5
|
+
|
6
|
+
# This module is a convenience module to include all Databricks modules.
|
7
|
+
module Databricks
|
8
|
+
# /api/2.0/sql/
|
9
|
+
include DatabricksSQL
|
10
|
+
# /api/2.0/jobs/
|
11
|
+
include DatabricksJobs
|
12
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
# This module handles the execution of jobs/runs using the DBX API.
|
6
|
+
# For more information about the DBX Jobs API,
|
7
|
+
# see: https://learn.microsoft.com/en-us/azure/databricks/workflows/jobs/jobs-2.0-api
|
8
|
+
module DatabricksJobs
|
9
|
+
def jobs_url
|
10
|
+
"#{@base_url}/api/2.0/jobs/"
|
11
|
+
end
|
12
|
+
|
13
|
+
# create job
|
14
|
+
# POST /api/2.0/jobs/create/
|
15
|
+
def create_job(job_json)
|
16
|
+
uri = URI("#{jobs_url}create")
|
17
|
+
request = Net::HTTP::Post.new(uri, request_headers)
|
18
|
+
request.body = job_json
|
19
|
+
response = http.request(request)
|
20
|
+
response.body
|
21
|
+
end
|
22
|
+
|
23
|
+
# list jobs
|
24
|
+
# GET /api/2.0/jobs/list/
|
25
|
+
def list_jobs
|
26
|
+
uri = URI("#{jobs_url}list")
|
27
|
+
request = Net::HTTP::Get.new(uri, request_headers)
|
28
|
+
response = http.request(request)
|
29
|
+
response.body
|
30
|
+
end
|
31
|
+
|
32
|
+
# delete job
|
33
|
+
# POST /api/2.0/jobs/delete/
|
34
|
+
def delete_job(job_id)
|
35
|
+
uri = URI("#{jobs_url}delete")
|
36
|
+
request = Net::HTTP::Post.new(uri, request_headers)
|
37
|
+
request.body = { "job_id" => job_id }.to_json
|
38
|
+
response = http.request(request)
|
39
|
+
response.body
|
40
|
+
end
|
41
|
+
|
42
|
+
# get information about a single job
|
43
|
+
# GET /api/2.0/jobs/get/
|
44
|
+
def job_info(job_id)
|
45
|
+
uri = URI("#{jobs_url}get")
|
46
|
+
request = Net::HTTP::Get.new(uri, request_headers)
|
47
|
+
request.body = { "job_id" => job_id }.to_json
|
48
|
+
response = http.request(request)
|
49
|
+
response.body
|
50
|
+
end
|
51
|
+
|
52
|
+
# run job
|
53
|
+
# POST /api/2.0/jobs/run-now
|
54
|
+
def run_now(job_id)
|
55
|
+
uri = URI("#{jobs_url}run-now")
|
56
|
+
request = Net::HTTP::Post.new(uri, request_headers)
|
57
|
+
request.body = { "job_id" => job_id }.to_json
|
58
|
+
response = http.request(request)
|
59
|
+
response.body
|
60
|
+
end
|
61
|
+
|
62
|
+
# submit a one-time run without directly creating a job
|
63
|
+
# POST /api/2.0/jobs/runs/submit
|
64
|
+
def submit_job(job_json)
|
65
|
+
uri = URI("#{jobs_url}runs/submit")
|
66
|
+
request = Net::HTTP::Post.new(uri, request_headers)
|
67
|
+
request.body = job_json
|
68
|
+
response = http.request(request)
|
69
|
+
response.body
|
70
|
+
end
|
71
|
+
|
72
|
+
# list runs
|
73
|
+
# GET /api/2.0/jobs/runs/list
|
74
|
+
def list_runs(active_only: false)
|
75
|
+
uri = URI("#{jobs_url}runs/list")
|
76
|
+
request = Net::HTTP::Get.new(uri, request_headers)
|
77
|
+
request.body = { "active_only" => active_only }.to_json
|
78
|
+
response = http.request(request)
|
79
|
+
response.body
|
80
|
+
end
|
81
|
+
|
82
|
+
# get run metadata
|
83
|
+
# GET /api/2.0/jobs/runs/get
|
84
|
+
def get_run(run_id)
|
85
|
+
uri = URI("#{jobs_url}runs/get")
|
86
|
+
request = Net::HTTP::Get.new(uri, request_headers)
|
87
|
+
request.body = { "run_id" => run_id }.to_json
|
88
|
+
response = http.request(request)
|
89
|
+
response.body
|
90
|
+
end
|
91
|
+
|
92
|
+
# export notebook run as HTML
|
93
|
+
# GET /api/2.0/jobs/runs/export
|
94
|
+
def export_run(run_id)
|
95
|
+
uri = URI("#{jobs_url}runs/export")
|
96
|
+
request = Net::HTTP::Get.new(uri, request_headers)
|
97
|
+
request.body = { "run_id" => run_id }.to_json
|
98
|
+
response = http.request(request)
|
99
|
+
response.body
|
100
|
+
end
|
101
|
+
|
102
|
+
# cancel a single run
|
103
|
+
# POST /api/2.0/jobs/runs/cancel
|
104
|
+
def cancel_run(run_id)
|
105
|
+
uri = URI("#{jobs_url}runs/cancel")
|
106
|
+
request = Net::HTTP::Post.new(uri, request_headers)
|
107
|
+
request.body = { "run_id" => run_id }.to_json
|
108
|
+
response = http.request(request)
|
109
|
+
response.body
|
110
|
+
end
|
111
|
+
|
112
|
+
# cancel all active runs of a job
|
113
|
+
# POST /api/2.0/jobs/runs/cancel-all
|
114
|
+
def cancel_all_runs(job_id)
|
115
|
+
uri = URI("#{jobs_url}runs/cancel-all")
|
116
|
+
request = Net::HTTP::Post.new(uri, request_headers)
|
117
|
+
request.body = { "job_id" => job_id }.to_json
|
118
|
+
response = http.request(request)
|
119
|
+
response.body
|
120
|
+
end
|
121
|
+
|
122
|
+
# get run output
|
123
|
+
# GET /api/2.0/jobs/runs/get-output
|
124
|
+
def get_run_output(run_id)
|
125
|
+
uri = URI("#{jobs_url}runs/get-output")
|
126
|
+
request = Net::HTTP::Get.new(uri, request_headers)
|
127
|
+
request.body = { "run_id" => run_id }.to_json
|
128
|
+
response = http.request(request)
|
129
|
+
response.body
|
130
|
+
end
|
131
|
+
|
132
|
+
# delete inactive run
|
133
|
+
# POST /api/2.0/jobs/runs/delete
|
134
|
+
def delete_run(run_id)
|
135
|
+
uri = URI("#{jobs_url}runs/delete")
|
136
|
+
request = Net::HTTP::Post.new(uri, request_headers)
|
137
|
+
request.body = { "run_id" => run_id }.to_json
|
138
|
+
response = http.request(request)
|
139
|
+
response.body
|
140
|
+
end
|
141
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
# This module handles the execution of SQL statements via the DBX API.
|
6
|
+
# For more information about the DBX SQL API, see: https://docs.databricks.com/sql/admin/sql-execution-tutorial.html
|
7
|
+
# Azure specific tutorial: https://learn.microsoft.com/en-us/azure/databricks/sql/api/sql-execution-tutorial
|
8
|
+
module DatabricksSQL
|
9
|
+
def sql_url
|
10
|
+
"#{@base_url}/api/2.0/sql/statements/"
|
11
|
+
end
|
12
|
+
|
13
|
+
def sql_uri
|
14
|
+
URI(sql_url)
|
15
|
+
end
|
16
|
+
|
17
|
+
# POST request object
|
18
|
+
# POST /api/2.0/sql/statements
|
19
|
+
def sql_request(sql)
|
20
|
+
params = {
|
21
|
+
"statement" => sql,
|
22
|
+
"wait_timeout" => "0s",
|
23
|
+
"warehouse_id" => @warehouse
|
24
|
+
}
|
25
|
+
request = Net::HTTP::Post.new(sql_uri.request_uri, request_headers)
|
26
|
+
request.body = params.to_json
|
27
|
+
request
|
28
|
+
end
|
29
|
+
|
30
|
+
# POST SQL query to DBX
|
31
|
+
def post_sql_request(sql)
|
32
|
+
response = http.request(sql_request(sql))
|
33
|
+
response.body
|
34
|
+
end
|
35
|
+
|
36
|
+
# GET request object
|
37
|
+
# GET /api/2.0/sql/statements/{statement_id}
|
38
|
+
def sql_results_request(statement_id)
|
39
|
+
req_uri = "#{sql_uri.request_uri}#{statement_id}"
|
40
|
+
Net::HTTP::Get.new(req_uri, request_headers)
|
41
|
+
end
|
42
|
+
|
43
|
+
# GET results of SQL query from DBX.
|
44
|
+
def get_sql_results(http_response)
|
45
|
+
statement_id = JSON.parse(http_response)["statement_id"]
|
46
|
+
response = http.request(sql_results_request(statement_id))
|
47
|
+
puts "#{statement_id}: #{JSON.parse(response.body)["status"]["state"]}"
|
48
|
+
response.body
|
49
|
+
end
|
50
|
+
|
51
|
+
# GET SQL chunk from DBX by internal link
|
52
|
+
def get_sql_chunk(chunk_url)
|
53
|
+
request = Net::HTTP::Get.new(chunk_url, request_headers)
|
54
|
+
response = http.request(request)
|
55
|
+
response.body
|
56
|
+
end
|
57
|
+
|
58
|
+
# Load additional chunks of data from DBX.
|
59
|
+
# DBX returns data with maximum chunk size of 16mb.
|
60
|
+
def load_additional_chunks(results_hash)
|
61
|
+
next_chunk = results_hash["result"]["next_chunk_internal_link"]
|
62
|
+
while next_chunk
|
63
|
+
response = get_sql_chunk(next_chunk)
|
64
|
+
parsed_response = JSON.parse(response)
|
65
|
+
result = parsed_response["data_array"]
|
66
|
+
data = results_hash["result"]["data_array"]
|
67
|
+
results_hash["result"]["data_array"] = [*data, *result]
|
68
|
+
next_chunk = parsed_response["next_chunk_internal_link"]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Wait for SQL query response from DBX.
|
73
|
+
# Returns a hash of the results of the SQL query.
|
74
|
+
def wait_for_sql_response(response)
|
75
|
+
result = get_sql_results(response)
|
76
|
+
status = JSON.parse(result)["status"]["state"]
|
77
|
+
# PENDING means the warehouse is starting up
|
78
|
+
# RUNNING means the query is still executing
|
79
|
+
while %w[PENDING RUNNING].include?(status)
|
80
|
+
sleep(5)
|
81
|
+
result = get_sql_results(response)
|
82
|
+
status = JSON.parse(result)["status"]["state"]
|
83
|
+
end
|
84
|
+
JSON.parse(result)
|
85
|
+
end
|
86
|
+
|
87
|
+
# Parse JSON response from DBX into array of hashes.
|
88
|
+
# Provides output c/w Big Query.
|
89
|
+
def parse_result(http_response)
|
90
|
+
keys = JSON.parse(http_response)["manifest"]["schema"]["columns"]
|
91
|
+
data_array = JSON.parse(http_response)["result"]["data_array"]
|
92
|
+
|
93
|
+
data_array.map do |row|
|
94
|
+
hash = {}
|
95
|
+
keys.each do |key|
|
96
|
+
hash[key["name"]] = row[key["position"]]
|
97
|
+
end
|
98
|
+
hash
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Submit SQL query to DBX and return results.
|
103
|
+
# returns a JSON string of the results of the SQL query
|
104
|
+
def run_sql(sql)
|
105
|
+
response = post_sql_request(sql)
|
106
|
+
results_hash = wait_for_sql_response(response)
|
107
|
+
load_additional_chunks(results_hash) if results_hash["manifest"]["total_chunk_count"] > 1
|
108
|
+
JSON.dump(results_hash)
|
109
|
+
end
|
110
|
+
end
|
data/lib/dbx/gateway.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "net/http"
|
4
|
+
require_relative "databricks/databricks"
|
5
|
+
|
6
|
+
# This class is a gateway to the Databricks API.
|
7
|
+
# https://docs.databricks.com/api-explorer/workspace/introduction
|
8
|
+
class DatabricksGateway
|
9
|
+
def initialize(host: ENV.fetch("DBX_HOST", nil), token: ENV.fetch("DBX_TOKEN", nil), warehouse: ENV.fetch("DBX_WAREHOUSE_ID", nil))
|
10
|
+
@base_url = host
|
11
|
+
@uri = URI(@base_url)
|
12
|
+
@token = token
|
13
|
+
@warehouse = warehouse
|
14
|
+
end
|
15
|
+
|
16
|
+
# HTTP request headers
|
17
|
+
def request_headers
|
18
|
+
{
|
19
|
+
"Authorization" => "Bearer #{@token}",
|
20
|
+
"Content-Type" => "application/json",
|
21
|
+
"Accept" => "application/json"
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
# HTTP connection object
|
26
|
+
def http
|
27
|
+
http = Net::HTTP.new(@uri.host, @uri.port)
|
28
|
+
http.use_ssl = true
|
29
|
+
http
|
30
|
+
end
|
31
|
+
|
32
|
+
# include DatabricksSQL, DatabricksJobs
|
33
|
+
include Databricks
|
34
|
+
end
|
data/lib/dbx/version.rb
ADDED
data/lib/dbx.rb
ADDED
data/sig/dbx.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dbx-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- cmmille
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-09-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dotenv
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
description: Ruby wrapper for the Databricks API. Includes access to the Databricks
|
28
|
+
Jobs API and the SQL Execution API.
|
29
|
+
email:
|
30
|
+
- christopher.lockfeld@providence.org
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".rspec"
|
36
|
+
- ".rubocop.yml"
|
37
|
+
- CHANGELOG.md
|
38
|
+
- CODE_OF_CONDUCT.md
|
39
|
+
- Gemfile
|
40
|
+
- Gemfile.lock
|
41
|
+
- LICENSE.txt
|
42
|
+
- README.md
|
43
|
+
- Rakefile
|
44
|
+
- lib/dbx.rb
|
45
|
+
- lib/dbx/databricks/databricks.rb
|
46
|
+
- lib/dbx/databricks/jobs.rb
|
47
|
+
- lib/dbx/databricks/sql.rb
|
48
|
+
- lib/dbx/gateway.rb
|
49
|
+
- lib/dbx/version.rb
|
50
|
+
- sig/dbx.rbs
|
51
|
+
homepage: https://github.com/pdxmolab/dbx-gem
|
52
|
+
licenses:
|
53
|
+
- MIT
|
54
|
+
metadata:
|
55
|
+
homepage_uri: https://github.com/pdxmolab/dbx-gem
|
56
|
+
source_code_uri: https://github.com/pdxmolab/dbx-gem
|
57
|
+
changelog_uri: https://github.com/pdxmolab/dbx-gem/blob/main/CHANGELOG.md
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: 2.6.0
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubygems_version: 3.4.6
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: Access the Databricks API with ruby.
|
77
|
+
test_files: []
|