普通网友 2018-10-26 15:48
浏览 48
已采纳

无法使golang和包bigquery正常工作以加载到大查询

I am trying to figure out how to get a simple bq load command to work with https://godoc.org/cloud.google.com/go/bigquery#Table.LoaderFrom

Running it manually it looks like this:

bq load --source_format=AVRO --ignore_unknown_values --replace=true mydataset.mytable gs://mybucket/table/*

And running it in my golang with exec.Command() successfully looks like this:

exec.Command("bq", "load", "--source_format=AVRO", "--ignore_unknown_values",
             "--replace=true", "mydataset.mytable",
             "gs://mybucket/table/*")

However, I cannot get this program to run without a segmentation fault when trying to get the load and job.wait to run successfully it seems to be getting a segmentation violation at the job.wait line of the program

package main

import (
    "context"
        "log"
        "cloud.google.com/go/bigquery"
)

func main(){
    ctx := context.Background()
    client, err := bigquery.NewClient(ctx, "my-project-id")
    if err != nil {
        // TODO: Handle error.
    }
    gcsRef := bigquery.NewGCSReference("gs://mybucket/table/*")
    gcsRef.SourceFormat = "AVRO"
    gcsRef.IgnoreUnknownValues = true
    // TODO: set other options on the GCSReference.
    ds := client.Dataset("mydataset")
    loader := ds.Table("mytable").LoaderFrom(gcsRef)
    // TODO: set other options on the Loader.
    job, err := loader.Run(ctx)
    if err != nil {
        // TODO: Handle error.
    }
    status, err := job.Wait(ctx) //seg faults right here
    if err != nil {
        // TODO: Handle error.
    }
    if status.Err() != nil {
        // TODO: Handle error.
    }
}
  • 写回答

1条回答 默认 最新

  • dongwenhui8900 2018-10-26 16:01
    关注

    The panic is probably coming from a nil pointer reference to the job variable.

    I would suggest including a log.Fatal(err)

    In all of your err!= nil blocks.

    This will help get you closer to why job is not being assigned correctly.

    When you're writing one off scripts like this one in go log.Fatal is a great way to exit the program and print exactly what the issue is.

    With go you're always trying to bubble errors up the stack to determine if the code should continue to execute, if things can be recovered, or if it's just a fatal thing and you should end the program.

    For more info on the logging package checkout here: https://golang.org/pkg/log/

    If you're just starting out learning go here are some awesome resources that can help give you ideas on how different types of programs can be designed.

    https://github.com/dashpradeep99/https-github.com-miguellgt-books/tree/master/go

    Best,

    Christopher

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动