C# 使用MapReduce获取c中URL的响应#
我有以下程序,它从数据库中获取超过100000个URL,并使用C#HttpWebRequest对象获取响应并保存到数据库中 要获得100000个URL的响应,需要在单个服务器上运行很长时间。我想把这个进程分成线程,并希望在多个服务器上运行 有可能在C#中的MapReduce中使用这种类型的代码以及线程和TPLC# 使用MapReduce获取c中URL的响应#,c#,.net,hadoop,mapreduce,task-parallel-library,C#,.net,Hadoop,Mapreduce,Task Parallel Library,我有以下程序,它从数据库中获取超过100000个URL,并使用C#HttpWebRequest对象获取响应并保存到数据库中 要获得100000个URL的响应,需要在单个服务器上运行很长时间。我想把这个进程分成线程,并希望在多个服务器上运行 有可能在C#中的MapReduce中使用这种类型的代码以及线程和TPL using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; usin
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Json;
using System.Net;
using System.Runtime.Serialization;
using System.IO;
namespace Test
{
class Program
{
static void Main(string[] args)
{
TestApp testapp = new TestApp();
var urls = testapp.GetURls();
//get string from urls
foreach (var url in urls)
{
var responseString=testapp.GetURLString(url);
//now save response into database
testapp.saveResponseToDB(responseString);
}
}
}
public class TestApp
{
public void saveResponseToDB(string response)
{
//save into db
}
public List<string> GetURls()
{
var urls =new List<string>();
//fetching more then 100000 urls from database.
return urls;
}
public string GetURLString(string requestUrl)
{
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
// Read the content.
string responseFromServer = reader.ReadToEnd();
// return the content.
return responseFromServer;
}
}
}
}
使用Newtonsoft.Json;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用系统文本;
使用System.Threading.Tasks;
使用System.Runtime.Serialization.Json;
Net系统;
使用System.Runtime.Serialization;
使用System.IO;
名称空间测试
{
班级计划
{
静态void Main(字符串[]参数)
{
TestApp TestApp=新TestApp();
var url=testapp.GetURls();
//从URL获取字符串
foreach(url中的变量url)
{
var responseString=testapp.GetURLString(url);
//现在将响应保存到数据库中
testapp.saveResponseToDB(responseString);
}
}
}
公共类测试
{
公共void saveResponseToDB(字符串响应)
{
//保存到数据库中
}
公共列表getURL()
{
var url=新列表();
//从数据库获取超过100000个URL。
返回URL;
}
公共字符串GetURLString(字符串请求URL)
{
HttpWebRequest-request=WebRequest.Create(requestUrl)作为HttpWebRequest;
使用(HttpWebResponse=request.GetResponse()作为HttpWebResponse)
{
StreamReader=新的StreamReader(response.GetResponseStream());
//阅读内容。
字符串responseFromServer=reader.ReadToEnd();
//返回内容。
返回responseFromServer;
}
}
}
}
为此,您可能想看看hadoop流媒体。您不需要MapReduce。MapReduce用于处理数据,而不是发出GET请求并保存结果。Hadoop也不会增加单台机器的请求数量。