gpt4 book ai didi

c# - 使用 Azure Functions 调用 REST API 并将结果保存在 Azure Data Lake gen2 中

转载 作者:太空宇宙 更新时间:2023-11-03 22:35:05 25 4
gpt4 key购买 nike

我想调用 REST API 并将结果保存为 Azure Data Lake Gen2 中的 csv 或 json 文件。根据我所读到的内容,Azure Functions 是可行的方法。

网络服务返回如下格式的数据:

"ID","ProductName","Company"
"1","Apples","Alfreds futterkiste"
"2","Oranges","Alfreds futterkiste"
"3","Bananas","Alfreds futterkiste"
"4","Salad","Alfreds futterkiste"
...next rows

我用 C# 编写了一个控制台应用程序,目前将数据输出到控制台。 Web 服务使用分页并返回 1000 行(由 &num 参数确定,最大值为 1000)。在第一个请求之后,我可以使用 &next-parameter 根据 ID 获取接下来的 1000 行。例如网址

http://testWebservice123.com/Example.csv?auth=abc&number=1000&next=1000

将获取 ID 1001 到 2000 之间的行。(实际上,API 的调用和分页有点复杂,因此我无法使用例如 Azure Data Factory_v2 来加载到 Azure Data Lake - 这就是我认为我需要 Azure Functions 的原因 - 除非我忽略了另一个service??。所以下面只是一个演示,学习如何写入Azure Data Lake。)

我有以下 C#:

static void Main(string[] args)
{


string startUrl = "http://testWebservice123.com/Example.csv?auth=abc&number=1000";
string url = "";
string deltaRequestParameter = "";
string lastLine;
int numberOfLines = 0;

do
{
url = startUrl + deltaRequestParameter;
WebClient myWebClient = new WebClient();

using (Stream myStream = myWebClient.OpenRead(url))
{

using (StreamReader sr = new StreamReader(myStream))
{
numberOfLines = 0;
while (!sr.EndOfStream)
{
var row = sr.ReadLine();
var values = row.Split(',');

//do whatever with the rows by now - i.e. write to console
Console.WriteLine(values[0] + " " + values[1]);

lastLine = values[0].Replace("\"", ""); //last line in the loop - get the last ID.
numberOfLines++;
deltaRequestParameter = "&next=" + lastLine;
}

}

}
} while (numberOfLines == 1001); //since the header is returned each time the number of rows will be 1001 until we get to the last request


}

我想以最有效的方式将数据写入 csv 文件到数据湖。如何重写上述代码以在 Azure Function 中工作并保存到 Azure 数据湖 gen2 中的 csv?

最佳答案

以下是实现结果所需执行的步骤:

1)创建一个azure函数和触发器,您可以将其保留为HTTPTrigger/TimerTrigger,或者根据您的需要。

2) 我假设您有循环调用 api 的代码,直到它给出您想要的结果。

3)一旦内存中有数据,您必须编写以下代码将其写入Azure数据湖。

使用 C# 代码访问 ADLS 的先决条件:

1) 在 Azure AD 中注册应用

enter image description here

enter image description here

在数据湖存储中授予权限

enter image description here

enter image description here

enter image description here

enter image description here

下面是创建ADLS客户端的代码。

// ADLS connection 
var adlCreds = GetCreds_SPI_SecretKey(tenantId, ADL_TOKEN_AUDIENCE, serviceAppIDADLS, servicePrincipalSecretADLS);
var adlsClient = AdlsClient.CreateClient(adlsName, adlCreds);



private static ServiceClientCredentials GetCreds_SPI_SecretKey(string tenant,Uri tokenAudience,string clientId,string secretKey)
{
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
var serviceSettings = ActiveDirectoryServiceSettings.Azure;
serviceSettings.TokenAudience = tokenAudience;
var creds = ApplicationTokenProvider.LoginSilentAsync(tenant,clientId,secretKey,serviceSettings).GetAwaiter().GetResult();
return creds;
}

最后编写将文件保存在Azure数据湖中的实现

 const string delim = ",";
static string adlsInputPath = ConfigurationManager.AppSettings.Get("AdlsInputPath");

public static void ProcessUserProfile(this SampleProfile, AdlsClient adlsClient, string fileNameExtension = "")
{
using (MemoryStream memStreamProfile = new MemoryStream())
{
using (TextWriter textWriter = new StreamWriter(memStreamProfile))
{
string profile;
string header = Helper.GetHeader(delim, Entities.FBEnitities.Profile);
string fileName = adlsInputPath + fileNameExtension + "/profile.csv";
adlsClient.DataLakeFileHandler(textWriter, header, fileName);
profile = socialProfile.UserID
+ delim + socialProfile.Profile.First_Name
+ delim + socialProfile.Profile.Last_Name
+ delim + socialProfile.Profile.Name
+ delim + socialProfile.Profile.Age_Range_Min
+ delim + socialProfile.Profile.Age_Range_Max
+ delim + socialProfile.Profile.Birthday
;

textWriter.WriteLine(profile);
textWriter.Flush();
memStreamProfile.Flush();
adlsClient.DataLakeUpdateHandler(fileName, memStreamProfile);
}
}
}

希望有帮助。

关于c# - 使用 Azure Functions 调用 REST API 并将结果保存在 Azure Data Lake gen2 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55653360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com