Beispiel #1
0
    def create_table(cls, table_name):
        table_schema = bigquery.TableSchema()
        table_field = bigquery.TableFieldSchema()
        table_field.name = 'float'
        table_field.type = 'FLOAT'
        table_schema.fields.append(table_field)
        table_field = bigquery.TableFieldSchema()
        table_field.name = 'numeric'
        table_field.type = 'NUMERIC'
        table_schema.fields.append(table_field)
        table_field = bigquery.TableFieldSchema()
        table_field.name = 'bytes'
        table_field.type = 'BYTES'
        table_schema.fields.append(table_field)
        table_field = bigquery.TableFieldSchema()
        table_field.name = 'date'
        table_field.type = 'DATE'
        table_schema.fields.append(table_field)
        table_field = bigquery.TableFieldSchema()
        table_field.name = 'time'
        table_field.type = 'TIME'
        table_schema.fields.append(table_field)
        table_field = bigquery.TableFieldSchema()
        table_field.name = 'datetime'
        table_field.type = 'DATETIME'
        table_schema.fields.append(table_field)
        table_field = bigquery.TableFieldSchema()
        table_field.name = 'timestamp'
        table_field.type = 'TIMESTAMP'
        table_schema.fields.append(table_field)
        table_field = bigquery.TableFieldSchema()
        table_field.name = 'geo'
        table_field.type = 'GEOGRAPHY'
        table_schema.fields.append(table_field)
        table = bigquery.Table(tableReference=bigquery.TableReference(
            projectId=cls.project,
            datasetId=cls.dataset_id,
            tableId=table_name),
                               schema=table_schema)
        request = bigquery.BigqueryTablesInsertRequest(
            projectId=cls.project, datasetId=cls.dataset_id, table=table)
        cls.bigquery_client.client.tables.Insert(request)
        row_data = {
            'float': 0.33,
            'numeric': Decimal('10'),
            'bytes': base64.b64encode(b'\xab\xac').decode('utf-8'),
            'date': '3000-12-31',
            'time': '23:59:59',
            'datetime': '2018-12-31T12:44:31',
            'timestamp': '2018-12-31 12:44:31.744957 UTC',
            'geo': 'POINT(30 10)'
        }

        table_data = [row_data]
        # add rows with only one key value pair and None values for all other keys
        for key, value in iteritems(row_data):
            table_data.append({key: value})

        cls.bigquery_client.insert_rows(cls.project, cls.dataset_id,
                                        table_name, table_data)
Beispiel #2
0
 def create_table(self, table_name):
     table_schema = bigquery.TableSchema()
     table_field = bigquery.TableFieldSchema()
     table_field.name = 'int64'
     table_field.type = 'INT64'
     table_field.mode = 'REQUIRED'
     table_schema.fields.append(table_field)
     table_field = bigquery.TableFieldSchema()
     table_field.name = 'bytes'
     table_field.type = 'BYTES'
     table_schema.fields.append(table_field)
     table_field = bigquery.TableFieldSchema()
     table_field.name = 'date'
     table_field.type = 'DATE'
     table_schema.fields.append(table_field)
     table_field = bigquery.TableFieldSchema()
     table_field.name = 'time'
     table_field.type = 'TIME'
     table_schema.fields.append(table_field)
     table = bigquery.Table(tableReference=bigquery.TableReference(
         projectId=self.project,
         datasetId=self.dataset_id,
         tableId=table_name),
                            schema=table_schema)
     request = bigquery.BigqueryTablesInsertRequest(
         projectId=self.project, datasetId=self.dataset_id, table=table)
     self.bigquery_client.client.tables.Insert(request)
Beispiel #3
0
 def create_table(self, tablename):
     table_schema = bigquery.TableSchema()
     table_field = bigquery.TableFieldSchema()
     table_field.name = 'number'
     table_field.type = 'INTEGER'
     table_schema.fields.append(table_field)
     table_field = bigquery.TableFieldSchema()
     table_field.name = 'str'
     table_field.type = 'STRING'
     table_schema.fields.append(table_field)
     table = bigquery.Table(tableReference=bigquery.TableReference(
         projectId=self.project,
         datasetId=self.dataset_id,
         tableId=tablename),
                            schema=table_schema)
     request = bigquery.BigqueryTablesInsertRequest(
         projectId=self.project, datasetId=self.dataset_id, table=table)
     self.bigquery_client.client.tables.Insert(request)
     table_data = [{
         'number': 1,
         'str': 'abc'
     }, {
         'number': 2,
         'str': 'def'
     }, {
         'number': 3,
         'str': u'你好'
     }, {
         'number': 4,
         'str': u'привет'
     }]
     self.bigquery_client.insert_rows(self.project, self.dataset_id,
                                      tablename, table_data)
 def _setup_new_types_env(self):
   table_schema = bigquery.TableSchema()
   table_field = bigquery.TableFieldSchema()
   table_field.name = 'bytes'
   table_field.type = 'BYTES'
   table_schema.fields.append(table_field)
   table_field = bigquery.TableFieldSchema()
   table_field.name = 'date'
   table_field.type = 'DATE'
   table_schema.fields.append(table_field)
   table_field = bigquery.TableFieldSchema()
   table_field.name = 'time'
   table_field.type = 'TIME'
   table_schema.fields.append(table_field)
   table = bigquery.Table(
       tableReference=bigquery.TableReference(
           projectId=self.project,
           datasetId=self.dataset_id,
           tableId=NEW_TYPES_INPUT_TABLE),
       schema=table_schema)
   request = bigquery.BigqueryTablesInsertRequest(
       projectId=self.project, datasetId=self.dataset_id, table=table)
   self.bigquery_client.client.tables.Insert(request)
   table_data = [
       {'bytes':b'xyw=', 'date':'2011-01-01', 'time':'23:59:59.999999'},
       {'bytes':b'abc=', 'date':'2000-01-01', 'time':'00:00:00'},
       {'bytes':b'dec=', 'date':'3000-12-31', 'time':'23:59:59.990000'}
   ]
   self.bigquery_client.insert_rows(
       self.project, self.dataset_id, NEW_TYPES_INPUT_TABLE, table_data)
Beispiel #5
0
 def create_table_new_types(self, table_name):
   table_schema = bigquery.TableSchema()
   table_field = bigquery.TableFieldSchema()
   table_field.name = 'bytes'
   table_field.type = 'BYTES'
   table_schema.fields.append(table_field)
   table_field = bigquery.TableFieldSchema()
   table_field.name = 'date'
   table_field.type = 'DATE'
   table_schema.fields.append(table_field)
   table_field = bigquery.TableFieldSchema()
   table_field.name = 'time'
   table_field.type = 'TIME'
   table_schema.fields.append(table_field)
   table = bigquery.Table(
       tableReference=bigquery.TableReference(
           projectId=self.project,
           datasetId=self.dataset_id,
           tableId=table_name),
       schema=table_schema)
   request = bigquery.BigqueryTablesInsertRequest(
       projectId=self.project, datasetId=self.dataset_id, table=table)
   self.bigquery_client.client.tables.Insert(request)
   table_data = [
       {'bytes': b'xyw', 'date': '2011-01-01', 'time': '23:59:59.999999'},
       {'bytes': b'abc', 'date': '2000-01-01', 'time': '00:00:00'},
       {'bytes': b'\xe4\xbd\xa0\xe5\xa5\xbd', 'date': '3000-12-31',
        'time': '23:59:59'},
       {'bytes': b'\xab\xac\xad', 'date': '2000-01-01', 'time': '00:00:00'}
   ]
   # bigquery client expects base64 encoded bytes
   for row in table_data:
     row['bytes'] = base64.b64encode(row['bytes']).decode('utf-8')
   self.bigquery_client.insert_rows(
       self.project, self.dataset_id, table_name, table_data)
Beispiel #6
0
 def _create_table(self, project_id, dataset_id, table_id, schema):
   table = bigquery.Table(
       tableReference=bigquery.TableReference(
           projectId=project_id, datasetId=dataset_id, tableId=table_id),
       schema=schema)
   request = bigquery.BigqueryTablesInsertRequest(
       projectId=project_id, datasetId=dataset_id, table=table)
   response = self.client.tables.Insert(request)
   # The response is a bigquery.Table instance.
   return response
Beispiel #7
0
 def create_table(cls, table_name, data, table_schema):
     table = bigquery.Table(tableReference=bigquery.TableReference(
         projectId=cls.project,
         datasetId=cls.dataset_id,
         tableId=table_name),
                            schema=table_schema)
     request = bigquery.BigqueryTablesInsertRequest(
         projectId=cls.project, datasetId=cls.dataset_id, table=table)
     cls.bigquery_client.client.tables.Insert(request)
     cls.bigquery_client.insert_rows(cls.project, cls.dataset_id,
                                     table_name, data)
     return table_schema
Beispiel #8
0
    def _create_table(cls, table_name):
        table_schema = bigquery.TableSchema()

        number = bigquery.TableFieldSchema()
        number.name = 'number'
        number.type = 'INTEGER'
        table_schema.fields.append(number)

        string = bigquery.TableFieldSchema()
        string.name = 'string'
        string.type = 'STRING'
        table_schema.fields.append(string)

        time = bigquery.TableFieldSchema()
        time.name = 'time'
        time.type = 'TIME'
        table_schema.fields.append(time)

        datetime = bigquery.TableFieldSchema()
        datetime.name = 'datetime'
        datetime.type = 'DATETIME'
        table_schema.fields.append(datetime)

        rec = bigquery.TableFieldSchema()
        rec.name = 'rec'
        rec.type = 'RECORD'
        rec_datetime = bigquery.TableFieldSchema()
        rec_datetime.name = 'rec_datetime'
        rec_datetime.type = 'DATETIME'
        rec.fields.append(rec_datetime)
        rec_rec = bigquery.TableFieldSchema()
        rec_rec.name = 'rec_rec'
        rec_rec.type = 'RECORD'
        rec_rec_datetime = bigquery.TableFieldSchema()
        rec_rec_datetime.name = 'rec_rec_datetime'
        rec_rec_datetime.type = 'DATETIME'
        rec_rec.fields.append(rec_rec_datetime)
        rec.fields.append(rec_rec)
        table_schema.fields.append(rec)

        table = bigquery.Table(tableReference=bigquery.TableReference(
            projectId=cls.project,
            datasetId=cls.dataset_id,
            tableId=table_name),
                               schema=table_schema)
        request = bigquery.BigqueryTablesInsertRequest(
            projectId=cls.project, datasetId=cls.dataset_id, table=table)
        cls.bigquery_client.client.tables.Insert(request)
        cls.bigquery_client.insert_rows(cls.project, cls.dataset_id,
                                        table_name, cls.TABLE_DATA)
Beispiel #9
0
 def _setup_new_types_env(self):
     table_schema = bigquery.TableSchema()
     table_field = bigquery.TableFieldSchema()
     table_field.name = 'bytes'
     table_field.type = 'BYTES'
     table_schema.fields.append(table_field)
     table_field = bigquery.TableFieldSchema()
     table_field.name = 'date'
     table_field.type = 'DATE'
     table_schema.fields.append(table_field)
     table_field = bigquery.TableFieldSchema()
     table_field.name = 'time'
     table_field.type = 'TIME'
     table_schema.fields.append(table_field)
     table = bigquery.Table(tableReference=bigquery.TableReference(
         projectId=self.project,
         datasetId=self.dataset_id,
         tableId=NEW_TYPES_INPUT_TABLE),
                            schema=table_schema)
     request = bigquery.BigqueryTablesInsertRequest(
         projectId=self.project, datasetId=self.dataset_id, table=table)
     self.bigquery_client.client.tables.Insert(request)
     table_data = [{
         'bytes': b'xyw',
         'date': '2011-01-01',
         'time': '23:59:59.999999'
     }, {
         'bytes': b'abc',
         'date': '2000-01-01',
         'time': '00:00:00'
     }, {
         'bytes': b'\xe4\xbd\xa0\xe5\xa5\xbd',
         'date': '3000-12-31',
         'time': '23:59:59.990000'
     }, {
         'bytes': b'\xab\xac\xad',
         'date': '2000-01-01',
         'time': '00:00:00'
     }]
     # the API Tools bigquery client expects byte values to be base-64 encoded
     # TODO https://github.com/apache/beam/issues/19073: upgrade to
     # google-cloud-bigquery which does not require handling the encoding in
     # beam
     for row in table_data:
         row['bytes'] = base64.b64encode(row['bytes']).decode('utf-8')
     passed, errors = self.bigquery_client.insert_rows(
         self.project, self.dataset_id, NEW_TYPES_INPUT_TABLE, table_data)
     self.assertTrue(passed, 'Error in BQ setup: %s' % errors)
Beispiel #10
0
 def _create_table(self,
                   project_id,
                   dataset_id,
                   table_id,
                   schema,
                   additional_parameters=None):
     additional_parameters = additional_parameters or {}
     table = bigquery.Table(tableReference=bigquery.TableReference(
         projectId=project_id, datasetId=dataset_id, tableId=table_id),
                            schema=schema,
                            **additional_parameters)
     request = bigquery.BigqueryTablesInsertRequest(projectId=project_id,
                                                    datasetId=dataset_id,
                                                    table=table)
     response = self.client.tables.Insert(request)
     logging.debug("Created the table with id %s", table_id)
     # The response is a bigquery.Table instance.
     return response
Beispiel #11
0
 def create_table(cls, table_name):
     table_schema = bigquery.TableSchema()
     table_field = bigquery.TableFieldSchema()
     table_field.name = 'number'
     table_field.type = 'INTEGER'
     table_schema.fields.append(table_field)
     table_field = bigquery.TableFieldSchema()
     table_field.name = 'str'
     table_field.type = 'STRING'
     table_schema.fields.append(table_field)
     table = bigquery.Table(tableReference=bigquery.TableReference(
         projectId=cls.project,
         datasetId=cls.dataset_id,
         tableId=table_name),
                            schema=table_schema)
     request = bigquery.BigqueryTablesInsertRequest(
         projectId=cls.project, datasetId=cls.dataset_id, table=table)
     cls.bigquery_client.client.tables.Insert(request)
     cls.bigquery_client.insert_rows(cls.project, cls.dataset_id,
                                     table_name, cls.TABLE_DATA)