1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
class Program
{
private static IMapReduces.IMapReduce remoteOperation;
private static string epath = @"F:\Visual Studio 2008\Projects\BlueCurve Desktop Search\Lucene\Lucene.Net\Index";
private static IEnumerable<KeyValuePair<string, int>> WordCountMap(string docId, string doc)
{
char[] splitChars = { ' ', '\r', '\n', '\t', '\x0085' };
foreach (string word in doc.Split(splitChars, StringSplitOptions.RemoveEmptyEntries))
{
yield return new KeyValuePair<string, int>(word, 1);
}
}
private static IEnumerable<KeyValuePair<string, int>> WordCountReduce(string word, IEnumerable<int> counts)
{
int wordCount = 0;
foreach (int count in counts)
{
wordCount += count;
}
yield return new KeyValuePair<string, int>(word, wordCount);
}
private static IEnumerable<KeyValuePair<string, string>> AllFilesInDirectory(string path, string searchPattern)
{
foreach (string fileName in Directory.GetFiles(path, searchPattern))
{
yield return new KeyValuePair<string, string>(fileName, File.ReadAllText(fileName));
}
}
static void Main()
{
try
{
TcpChannel channel = new TcpChannel();
ChannelServices.RegisterChannel(channel,true);
remoteOperation = (IMapReduces.IMapReduce)Activator.GetObject(
typeof(IMapReduces.IMapReduce),
"tcp://192.168.1.5:1069/RemoteOperation");
if (remoteOperation != null)
{
foreach (
KeyValuePair<string, int> wordAndCount in remoteOperation.Run<string, string, string, int, string, int>(
WordCountMap, WordCountReduce, AllFilesInDirectory(epath, "*.cs")))
{
Console.WriteLine("{0}: {1}", wordAndCount.Value, wordAndCount.Key);
}
}
}
catch (Exception e) {
Console.WriteLine(e);
}
Console.ReadLine();
}
} |
Partager