对象引用未设置为Google Cloud Loadobject中对象的实例

时间:2015-07-15 01:52:09

标签: bigdata google-bigquery google-cloud-storage

`loadconfig.SourceUris.Add(@"gs:\\planar-fulcrum-837\leadload-ip\01-         
 02-2013");`

空对象引用设置为对象的实例

1 个答案:

答案 0 :(得分:1)

以下是将CSV文件从云存储加载到Google Big Query的工作示例。

更新变量,例如“ServiceAccountEmail,KeyFileName,KeySecret,ProjectID,Dataset name等。

将表格架构添加到此变量

TableSchema Schema = new TableSchema();

这里我使用单个文件加载,您可以将N个CSV文件添加到此变量

System.Collections.Generic.IList<string> URIs = newSystem.Collections.Generic.List<string>();
URIs.Add(filePath);

使用以下代码修改&amp;与它合作。祝你有美好的一天。 (这个解决方案我发现工作超过3天)。

using Google.Apis.Auth.OAuth2;
using System.IO;
using System.Threading;
using Google.Apis.Bigquery.v2;
using Google.Apis.Bigquery.v2.Data;
using System.Data;
using Google.Apis.Services;
using System;
using System.Security.Cryptography.X509Certificates;

namespace GoogleBigQuery
{
    public class Class1
    {
        private static void Main()
        {
            try
            {
                String serviceAccountEmail = "SERVICE ACCOUNT EMAIL";

                var certificate = new X509Certificate2(@"KEY FILE NAME & PATH", "KEY SECRET", X509KeyStorageFlags.Exportable);

                // SYNTAX: var certificate=new X509Certificate2(KEY FILE PATH+NAME (Here it resides in Bin\Debug folder so only name is enough), SECRET KEY, X509KeyStorageFlags.Exportable);

                ServiceAccountCredential credential = new ServiceAccountCredential(
                   new ServiceAccountCredential.Initializer(serviceAccountEmail)
                   {
                       Scopes = new[] { BigqueryService.Scope.Bigquery, BigqueryService.Scope.BigqueryInsertdata, BigqueryService.Scope.CloudPlatform, BigqueryService.Scope.DevstorageFullControl }
                   }.FromCertificate(certificate));

                //  Create and initialize the Bigquery service. Use the Project Name value
                //  from the New Project window for the ApplicationName variable.

                BigqueryService Service = new BigqueryService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "APPLICATION NAME"
                });

                TableSchema Schema = new TableSchema();

                TableFieldSchema F1 = new TableFieldSchema();
                F1.Name = "COLUMN NAME";
                F1.Type = "STRING";
                F1.Mode = "REQUIRED";

                TableFieldSchema F2 = new TableFieldSchema();
                F1.Name = "COLUMN NAME";
                F1.Type = "INTEGER";
                F1.Mode = "NULLABLE";

                //Add N number of fields as per your needs

                System.Collections.Generic.IList<TableFieldSchema> FS = new System.Collections.Generic.List<TableFieldSchema>();
                FS.Add(F1);
                FS.Add(F2);

                Schema.Fields = FS;

                JobReference JR = JobUpload("PROJECT ID", "DATASET NAME", "TABLE NAME", @"gs://BUCKET NAME/FILENAME", Schema, "CREATE_IF_NEEDED", "WRITE_APPEND", '|', Service);

                //SYNTAX JobReference JR = JobUpload(PROJECT ID, DATASET NAME, TABLE NAME, FULL PATH OF CSV FILE,FILENAME IN CLOUD STORAGE, TABLE SCHEMA, CREATE DISPOSITION, DELIMITER, BIGQUERY SERVICE);

                while (true)
                {
                    var PollJob = Service.Jobs.Get(JR.ProjectId, JR.JobId).Execute();

                    Console.WriteLine("Job status" + JR.JobId + ": " + PollJob.Status.State);
                    if (PollJob.Status.State.Equals("DONE"))
                    {
                        Console.WriteLine("JOB Completed");
                        Console.ReadLine();
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error Occurred: " + e.Message);
            }

            Console.ReadLine();
        }

        public static JobReference JobUpload(string project, string dataset, string tableId, string filePath, TableSchema schema, string createDisposition, string writeDisposition, char delimiter, BigqueryService BigQueryService)
        {

            TableReference DestTable = new TableReference();
            DestTable.ProjectId = project;
            DestTable.DatasetId = dataset;
            DestTable.TableId = tableId;

            Job Job = new Job();
            JobConfiguration Config = new JobConfiguration();
            JobConfigurationLoad ConfigLoad = new JobConfigurationLoad();

            ConfigLoad.Schema = schema;
            ConfigLoad.DestinationTable = DestTable;
            ConfigLoad.Encoding = "ISO-8859-1";
            ConfigLoad.CreateDisposition = createDisposition;
            ConfigLoad.WriteDisposition = writeDisposition;
            ConfigLoad.FieldDelimiter = delimiter.ToString();
            ConfigLoad.AllowJaggedRows = true;
            ConfigLoad.SourceFormat = "CSV";
            ConfigLoad.SkipLeadingRows = 1;
            ConfigLoad.MaxBadRecords = 100000;

            System.Collections.Generic.IList<string> URIs = new System.Collections.Generic.List<string>();
            URIs.Add(filePath);

            //You can add N number of CSV Files here

            ConfigLoad.SourceUris = URIs;
            Config.Load = ConfigLoad;
            Job.Configuration = Config;

            //set job reference (mainly job id)
            JobReference JobRef = new JobReference();
            Random r = new Random();
            var JobNo = r.Next();
            JobRef.JobId = "Job" + JobNo.ToString();
            JobRef.ProjectId = project;
            Job.JobReference = JobRef;

            JobsResource.InsertRequest InsertMediaUpload = new JobsResource.InsertRequest(BigQueryService, Job, Job.JobReference.ProjectId);
            var JobInfo = InsertMediaUpload.Execute();

            return JobRef;
        }
    }
}