Example #1
0
    def query(sql):
        from _internals import _Metadata
        metadata = _Metadata.load()

        request = { \
          'kind': 'bigquery#queryRequest', \
          'query': sql, \
          'defaultDataset': { 'datasetId':'github' }, \
          'maxResults': _BIGQUERY_PAGE_SIZE, \
          'timeoutMs': _BIGQUERY_TIMEOUT
        }

        url = _BIGQUERY_ENDPOINT + (_BIGQUERY_QUERY_SEGMENT % metadata.projectId())
        body = json.dumps(request)
        headers = { \
          'Content-Type': 'application/json', \
          'Content-Length': str(len(body)), \
          'Authorization': 'OAuth ' + metadata.accessToken() \
        }

        http = httplib2.Http()
        response, content = http.request(url, method = 'POST', headers = headers, body = body)

        if response.status == 200:
            result = json.loads(content)
            try:
                return _BigQueryExecutor.parseData(metadata, result)
            except:
                print result
                return None
        else:
            print str(response.status)
            print content
            # TODO: Throw error
            return None
Example #2
0
    def schema(project, dataset, table):
        from _internals import _Metadata
        metadata = _Metadata.load()

        url = _BIGQUERY_ENDPOINT + (_BIGQUERY_TABLE_SEGMENT % (project, dataset, table))
        headers = { \
          'Authorization': 'OAuth ' + metadata.accessToken() \
        }

        http = httplib2.Http()
        response, content = http.request(url, method = 'GET', headers = headers)

        if response.status == 200:
            result = json.loads(content)
            return result['schema']['fields']
        else:
            print str(response.status)
            print content
            return None
Example #3
0
    def table(name):
        m = re.match('^\[([a-z0-9\-_]+)\:([a-z0-9]+)\.([a-z0-9]+)\]$', name)
        if m is not None:
            return _BigQueryTable(name, m.groups())

        m = re.match('^\[([a-z0-9]+)\.([a-z0-9]+)\]$', name)
        if m is not None:
            from _internals import _Metadata
            metadata = _Metadata.load()

            groups = m.groups()

            parts = []
            parts.append(metadata.projectId());
            parts.append(groups[0])
            parts.append(groups[1]);
            return _BigQueryTable(name, parts);

        return None