Пример #1
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.

    Raises:
      ToolException: when destination uri is not specified or invalid.
    """
    apitools_client = self.context[commands.APITOOLS_CLIENT_KEY]
    bigquery_messages = self.context[commands.BIGQUERY_MESSAGES_MODULE_KEY]
    resource_parser = self.context[commands.BIGQUERY_REGISTRY_KEY]
    project_id = properties.VALUES.core.project.Get(required=True)
    source_table_resource = resource_parser.Parse(
        args.source_table, collection='bigquery.tables')
    source_table_reference = message_conversions.TableResourceToReference(
        bigquery_messages, source_table_resource)

    if not args.destination_uri:
      raise exceptions.ToolException(
          'At least one destination URI must be specified.')
    destination_uris = args.destination_uri
    for uri in destination_uris:
      if not uri.startswith('gs://'):
        raise exceptions.ToolException(
            ('Illegal URI: {0}. Only Google Storage ("gs://") URIs are '
             'supported.').format(uri))

    job = job_control.ExecuteJob(
        apitools_client,
        bigquery_messages,
        args,
        configuration=bigquery_messages.JobConfiguration(
            extract=bigquery_messages.JobConfigurationExtract(
                sourceTable=source_table_reference,
                destinationUris=destination_uris,
                destinationFormat=bigquery_client_helper.NormalizeTextualFormat(
                    args.destination_format),
                fieldDelimiter=bigquery_client_helper.NormalizeFieldDelimiter(
                    args.field_delimiter))),
        async=args.async,
        project_id=project_id,
        job_id=job_ids.JobIdProvider().GetJobId(
            args.job_id, args.fingerprint_job_id))

    if args.async:
      job_resource = resource_parser.Create(
          'bigquery.jobs',
          projectId=job.jobReference.projectId,
          jobId=job.jobReference.jobId)
      log.CreatedResource(job_resource)
Пример #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:

    """
        apitools_client = self.context[commands.APITOOLS_CLIENT_KEY]
        bigquery_messages = self.context[commands.BIGQUERY_MESSAGES_MODULE_KEY]
        resource_parser = self.context[commands.BIGQUERY_REGISTRY_KEY]
        project_id = properties.VALUES.core.project.Get(required=True)
        table_resource = resource_parser.Parse(args.destination_table,
                                               collection='bigquery.tables')
        # TODO(user): Define constants for collection names in one place
        table_reference = message_conversions.TableResourceToReference(
            bigquery_messages, table_resource)

        sources = _ProcessSources(args.source)

        if args.schema:
            table_schema = bigquery_schemas.ReadSchema(args.schema,
                                                       bigquery_messages)
        elif args.schema_file:
            table_schema = bigquery_schemas.ReadSchemaFile(
                args.schema_file, bigquery_messages)
        else:
            table_schema = None

        normalized_source_format = bigquery_client_helper.NormalizeTextualFormat(
            args.source_format)

        if (not normalized_source_format) or normalized_source_format == 'CSV':
            normalized_quote = (
                args.quote
                and bigquery_client_helper.NormalizeFieldDelimiter(args.quote))
            normalized_skip_leading_rows = args.skip_leading_rows
        else:
            # Server accepts non-None quote and skipLeadingRows only for CSV source
            # format:
            normalized_quote = None
            normalized_skip_leading_rows = None

        load_config = bigquery_messages.JobConfigurationLoad(
            allowJaggedRows=args.allow_jagged_rows,
            allowQuotedNewlines=args.allow_quoted_newlines,
            destinationTable=table_reference,
            encoding=args.encoding and args.encoding.upper(),
            fieldDelimiter=(args.field_delimiter
                            and bigquery_client_helper.NormalizeFieldDelimiter(
                                args.field_delimiter)),
            ignoreUnknownValues=args.ignore_unknown_values,
            maxBadRecords=args.max_bad_records,
            quote=normalized_quote,
            schema=table_schema,
            skipLeadingRows=normalized_skip_leading_rows,
            sourceFormat=normalized_source_format,
            sourceUris=sources if sources[0].startswith('gs://') else [],
            writeDisposition='WRITE_TRUNCATE' if args.replace else None,
        )
        job = job_control.ExecuteJob(
            apitools_client,
            bigquery_messages,
            args,
            configuration=bigquery_messages.JobConfiguration(load=load_config),
            async=args. async,
            project_id=project_id,
            upload_file=None if sources[0].startswith('gs://') else sources[0],
            job_id=job_ids.JobIdProvider().GetJobId(args.job_id,
                                                    args.fingerprint_job_id))
        if args. async:
            job_resource = resource_parser.Create(
                'bigquery.jobs',
                projectId=job.jobReference.projectId,
                jobId=job.jobReference.jobId)
            log.CreatedResource(job_resource)