示例#1
0
    def _LoadData(self, table_data, table_name):
        """Load data for a specified table.

    Args:
      table_data: a 2D list containing data from a CSV File. Example...
        table_data[0] = ['1', 'Some name', 'Some value'] table_data[1] = ['2',
        'Some other name', 'Some value']
      table_name: String. The name of the table to insert into.
    """
        ddl = databases.GetDdl(self._database)
        table = write_util.Table.FromDdl(ddl, table_name)
        columns = table.GetColumnTypes()

        session_name = database_sessions.Create(self._database)
        session = resources.REGISTRY.ParseRelativeName(
            relative_name=session_name.name,
            collection='spanner.projects.instances.databases.sessions')

        mutation_batch = []
        for row in table_data:
            mutation = sampledb_util.CreateInsertMutationFromCSVRow(
                table, row, columns)
            mutation_batch.append(mutation)
            if len(mutation_batch) >= self._BATCH_SIZE:
                database_sessions.Commit(session, mutation_batch)
                del mutation_batch[:]

        #  Commit any remaining mutations
        if mutation_batch:
            database_sessions.Commit(session, mutation_batch)

        database_sessions.Delete(session)
        return
示例#2
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      Some value that we want to have printed later.
    """
        database_ref = args.CONCEPTS.database.Parse()
        # DDL(Data Definition Language) is needed to get the schema of the current
        # database and table so that we know the type of each column (e.g. INT64)
        # user wants to delete.
        ddl = databases.GetDdl(database_ref)
        table = write_util.Table.FromDdl(ddl, args.table)

        mutation = database_sessions.MutationFactory.Delete(table, args.keys)

        # To commit a transaction in a session, we need to create one and delete it
        # at the end.
        session_name = database_sessions.Create(database_ref)
        session = resources.REGISTRY.ParseRelativeName(
            relative_name=session_name.name,
            collection='spanner.projects.instances.databases.sessions')
        try:
            return database_sessions.Commit(session, [mutation])
        finally:
            database_sessions.Delete(session)
示例#3
0
    def Run(self, args):
        """This is what gets called when the user runs this command."""

        database_ref = args.CONCEPTS.database.Parse()
        # DDL(Data Definition Language) is needed to get the schema of the current
        # database and table so that we know the type of each column (e.g. INT64)
        # user wants to delete.
        ddl = databases.GetDdl(database_ref)
        table = write_util.Table.FromDdl(ddl, args.table)
        data = write_util.ValidateArrayInput(table, args.data)

        mutation = database_sessions.MutationFactory.Update(table, data)

        # To commit a transaction in a session, we need to create one and delete it
        # at the end.
        session_name = database_sessions.Create(database_ref)
        session = resources.REGISTRY.ParseRelativeName(
            relative_name=session_name.name,
            collection='spanner.projects.instances.databases.sessions')
        try:
            return database_sessions.Commit(session, [mutation])
        finally:
            database_sessions.Delete(session)