Class: RbBigQuery::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/rbbigquery/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, dataset, table_id, schema) ⇒ Table

Returns a new instance of Table.



5
6
7
8
9
10
11
12
# File 'lib/rbbigquery/table.rb', line 5

def initialize(client, dataset, table_id, schema)
  @client = client
  @dataset = dataset
  @table_id = table_id
  @schema = schema

  create
end

Instance Attribute Details

#schemaObject

Returns the value of attribute schema.



3
4
5
# File 'lib/rbbigquery/table.rb', line 3

def schema
  @schema
end

Instance Method Details

#createHash

Returns row response json.

Returns:

  • (Hash)

    row response json



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rbbigquery/table.rb', line 20

def create
  response = @client.client.execute({
     :api_method => @client.bq.tables.insert,
     :parameters => {
         'projectId' => @client.project_id,
         'datasetId' => @dataset
     },
     :body_object => {
         'tableReference' => {
             'projectId' => @client.project_id,
             'datasetId' => @dataset,
             'tableId'   => @table_id
         },
         'schema' => {
             'fields' => @schema
         }
     }
 })

  JSON.parse(response.body)
end

#insert(rows) ⇒ Hash

insert rows

Parameters:

  • rows (Array<Hash>)
    #{column_name=>value}

Returns:

  • (Hash)

    row response json



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rbbigquery/table.rb', line 45

def insert(rows)
  rows = rows.map { |row| {'json' => row} }

  response = @client.client.execute({
     :api_method => @client.bq.tabledata.insert_all,
     :parameters => {
         'projectId' => @client.project_id,
         'datasetId' => @dataset,
         'tableId' => @table_id,
     },
     :body_object => {
         "rows" => rows
     }
  })

  JSON.parse(response.body)
end

#sql_nameString

Returns GQL style table name. (dataset.table_id).

Returns:

  • (String)

    GQL style table name. (dataset.table_id)



15
16
17
# File 'lib/rbbigquery/table.rb', line 15

def sql_name
  "#{@dataset}.#{@table_id}"
end