Class: Outhad::Integrations::Destination::DatabricksLakehouse::Client

Inherits:
DestinationConnector
  • Object
show all
Defined in:
lib/outhad/integrations/destination/databricks_lakehouse/client.rb

Constant Summary collapse

MAX_CHUNK_SIZE =
10

Instance Method Summary collapse

Instance Method Details

#check_connection(connection_config) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/outhad/integrations/destination/databricks_lakehouse/client.rb', line 10

def check_connection(connection_config)
  connection_config = connection_config.with_indifferent_access
  db = create_connection(connection_config)
  response = db.get("/api/2.0/clusters/list")
  if response.status == 200
    success_status
  else
    failure_status(nil)
  end
rescue StandardError => e
  handle_exception(e, {
                     context: "DATABRICKS:LAKEHOUSE:CHECK_CONNECTION:EXCEPTION",
                     type: "error"
                   })
  failure_status(e)
end

#discover(connection_config) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/outhad/integrations/destination/databricks_lakehouse/client.rb', line 27

def discover(connection_config)
  connection_config = connection_config.with_indifferent_access
  table_query = "SHOW TABLES IN #{connection_config[:catalog]}.#{connection_config[:schema]};"
  db = create_connection(connection_config)
  records = []
  table_response = db.post("/api/2.0/sql/statements", generate_body(connection_config[:warehouse_id], table_query).to_json)
  table_response_body = JSON.parse(table_response.body)
  table_response_body["result"]["data_array"].each do |table|
    table_name = table[1]
    query = "DESCRIBE TABLE #{connection_config[:catalog]}.#{connection_config[:schema]}.#{table_name};"
    column_response = db.post("/api/2.0/sql/statements", generate_body(connection_config[:warehouse_id], query).to_json)
    column_response_body = JSON.parse(column_response.body)
    records << [table_name, column_response_body["result"]["data_array"]]
  end
  catalog = Catalog.new(streams: create_streams(records))
  catalog.to_outhad_message
rescue StandardError => e
  handle_exception(
    "DATABRICKS:LAKEHOUSE:DISCOVER:EXCEPTION",
    "error",
    e
  )
end

#write(sync_config, records, action = "destination_insert") ⇒ Object



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
# File 'lib/outhad/integrations/destination/databricks_lakehouse/client.rb', line 51

def write(sync_config, records, action = "destination_insert")
  connection_config = sync_config.destination.connection_specification.with_indifferent_access
  table_name = "#{connection_config[:catalog]}.#{connection_config[:schema]}.#{sync_config.stream.name}"
  primary_key = sync_config.model.primary_key
  db = create_connection(connection_config)
  write_success = 0
  write_failure = 0
  log_message_array = []

  records.each do |record|
    query = Outhad::Integrations::Core::QueryBuilder.perform(action, table_name, record, primary_key)
    logger.debug("DATABRICKS:LAKEHOUSE:WRITE:QUERY query = #{query} sync_id = #{sync_config.sync_id} sync_run_id = #{sync_config.sync_run_id}")
    begin
      arg = ["/api/2.0/sql/statements", generate_body(connection_config[:warehouse_id], query)]
      response = db.post("/api/2.0/sql/statements", generate_body(connection_config[:warehouse_id], query).to_json)
      if response.status == 200
        write_success += 1
      else
        write_failure += 1
      end
      log_message_array << log_request_response("info", arg, response)
    rescue StandardError => e
      handle_exception(e, {
                         context: "DATABRICKS:LAKEHOUSE:RECORD:WRITE:EXCEPTION",
                         type: "error",
                         sync_id: sync_config.sync_id,
                         sync_run_id: sync_config.sync_run_id
                       })
      write_failure += 1
    end
  end
  tracking_message(write_success, write_failure)
rescue StandardError => e
  handle_exception(e, {
                     context: "DATABRICKS:LAKEHOUSE:RECORD:WRITE:EXCEPTION",
                     type: "error",
                     sync_id: sync_config.sync_id,
                     sync_run_id: sync_config.sync_run_id
                   })
end