雅典娜.NET集成

时间:2018-08-01 19:02:53

标签: .net amazon-web-services amazon-athena

我正在开始一个项目,以将现有的VB.net Webform应用程序集成到存储在AWS上的新数据。我已经下载了一个Athena SDK,暗示它可以完全做到这一点,但我无法使其正常工作。这是我到目前为止的模块:

Imports System
Imports System.Threading
Imports System.Threading.Tasks
Imports System.Collections.Generic
Imports Amazon
Imports Amazon.Athena
Imports Amazon.Athena.Model

Namespace athena_api
Class AthenaQuery

    Private Const ATHENA_TEMP_PATH As String = "s3://xxxxxxxxxxx"
    Private Const ATHENA_DB As String = "xxxxx"
    Public Sub New()

        Using client = New AmazonAthenaClient(Amazon.RegionEndpoint.USEast1)
            Dim qContext As QueryExecutionContext = New QueryExecutionContext()
            qContext.Database = ATHENA_DB
            Dim resConf As ResultConfiguration = New ResultConfiguration()
            resConf.OutputLocation = ATHENA_TEMP_PATH
            Console.WriteLine("Created Athena Client")
            run(client, qContext, resConf).Wait()
        End Using

    End Sub

    Public Shared Async Function run(ByVal client As IAmazonAthena, ByVal qContext As QueryExecutionContext, ByVal resConf As ResultConfiguration) As Task
        Dim qReq As StartQueryExecutionRequest = New StartQueryExecutionRequest() With {
            .QueryString = "select count(*) from testTable",
            .QueryExecutionContext = qContext,
            .ResultConfiguration = resConf
        }

        Try
            Dim qRes As StartQueryExecutionResponse = Await client.StartQueryExecutionAsync(qReq)
            Dim items As List(Of Dictionary(Of String, String)) = Await getQueryExecution(client, qRes.QueryExecutionId)

            For Each item In items

                For Each pair As KeyValuePair(Of String, String) In item
                    Console.WriteLine("Col: {0}", pair.Key)
                    Console.WriteLine("Val: {0}", pair.Value)
                Next
            Next

        Catch e As InvalidRequestException
            Console.WriteLine("Run Error: {0}", e.Message)
        End Try


    End Function

    Public Shared Async Function getQueryExecution(ByVal client As IAmazonAthena, ByVal id As String) As Task(Of List(Of Dictionary(Of String, String)))
        Dim items As List(Of Dictionary(Of String, String)) = New List(Of Dictionary(Of String, String))()
        Dim results As GetQueryExecutionResponse = Nothing
        Dim q As QueryExecution = Nothing
        Dim qReq As GetQueryExecutionRequest = New GetQueryExecutionRequest() With {
            .QueryExecutionId = id
        }

        Do

            Try
                results = Await client.GetQueryExecutionAsync(qReq)
                q = results.QueryExecution
                Console.WriteLine("Status: {0}... {1}", q.Status.State, q.Status.StateChangeReason)
                Await Task.Delay(5000)
            Catch e As InvalidRequestException
                Console.WriteLine("GetQueryExec Error: {0}", e.Message)
            End Try
        Loop While q.Status.State = "RUNNING" OrElse q.Status.State = "QUEUED"

        Console.WriteLine("Data Scanned for {0}: {1} Bytes", id, q.Statistics.DataScannedInBytes)
        Dim resReq As GetQueryResultsRequest = New GetQueryResultsRequest() With {
            .QueryExecutionId = id,
            .MaxResults = 10
        }
        Dim resResp As GetQueryResultsResponse = Nothing

        Do
            resResp = Await client.GetQueryResultsAsync(resReq)

            For Each row As Row In resResp.ResultSet.Rows
                Dim dict As Dictionary(Of String, String) = New Dictionary(Of String, String)()

                For i = 0 To resResp.ResultSet.ResultSetMetadata.ColumnInfo.Count - 1
                    dict.Add(resResp.ResultSet.ResultSetMetadata.ColumnInfo(i).Name, row.Data(i).VarCharValue)
                Next

                items.Add(dict)
            Next

            If resResp.NextToken IsNot Nothing Then
                resReq.NextToken = resResp.NextToken
            End If
        Loop While resResp.NextToken IsNot Nothing

        Return items
    End Function
End Class

End Namespace

运行时出现以下错误:

mscorlib.dll中发生了'System.AggregateException'类型的未处理异常

我还不清楚此api如何对AWS进行身份验证...没有请求任何秘密密钥或令牌,因此似乎拥有S3路径和数据库名称的任何人都可以访问。

谢谢大家,这是我使用AWS的项目的第一步,请耐心等待!

1 个答案:

答案 0 :(得分:0)

在身份验证文件上...对于Visual Studio ...安装适用于Visual Studio的AWS工具包...,其中将包括AWS Explorer。您可以从那里创建和管理配置文件-然后在AppSettings中引用配置文件名称...类似

<appSettings>
    <add key="AWSProfileName" value="YOUR-PROFILE-NAME"/>
    <add key="AWSRegion" value="us-east-1" />
</appSettings>

一个好的起点:https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/welcome.html

关于错误... how to fix "'System.AggregateException' occurred in mscorlib.dll"

在Athena上... c#,但我认为应该足够接近:https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Athena/NAthena.html