【问题标题】:Google DataProc API spark cluster with c#带有 c# 的 Google DataProc API 火花集群
【发布时间】:2016-06-08 22:27:48
【问题描述】:

我在 Big Query 中有数据,我想在 Spark 集群中运行分析。根据文档,如果我实例化一个 Spark 集群,它应该带有一个 Big Query 连接器。我正在寻找任何示例代码来执行此操作,在pyspark 中找到了一个。找不到任何 c# 示例。还在 DataProc APIs nuget 包中的函数上找到了一些 documentation

正在寻找使用 c# 在 Google 云中启动 Spark 集群的示例。

【问题讨论】:

  • 感谢您提出这个问题。在准备示例时,我意识到 nuget 上可用的自动生成的 V1 API 已过时,并且不包含执行此操作所需的方法。我将努力尽快推动这一点。更新包时我会在这里更新。
  • @AngusDavis 感谢您的回复。您知道样品和新包装准备好多长时间?
  • NuGet 包已更新。我会一起收集一个小样本。

标签: c# google-bigquery google-cloud-platform google-cloud-dataproc


【解决方案1】:

安装 Google.Apis.Dataproc.v1 版本 1.10.0.40(或更高版本)后:

下面是一个使用 C# 创建 Dataproc 集群的快速示例控制台应用:

using Google.Apis.Auth.OAuth2; 
using Google.Apis.Services;
using Google.Apis.Dataproc.v1; 
using Google.Apis.Dataproc.v1.Data;

using System; 
using System.Threading;

namespace DataprocSample {
    class Program
    {
        static void Main(string[] args)
        {
            string project = "YOUR PROJECT HERE";
            string dataprocGlobalRegion = "global";
            string zone = "us-east1-b";
            string machineType = "n1-standard-4";
            string clusterName = "sample-cluster";
            int numWorkers = 2;
            // See the docs for Application Default Credentials:
            // https://developers.google.com/identity/protocols/application-default-credentials
            // In general, a previous 'gcloud auth login' will suffice if running as yourself.
            // If running from a VM, ensure the VM was started such that the service account has
            // the CLOUD_PLATFORM scope. 
            GoogleCredential credential = GoogleCredential.GetApplicationDefaultAsync().Result;
            if (credential.IsCreateScopedRequired)
            {
                credential = credential.CreateScoped(new[] { DataprocService.Scope.CloudPlatform });
            }

            DataprocService service = new DataprocService(
                new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Dataproc Sample",
                });

            // Create a new cluster:
            Cluster newCluster = new Cluster
            {
                ClusterName = clusterName,
                Config = new ClusterConfig
                {
                    GceClusterConfig = new GceClusterConfig
                    {
                        ZoneUri = String.Format(
                            "https://www.googleapis.com/compute/v1/projects/{0}/zones/{1}",
                            project, zone),
                    },
                    MasterConfig = new InstanceGroupConfig
                    {
                        NumInstances = 1,
                        MachineTypeUri = String.Format(
                            "https://www.googleapis.com/compute/v1/projects/{0}/zones/{1}/machineTypes/{2}",
                            project, zone, machineType),
                    },
                    WorkerConfig = new InstanceGroupConfig
                    {
                        NumInstances = numWorkers,
                        MachineTypeUri = String.Format(
                            "https://www.googleapis.com/compute/v1/projects/{0}/zones/{1}/machineTypes/{2}",
                            project, zone, machineType),
                    },
                },
            };
            Operation createOperation = 
                service.Projects.Regions.Clusters.Create(newCluster, project, dataprocGlobalRegion).Execute();
            // Poll the operation:
            while (!IsDone(createOperation))
            {
                Console.WriteLine("Polling operation {0}", createOperation.Name);
                createOperation =
                    service.Projects.Regions.Operations.Get(createOperation.Name).Execute();
                Thread.Sleep(1000);
            }

            Console.WriteLine("Done creating cluster {0}", newCluster.ClusterName);
        }
        static bool IsDone(Operation op)
        {
            return op.Done ?? false;
        }
    }
 }

【讨论】:

  • 感谢您的回复,我正在尝试使用GoogleCredential.GetApplicationDefaultAsync 进行身份验证我确实下载了我们的服务密钥文件并将GOOGLE_APPLICATION_CREDENTIALS 环境变量指向它,但仍然得到异常。 System.InvalidOperationException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials.
  • 我们是否需要获得 Google Compute Engine 和 Google Cloud Dataproc 的权限才能尝试启动集群?
  • @TeslaCodes - 您需要确保启用 Dataproc 和 Compute Engine API(最简单的方法是转到 Cloud Console 中的计算引擎和 Dataproc 屏幕)。您看到的错误消息似乎表明客户端库无法获取凭据。如果您在 GCE 上运行您的应用程序,请确保 VM 有一个服务帐户,并且该服务帐户可以通过指定 CLOUD_PLATFORM OAuth 范围来访问所有云 API(这是最简单的方法)。如果在本地运行,请尝试打印出 GOOGLE_APPLICATION_CREDENTIALS 的值和 FileInfo.Exists 的值。
  • 打印了 GOOGLE_APPLICATION_CREDENTIALS 它打印了我的密钥的路径,仍然出现同样的错误。所以切换到我用于通过 GCS 进行身份验证的相同身份验证代码。但是现在在这里发布了一个不同的错误stackoverflow.com/questions/35760854/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
  • 2017-05-04
  • 1970-01-01
  • 2019-06-02
  • 2019-06-24
  • 2016-07-14
相关资源
最近更新 更多