如何在谷歌脚本中定义模式而不是使用模式检测?

     2023-03-24     206

关键词:

【中文标题】如何在谷歌脚本中定义模式而不是使用模式检测?【英文标题】:How to define schema in the google script instead of using schema detect? 【发布时间】:2021-07-08 13:15:10 【问题描述】:

我一直在使用以下 google 脚本将表格从云存储上传到 bigquery。

try      
        source="gs://path/table.csv";
        ProjectId="project";
        datasetId="dataset";
        tableId="tablename";
        schema="configuration.load.autodetect":'true';
             
        var tableReference = BigQuery.newTableReference();
        tableReference.setProjectId(ProjectId);
        tableReference.setDatasetId(datasetId);
        tableReference.setTableId(tableId);

        var load = BigQuery.newJobConfigurationLoad();
        load.setDestinationTable(tableReference);
        load.setSourceUris([source]);
        load.setSourceFormat('CSV');
        load.setAutodetect(true);
        load.setMaxBadRecords(0);
        load.setWriteDisposition('WRITE_TRUNCATE');

        var configuration = BigQuery.newJobConfiguration();
        configuration.setLoad(load);

        var newJob = BigQuery.newJob();
        newJob.setConfiguration(configuration);

        var job = BigQuery.Jobs.insert(newJob, ProjectId);

    catch(err) 
        Logger.log('Table upload error: %s', err);  
     

这一直工作正常,并使用自动检测架构来自动设置表架构,但现在我想自己定义表架构。我尝试了以下代码更改,但没有成功。

source="gs://path/table.csv";
        ProjectId="project";
        datasetId="dataset";
        tableId="tablename";
        schema="configuration.load.autodetect":'false';
        
        schema=fields:[ name:'A': type:'STRING',
                         name:'B': type:'STRING',
                         name:'C': type:'INTEGER',
                         name:'D': type:'STRING',
                         name:'E': type:'STRING' ] ;

谁能告诉我我做错了什么?

【问题讨论】:

【参考方案1】:

你能试试这个架构定义吗

schema= 
        fields: [
          name: 'name', type: 'STRING',
          name: 'post_abbr', type: 'STRING',
        ],
      

更多示例here

【讨论】:

嘿,我试过这个,但它仍然没有得到正确的架构。我只是看到“string_field_0”。【参考方案2】:

你删除了吗

load.setAutodetect(true);

并添加到那里

load.setSchema(schema);

原始代码是3年前从你here on SO发布的

【讨论】:

谢谢,就是这样。我删除了“load.setAutodetect(true);”但忘记设置“load.setSchema(schema);”。谢谢你。【参考方案3】:

通常,我会尝试在 app-script 中编写作业配置,并通过函数对其进行参数化。我对我的礼物 job.insert load 做了一些修改。我希望它有效:-)

function insertRows(projectId, datasetId, tableId, schema, partField, sourceUris) 
  // Create the data upload job.
  Logger.log("Create (or) insert rows process started");
  var _sourceFormat = "CSV";
  var _createDisposition = "CREATE_IF_NEEDED";
  var _writeDisposition = "WRITE_TRUNCATE";
  
  var job = 
    configuration: 
      load: 
        sourceUris: sourceUris,
        destinationTable: 
          projectId: projectId,
          datasetId: datasetId,
          tableId: tableId
        , 
        schema: 
          fields: schema
        ,
        timePartitioning: 
          type: 'DAY',
          requirePartitionFilter: true, 
          field: partField
        ,
        sourceFormat: _sourceFormat,
        writeDisposition: _writeDisposition,
        createDisposition: _createDisposition,
        useLegacySql: false
      
    
  ;
  try 
    var bqRes = BigQuery.Jobs.insert(job, projectId);
    Logger.log(bqRes);
  
  catch(err) 
    Logger.log("Table data insertion failed");
    Logger.log(err);
  

示例:

...
var source_uris = ["gs://path/to/bucket/file"]
var schema = ["type":"STRING","name":"field_1","mode":"NULLABLE","type":"STRING","name":"field_2","mode":"NULLABLE","type":"STRING","name":"field_3","mode":"NULLABLE","type":"STRING","name":"field_4","mode":"NULLABLE","type":"DATE","name":"field_5","mode":"NULLABLE"]
...
insertRows(project_id, dataset_id, table_id, schema, 'field_5', source_uris)

【讨论】:

如何在大查询应用程序脚本中使用自动检测模式将字符串类型列加载为大查询表中的标题?

】如何在大查询应用程序脚本中使用自动检测模式将字符串类型列加载为大查询表中的标题?【英文标题】:Howtouseautodetectschemainbigqueryappscripttoloadstringtypecolumnsasheadersinbigquerytable?【发布时间】:2021-04-1616:47:48【问题描述】:我... 查看详情

如何在谷歌分析器中查找 CPU 使用率

】如何在谷歌分析器中查找CPU使用率【英文标题】:HowtofindCPUUsageingoogleprofiler【发布时间】:2011-06-0312:39:14【问题描述】:我正在使用GoogleCPUProfiling工具。http://google-perftools.googlecode.com/svn/trunk/doc/cpuprofile.html在文档中给出了它分... 查看详情

如何在谷歌应用程序脚本中汇集连接?

】如何在谷歌应用程序脚本中汇集连接?【英文标题】:Howtopoolaconnectioningoogleappsscript?【发布时间】:2013-10-1116:59:56【问题描述】:之前在GoogleApps脚本中编写脚本时,我在所有函数中打开和关闭了我的Jdbc连接。为了加快我的应... 查看详情

python我的工作涉及计算机视觉,使用pil在谷歌地图中查找屏幕截图上的模式(代码片段)

查看详情

我需要在我的谷歌驱动器中显示一个 pdf 作为缩略图,在谷歌表中..如何使用谷歌脚本?

...我的谷歌驱动器中显示一个pdf作为缩略图,在谷歌表中..如何使用谷歌脚本?【英文标题】:Ineedtodisplayapdfinmygoogledriveasathumbnail,inagooglesheet..howtodoitUsinggooglescript?【发布时间】:2020-01-2002:30:39【问题描述】:我在google驱动器中有... 查看详情

如何在 Pentaho xml 模式中使用 CalculatedMember(维度,而不是度量)?

】如何在Pentahoxml模式中使用CalculatedMember(维度,而不是度量)?【英文标题】:HowcaniuseCalculatedMember(dimension,notmeasure)inPentahoxmlschema?【发布时间】:2015-03-1020:57:15【问题描述】:我有这个MDX查询:WITHMEMBER[CLIENT].[WITHOUTCLIENTX]AS\'[CL... 查看详情

如何以编程方式检测离子应用程序中是不是开启了飞行模式

】如何以编程方式检测离子应用程序中是不是开启了飞行模式【英文标题】:HowtoProgrammaticallydetectifairplanemodeisonwithinanionicapp如何以编程方式检测离子应用程序中是否开启了飞行模式【发布时间】:2020-10-0307:26:56【问题描述】:... 查看详情

如何使用 api 在谷歌地图中仅显示一个国家或特定区域?

】如何使用api在谷歌地图中仅显示一个国家或特定区域?【英文标题】:HowtodisplayonlyonecountryoraspecificareainGooglemapsusingtheapi?【发布时间】:2010-10-1713:13:25【问题描述】:我在一个项目中使用谷歌地图,我想在我的地图窗口中只显... 查看详情

如何在谷歌地图中检测到已达到每天 25000 个请求的限制?

】如何在谷歌地图中检测到已达到每天25000个请求的限制?【英文标题】:Howtodetectthatthe25000requestperdaylimithasbeenreachedinGoogleMaps?【发布时间】:2012-01-2917:50:57【问题描述】:我正在使用GoogleMapsJavascriptAPIv3开发网站。API的免费版本... 查看详情

如何在谷歌控制台中创建新版本

】如何在谷歌控制台中创建新版本【英文标题】:Howtocreateanewreleaseingoogleconsole【发布时间】:2022-01-0915:19:18【问题描述】:在谷歌控制台生产选项卡中,我只有“编辑发布”按钮而不是“创建新发布”按钮。enterimagedescriptionhere... 查看详情

如何在 Android 应用程序中使用自己的自定义图像而不是谷歌地图

】如何在Android应用程序中使用自己的自定义图像而不是谷歌地图【英文标题】:HowtouseowncustomimageryinsteadofgooglemapinAndroidapp【发布时间】:2016-01-2916:43:34【问题描述】:我在android中开发了一个应用程序,我在其中使用GoogleMap来捕... 查看详情

猫鼬模式包含整个数据库对象而不是定义的模式对象

】猫鼬模式包含整个数据库对象而不是定义的模式对象【英文标题】:mongooseschemacontainsentireDBobjectinsteadofdefinedschemaobject【发布时间】:2020-03-1817:31:54【问题描述】:我正在为用户使用两种模式。一个包含密码/盐,一个不包含返... 查看详情

如何在没有 ScriptDB 的情况下在谷歌应用程序脚本中汇集连接?

】如何在没有ScriptDB的情况下在谷歌应用程序脚本中汇集连接?【英文标题】:HowtopoolaconnectioningoogleappsscriptwithoutScriptDB?【发布时间】:2014-06-0911:33:12【问题描述】:我希望以类似的方式asdescribedinthisquestion建立连接。但是我不想... 查看详情

BigQuery - 通过应用脚本上传带有模式自动检测的 csv

...上传中,您可以检查“自动检测”,但在脚本中我找不到如何操作。 查看详情

在谷歌浏览器中调试时如何终止脚本执行?

】在谷歌浏览器中调试时如何终止脚本执行?【英文标题】:HowtoterminatescriptexecutionwhendebugginginGoogleChrome?【发布时间】:2012-10-1912:58:15【问题描述】:在GoogleChrome调试器中单步执行JavaScript代码时,如果我不想继续,如何终止脚... 查看详情

在谷歌地图中充气自定义视图

...insidegooglemap【发布时间】:2021-09-1018:33:59【问题描述】:如何在谷歌地图片段中填充自定义视图?要在地图上添加图像/点,我们可以使用Markers或GroundOverlay,但如果我们需要自定义视图(例如,列表或带有按钮的图像),两个类... 查看详情

markdown数据库内省而不是使用语言结构定义模式(代码片段)

查看详情

如何在 Gmail 中使用标签来识别单个邮件,而不是已由脚本处理的线程

】如何在Gmail中使用标签来识别单个邮件,而不是已由脚本处理的线程【英文标题】:HowtouselabelsinGmailtoidentifysinglemessagesinsteadofthreadsthathavealreadybeenprocessedbyascript【发布时间】:2015-01-1905:52:19【问题描述】:使用谷歌邮件的谷歌... 查看详情