question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sqladmin v1beta4 - sql.instances.export - exports NULLs improperly to CSV

See original GitHub issue

As the title states, when using the sqladmin v1beta4 sql.instances.export functionality, upon export, records containing NULL values have their NULLs represented as "N by default – which plays hell on every CSV parser & BigQuery.

This was done using the googleapis v18.0.0 module.

Example Output:

# I have replaced all not-null values with categorical representations
# The, "N, values are the NULLs
"2017-03-27 18:37:54","N,"city","country","N,"N,lat,long,int,"message","ja-jp","Message","IE","Windows","11","URL","URL","URL","URL","HASH"

Here is my code:

'use strict';

(function() {

  var google = require('googleapis');
  var sqladmin = google.sqladmin('v1beta4');

  module.exports = function exportCloudSqlToGCS(config) {

    google.auth.getApplicationDefault(function(err, authClient) {
      if (err) {
        console.log('Authentication failed because of ', err);
        return;
      }

      if (authClient.createScopedRequired && authClient.createScopedRequired()) {
        var scopes = [
          // Needed for ability to import / export.
          'https://www.googleapis.com/auth/cloud-platform',
          // Needed to login to the MySQL database successfully.
          'https://www.googleapis.com/auth/sqlservice.admin'
        ];
        authClient = authClient.createScoped(scopes);
      }

      var request = {
        // Project ID of the project that contains the instance to be exported.
        project: config.gcloudProject, // 'my-project-name'

        // Cloud SQL instance ID. This does not include the project ID.
        instance: config.gcloudInstance, // 'my-cloud-sql-db-name'

        resource: {
          // Contains details about the export operation.
          exportContext: {
            // This is always sql#exportContext.
            kind: "sql#exportContext",
            // The file type for the specified uri (e.g. SQL or CSV)
            fileType: config.exportFileType, // CSV
            /** 
             * The path to the file in GCS where the export will be stored.
             * The URI is in the form gs://bucketName/fileName.
             * If the file already exists, the operation fails.
             * If fileType is SQL and the filename ends with .gz, the contents are compressed.
             */
            uri: config.gcsBucketFilePath,
            /**
             * Databases from which the export is made.
             * If fileType is SQL and no database is specified, all databases are exported.
             * If fileType is CSV, you can optionally specify at most one database to export.
             * If csvExportOptions.selectQuery also specifies the database, this field will be ignored.
             */
            databases: config.databases, // ['myDatabase']
            // Options for exporting data as SQL statements.
            // sqlExportOptions: {
            //   /**
            //    * Tables to export, or that were exported, from the specified database.
            //    * If you specify tables, specify one and only one database. 
            //    */
            //   tables: config.tables,
            //   // Export only schemas?
            //   schemaOnly: config.schemaOnly
            // },
            csvExportOptions: {
              // The select query used to extract the data.
              selectQuery: config.selectQuery // "SELECT * from myTable"
            }
          }
        },
        // Auth client
        auth: authClient
      };

      // Kick off export with requested arguments.
      sqladmin.instances.export(request, function(err, result) {
        if (err) {
          console.log(err);
        } else {
          console.log(result);
        }
      });

    });

  };
}).call(this);

I believe the proper NULL representation should be “\N” or at least an empty string, or simply, no value at all: value,value,value,value,value,value

This was likely a simple typo – but it is a pain. I’ve worked around it by wrapping every field in a simple IFNULL( fieldName, ‘’) as fieldName – but that’s rather tedious to maintain.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
FreeZey78commented, Nov 1, 2017

Any status update or timescales for this bug fix?

Allowing the “Escaped By” value to be configurable would be the best solution but in the short term could Poma’s suggestion of changing the escape character to \ be implemented? It would significant improve the compatible of exported files with other systems like BigQuery.

4reactions
lukesneeringercommented, Apr 12, 2017

Hi @bllevy, Thanks for reporting this. That does sound annoying. We will look into it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Method: instances.export | Cloud SQL for MySQL
Exports data from a Cloud SQL instance to a Cloud Storage bucket as a SQL dump or CSV file.
Read more >
Understanding the behavior of gcloud sql export sql and ...
To achieve this, I decided to export the database of my instance, create a second one and import the exported backup of the...
Read more >
CloudSQL import and export via CSV is implemented incorrectly
We're exporting from Cloud SQL to load into BigQuery. Our current work around is to replace all nulls with a string which acts...
Read more >
sql.instances.export - Cloud SQL Admin - Any API
Exports data from a Cloud SQL instance to a Cloud Storage bucket as a SQL dump or CSV file.
Read more >
ExportContext (Cloud SQL Admin API v1beta4 (Rev. 76) 1.25.0)
Database instance export context. ... Options for exporting data as SQL statements. ... Parameters: csvExportOptions - csvExportOptions or null for none ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found