- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想调用 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 中注册应用
在数据湖存储中授予权限
下面是创建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/
为了让我的代码几乎完全用 Jquery 编写,我想用 Jquery 重写 AJAX 调用。 这是从网页到 Tomcat servlet 的调用。 我目前情况的类似代码: var http = new
我想使用 JNI 从 Java 调用 C 函数。在 C 函数中,我想创建一个 JVM 并调用一些 Java 对象。当我尝试创建 JVM 时,JNI_CreateJavaVM 返回 -1。 所以,我想知
环顾四周,我发现从 HTML 调用 Javascript 函数的最佳方法是将函数本身放在 HTML 中,而不是外部 Javascript 文件。所以我一直在网上四处寻找,找到了一些简短的教程,我可以根
我有这个组件: import {Component} from 'angular2/core'; import {UserServices} from '../services/UserService
我正在尝试用 C 实现一个简单的 OpenSSL 客户端/服务器模型,并且对 BIO_* 调用的使用感到好奇,与原始 SSL_* 调用相比,它允许一些不错的功能。 我对此比较陌生,所以我可能会完全错误
我正在处理有关异步调用的难题: 一个 JQuery 函数在用户点击时执行,然后调用一个 php 文件来检查用户输入是否与数据库中已有的信息重叠。如果是这样,则应提示用户确认是否要继续或取消,如果他单击
我有以下类(class)。 public Task { public static Task getInstance(String taskName) { return new
嘿,我正在构建一个小游戏,我正在通过制作一个数字 vector 来创建关卡,该数字 vector 通过枚举与 1-4 种颜色相关联。问题是循环(在 Simon::loadChallenge 中)我将颜
我有一个java spring boot api(数据接收器),客户端调用它来保存一些数据。一旦我完成了数据的持久化,我想进行另一个 api 调用(应该处理持久化的数据 - 数据聚合器),它应该自行异
首先,这涉及桌面应用程序而不是 ASP .Net 应用程序。 我已经为我的项目添加了一个 Web 引用,并构建了各种数据对象,例如 PayerInfo、Address 和 CreditCard。但问题
我如何告诉 FAKE 编译 .fs文件使用 fsc ? 解释如何传递参数的奖励积分,如 -a和 -target:dll . 编辑:我应该澄清一下,我正在尝试在没有 MSBuild/xbuild/.sl
我使用下划线模板配置了一个简单的主干模型和 View 。两个单独的 API 使用完全相同的配置。 API 1 按预期工作。 要重现该问题,请注释掉 API 1 的 URL,并取消注释 API 2 的
我不确定什么是更好的做法或更现实的做法。我希望从头开始创建目录系统,但不确定最佳方法是什么。 我想我在需要显示信息时使用对象,例如 info.php?id=100。有这样的代码用于显示 Game.cl
from datetime import timedelta class A: def __abs__(self): return -self class B1(A):
我在操作此生命游戏示例代码中的数组时遇到问题。 情况: “生命游戏”是约翰·康威发明的一种细胞自动化技术。它由一个细胞网格组成,这些细胞可以根据数学规则生存/死亡/繁殖。该网格中的活细胞和死细胞通过
如果我像这样调用 read() 来读取文件: unsigned char buf[512]; memset(buf, 0, sizeof(unsigned char) * 512); int fd;
我用 C 编写了一个简单的服务器,并希望调用它的功能与调用其他 C 守护程序的功能相同(例如使用 ./ftpd start 调用它并使用 ./ftpd stop 关闭该实例)。显然我遇到的问题是我不知
在 dos 中,当我粘贴此命令时它会起作用: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://google.
在 dos 中,当我粘贴此命令时它会起作用: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://google.
我希望能够从 cmd 在我的 Windows 10 计算机上调用 python3。 我已重新安装 Python3.7 以确保选择“添加到路径”选项,但仍无法调用 python3 并使 CMD 启动 P
我是一名优秀的程序员,十分优秀!