Module: BigQuery::Client::Tables

Included in:
BigQuery::Client
Defined in:
lib/big_query/client/tables.rb

Constant Summary collapse

ALLOWED_FIELD_TYPES =
['STRING', 'INTEGER', 'FLOAT', 'BOOLEAN', 'RECORD', 'TIMESTAMP']
ALLOWED_FIELD_MODES =
['NULLABLE', 'REQUIRED', 'REPEATED']

Instance Method Summary collapse

Instance Method Details

#create_table(tableId, schema = {}) ⇒ Object

Creating a new table

examples:

@bq.create_table(‘new_table’, id: { type: ‘INTEGER’, mode: ‘required’ }) @bq.create_table(‘new_table’, price: { type: ‘FLOAT’ })

Parameters:

  • tableId (String)

    table id to insert into

  • schema (Hash) (defaults to: {})

    name => opts hash for the schema



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/big_query/client/tables.rb', line 72

def create_table(tableId, schema={})
  api(
    api_method: @bq.tables.insert,
    parameters: { "datasetId" => @dataset },
    body_object: { "tableReference" => {
                      "tableId" => tableId,
                      "projectId" => @project_id,
                      "datasetId" => @dataset
                    },
                    "schema" => {
                      "fields" => validate_schema(schema)
                    }
                  }
  )
end

#delete_table(tableId) ⇒ Object

Deletes the given tableId

Parameters:

  • tableId (String)

    table id to insert into



91
92
93
94
95
96
# File 'lib/big_query/client/tables.rb', line 91

def delete_table(tableId)
  api(api_method: @bq.tables.delete,
      parameters: { 'tableId' => tableId,
                    'datasetId' => @dataset }
  )
end

#describe_table(tableId, dataset = @dataset) ⇒ Hash

Describe the schema of the given tableId

Parameters:

  • tableId (String)

    table id to describe

  • dataset (String) (defaults to: @dataset)

    dataset to look for

Returns:

  • (Hash)

    json api response



103
104
105
106
107
108
109
# File 'lib/big_query/client/tables.rb', line 103

def describe_table(tableId, dataset = @dataset)
  api(
    api_method: @bq.tables.get,
    parameters: { 'tableId' => tableId,
                  'datasetId' => @dataset }
  )
end

#insert(tableId, opts) ⇒ Hash

insert row into table

Parameters:

  • tableId (String)

    table id to insert into

  • opts (Hash)

    field value hash to be inserted

Returns:

  • (Hash)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/big_query/client/tables.rb', line 48

def insert(tableId, opts)
  if opts.class == Array
    body = { 'rows' => opts = opts.map{|x| {"json" => x}} }
  else
    body = { 'rows' => [{ 'json' => opts }] }
  end

  api(
    api_method: @bq.tabledata.insert_all,
    parameters: { 'tableId' => tableId,
                  'datasetId' => @dataset },
    body_object: body
  )
end

#table_data(tableId, dataset = @dataset) ⇒ Hash

Returns all rows of table data

Parameters:

  • tableId (String)

    id of the table to look for

  • dataset (String) (defaults to: @dataset)

    dataset to look for

Returns:

  • (Hash)

    json api response



36
37
38
39
40
41
# File 'lib/big_query/client/tables.rb', line 36

def table_data(tableId, dataset = @dataset)
  response = api(api_method: @bq.tabledata.list,
                 parameters: { 'datasetId' => dataset,
                               'tableId' => tableId })
  response['rows'] || []
end

#tables(dataset = @dataset) ⇒ Hash

Lists the tables

Parameters:

  • dataset (String) (defaults to: @dataset)

    dataset to look for

Returns:

  • (Hash)

    json api response



14
15
16
17
18
19
20
21
# File 'lib/big_query/client/tables.rb', line 14

def tables(dataset = @dataset)
  response = api({
    :api_method => @bq.tables.list,
    :parameters => {"datasetId" => dataset}
  })

  response['tables'] || []
end

#tables_formatted(dataset = @dataset) ⇒ Hash

Lists the tables returnning only the tableId

Parameters:

  • dataset (String) (defaults to: @dataset)

    dataset to look for

Returns:

  • (Hash)

    json api response



27
28
29
# File 'lib/big_query/client/tables.rb', line 27

def tables_formatted(dataset = @dataset)
  tables(dataset).map { |t| t['tableReference']['tableId'] }
end