Skip to content

Instantly share code, notes, and snippets.

@EricLondon
Created October 31, 2017 15:20
Show Gist options
  • Save EricLondon/1a8e81289af8b00c8d86fd5b207a3aeb to your computer and use it in GitHub Desktop.
Save EricLondon/1a8e81289af8b00c8d86fd5b207a3aeb to your computer and use it in GitHub Desktop.
EMR/Zeppelin set interpreter and run all notebooks
#!/usr/bin/env ruby
require 'json'
require 'net/http'
require 'open-uri'
require 'pp'
require 'uri'
ZEP_HOST = 'localhost'
ZEP_PORT = 8890
ZEP_ADDR = "http://#{ZEP_HOST}:#{ZEP_PORT}"
# list notebooks
notebooks = JSON.parse(open("#{ZEP_ADDR}/api/notebook").read)['body']
# get interpreter ids
interpreter_ids = JSON.parse(open("#{ZEP_ADDR}/api/interpreter/setting").read)['body'].map {|i| i['id']}
notebooks.each do |notebook|
next if notebook['name'] =~ /^(test|sandbox|scratch|quick)/
next if notebook['name'] !~ /prestaging\.(transform|compute)\./
puts "--------------------------------------------------"
pp notebook
# set interpreter
uri = URI("#{ZEP_ADDR}/api/notebook/interpreter/bind/#{notebook['id']}")
request = Net::HTTP::Put.new(uri, 'Content-Type' => 'application/json')
request.body = interpreter_ids.to_json
response = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(request) }
# start notebook
uri = URI("#{ZEP_ADDR}/api/notebook/job/#{notebook['id']}")
req = Net::HTTP::Post.new(uri)
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
pp res
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment