def _build_resource(self): """Generate a resource for ``create`` or ``update``.""" resource = { "tableReference": { "projectId": self._dataset.project, "datasetId": self._dataset.name, "tableId": self.name, }, "schema": {"fields": _build_schema_resource(self._schema)}, } if self.description is not None: resource["description"] = self.description if self.expires is not None: value = _millis_from_datetime(self.expires) resource["expirationTime"] = value if self.friendly_name is not None: resource["friendlyName"] = self.friendly_name if self.location is not None: resource["location"] = self.location if self.view_query is not None: view = resource["view"] = {} view["query"] = self.view_query return resource
def _build_resource(self): """Generate a resource for ``create`` or ``update``.""" resource = { 'tableReference': { 'projectId': self._dataset.project, 'datasetId': self._dataset.name, 'tableId': self.name}, } if self.description is not None: resource['description'] = self.description if self.expires is not None: value = _millis_from_datetime(self.expires) resource['expirationTime'] = value if self.friendly_name is not None: resource['friendlyName'] = self.friendly_name if self.location is not None: resource['location'] = self.location if self.view_query is not None: view = resource['view'] = {} view['query'] = self.view_query elif self._schema: resource['schema'] = { 'fields': _build_schema_resource(self._schema) } else: raise ValueError("Set either 'view_query' or 'schema'.") return resource
def _build_resource(self): """Generate a resource for ``create`` or ``update``.""" resource = { 'tableReference': { 'projectId': self._dataset.project, 'datasetId': self._dataset.name, 'tableId': self.name}, 'schema': {'fields': _build_schema_resource(self._schema)}, } if self.description is not None: resource['description'] = self.description if self.expires is not None: value = _millis_from_datetime(self.expires) resource['expirationTime'] = value if self.friendly_name is not None: resource['friendlyName'] = self.friendly_name if self.location is not None: resource['location'] = self.location if self.view_query is not None: view = resource['view'] = {} view['query'] = self.view_query return resource
def expires(self, value): """Update datetime at which the table will be removed. :type value: ``datetime.datetime``, or ``NoneType`` :param value: the new expiration time, or None """ if not isinstance(value, datetime.datetime) and value is not None: raise ValueError("Pass a datetime, or None") self._properties['expirationTime'] = _millis_from_datetime(value)
def _build_resource(self): """Generate a resource for ``create`` or ``update``.""" resource = { 'tableReference': { 'projectId': self._dataset.project, 'datasetId': self._dataset.name, 'tableId': self.name }, } if self.description is not None: resource['description'] = self.description if self.expires is not None: value = _millis_from_datetime(self.expires) resource['expirationTime'] = value if self.friendly_name is not None: resource['friendlyName'] = self.friendly_name if self.location is not None: resource['location'] = self.location if self.partitioning_type is not None: resource['timePartitioning'] = self._properties['timePartitioning'] if self.view_query is not None: view = resource['view'] = {} view['query'] = self.view_query elif self._schema: resource['schema'] = { 'fields': _build_schema_resource(self._schema) } else: raise ValueError("Set either 'view_query' or 'schema'.") return resource
def patch(self, client=None, friendly_name=_MARKER, description=_MARKER, location=_MARKER, expires=_MARKER, view_query=_MARKER, schema=_MARKER): """API call: update individual table properties via a PATCH request See https://cloud.google.com/bigquery/docs/reference/v2/tables/patch :type client: :class:`gcloud.bigquery.client.Client` or ``NoneType`` :param client: the client to use. If not passed, falls back to the ``client`` stored on the current dataset. :type friendly_name: str or ``NoneType`` :param friendly_name: point in time at which the table expires. :type description: str or ``NoneType`` :param description: point in time at which the table expires. :type location: str or ``NoneType`` :param location: point in time at which the table expires. :type expires: :class:`datetime.datetime` or ``NoneType`` :param expires: point in time at which the table expires. :type view_query: str :param view_query: SQL query defining the table as a view :type schema: list of :class:`SchemaField` :param schema: fields describing the schema :raises: ValueError for invalid value types. """ client = self._require_client(client) partial = {} if expires is not _MARKER: if (not isinstance(expires, datetime.datetime) and expires is not None): raise ValueError("Pass a datetime, or None") partial['expirationTime'] = _millis_from_datetime(expires) if description is not _MARKER: partial['description'] = description if friendly_name is not _MARKER: partial['friendlyName'] = friendly_name if location is not _MARKER: partial['location'] = location if view_query is not _MARKER: if view_query is None: partial['view'] = None else: partial['view'] = {'query': view_query} if schema is not _MARKER: if schema is None: partial['schema'] = None else: partial['schema'] = { 'fields': _build_schema_resource(schema)} api_response = client.connection.api_request( method='PATCH', path=self.path, data=partial) self._set_properties(api_response)
def _callFUT(self, value): from gcloud._helpers import _millis_from_datetime return _millis_from_datetime(value)
def patch(self, client=None, friendly_name=_MARKER, description=_MARKER, location=_MARKER, expires=_MARKER, view_query=_MARKER, schema=_MARKER): """API call: update individual table properties via a PATCH request See https://cloud.google.com/bigquery/docs/reference/v2/tables/patch :type client: :class:`gcloud.bigquery.client.Client` or ``NoneType`` :param client: the client to use. If not passed, falls back to the ``client`` stored on the current dataset. :type friendly_name: string or ``NoneType`` :param friendly_name: point in time at which the table expires. :type description: string or ``NoneType`` :param description: point in time at which the table expires. :type location: string or ``NoneType`` :param location: point in time at which the table expires. :type expires: :class:`datetime.datetime` or ``NoneType`` :param expires: point in time at which the table expires. :type view_query: string :param view_query: SQL query defining the table as a view :type schema: list of :class:`SchemaField` :param schema: fields describing the schema :raises: ValueError for invalid value types. """ client = self._require_client(client) partial = {} if expires is not _MARKER: if (not isinstance(expires, datetime.datetime) and expires is not None): raise ValueError("Pass a datetime, or None") partial['expirationTime'] = _millis_from_datetime(expires) if description is not _MARKER: partial['description'] = description if friendly_name is not _MARKER: partial['friendlyName'] = friendly_name if location is not _MARKER: partial['location'] = location if view_query is not _MARKER: if view_query is None: partial['view'] = None else: partial['view'] = {'query': view_query} if schema is not _MARKER: if schema is None: partial['schema'] = None else: partial['schema'] = { 'fields': _build_schema_resource(schema)} api_response = client.connection.api_request( method='PATCH', path=self.path, data=partial) self._set_properties(api_response)
def insert_data(self, rows, row_ids=None, skip_invalid_rows=None, ignore_unknown_values=None, client=None): """API call: insert table data via a POST request See: https://cloud.google.com/bigquery/reference/rest/v2/tabledata/insertAll :type rows: list of tuples :param rows: row data to be inserted :type row_ids: list of string :param row_ids: Unique ids, one per row being inserted. If not passed, no de-duplication occurs. :type skip_invalid_rows: boolean or ``NoneType`` :param skip_invalid_rows: skip rows w/ invalid data? :type ignore_unknown_values: boolean or ``NoneType`` :param ignore_unknown_values: ignore columns beyond schema? :type client: :class:`gcloud.bigquery.client.Client` or ``NoneType`` :param client: the client to use. If not passed, falls back to the ``client`` stored on the current dataset. :rtype: list of mappings :returns: One mapping per row with insert errors: the "index" key identifies the row, and the "errors" key contains a list of the mappings describing one or more problems with the row. """ client = self._require_client(client) rows_info = [] data = {'rows': rows_info} for index, row in enumerate(rows): row_info = {} for field, value in zip(self._schema, row): if field.field_type == 'TIMESTAMP': value = _millis_from_datetime(value) row_info[field.name] = value info = {'json': row_info} if row_ids is not None: info['insertId'] = row_ids[index] rows_info.append(info) if skip_invalid_rows is not None: data['skipInvalidRows'] = skip_invalid_rows if ignore_unknown_values is not None: data['ignoreUnknownValues'] = ignore_unknown_values response = client.connection.api_request( method='POST', path='%s/insertAll' % self.path, data=data) errors = [] for error in response.get('insertErrors', ()): errors.append({'index': int(error['index']), 'errors': error['errors']}) return errors
def insert_data(self, rows, row_ids=None, skip_invalid_rows=None, ignore_unknown_values=None, client=None): """API call: insert table data via a POST request See: https://cloud.google.com/bigquery/reference/rest/v2/tabledata/insertAll :type rows: list of tuples :param rows: row data to be inserted :type row_ids: list of string :param row_ids: Unique ids, one per row being inserted. If not passed, no de-duplication occurs. :type skip_invalid_rows: boolean or ``NoneType`` :param skip_invalid_rows: skip rows w/ invalid data? :type ignore_unknown_values: boolean or ``NoneType`` :param ignore_unknown_values: ignore columns beyond schema? :type client: :class:`gcloud.bigquery.client.Client` or ``NoneType`` :param client: the client to use. If not passed, falls back to the ``client`` stored on the current dataset. :rtype: list of mappings :returns: One mapping per row with insert errors: the "index" key identifies the row, and the "errors" key contains a list of the mappings describing one or more problems with the row. """ client = self._require_client(client) rows_info = [] data = {'rows': rows_info} for index, row in enumerate(rows): row_info = {} for field, value in zip(self._schema, row): if field.field_type == 'TIMESTAMP': value = _millis_from_datetime(value) row_info[field.name] = value info = {'json': row_info} if row_ids is not None: info['insertId'] = row_ids[index] rows_info.append(info) if skip_invalid_rows is not None: data['skipInvalidRows'] = skip_invalid_rows if ignore_unknown_values is not None: data['ignoreUnknownValues'] = ignore_unknown_values response = client.connection.api_request(method='POST', path='%s/insertAll' % self.path, data=data) errors = [] for error in response.get('insertErrors', ()): errors.append({ 'index': int(error['index']), 'errors': error['errors'] }) return errors