[转帖]C# Hadoop学习笔记(一)—环境安装 ._Hadoop,ERP及大数据讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Hadoop,ERP及大数据讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 4619 | 回复: 0   主题: [转帖]C# Hadoop学习笔记(一)—环境安装 .        下一篇 
jie.liang
注册用户
等级:少校
经验:1003
发帖:77
精华:0
注册:2013-10-11
状态:离线
发送短消息息给jie.liang 加好友    发送短消息息给jie.liang 发消息
发表于: IP:您无权察看 2013-10-17 17:02:06 | [全部帖] [楼主帖] 楼主

一、安装环境

1,前期准备:官网下载“NuGet Package Manager”,按自己已有的VS环境下载对应版本;

2,利用NuGet下载Hadoop For .NET SDK,地址“http://hadoopsdk.codeplex.com/”

3,安装。

4,通过HDInsight,安装Windows Azure,目前是预览版本。

5,参照网址“http://blogs.msdn.com/b/data_otaku/archive/2013/08/14/hadoop-for-net-developers.aspx” 系统学习API

二、测试DEMO



代码

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.Threading.Tasks; 
  6. using Microsoft.Hadoop; 
  7. using Microsoft.Hadoop.MapReduce; 
  8. using Microsoft.Hadoop.WebClient.WebHCatClient; 
  9. using System.Diagnostics; 
  10. using System.IO; 
  11. using System.IO.MemoryMappedFiles; 
  12. namespace HadoopConsol 
  13. { 
  14.        class Program 
  15.        { 
  16.              static void Main(string[] args) 
  17.              { 
  18.                    Stopwatch sw = new Stopwatch(); 
  19.                    long hadoopTime=0; 
  20.                    long normalTime=0; 
  21.                   
  22.                    sw.Start(); 
  23.                   
  24.                    //start hadoop 
  25.                    Console.WriteLine(" Hadoop Process Strating ...."); 
  26.                   
  27.                    #region Hadoop time 
  28.                   
  29.                    #region hadoopconnet 
  30.                   
  31.                    Console.WriteLine(" Hadoop Connect Strating ...."); 
  32.                   
  33.                    //establish job configuration 
  34.                   
  35.                    HadoopJobConfiguration myConfig = new HadoopJobConfiguration(); 
  36.                   
  37.                    myConfig.InputPath = "/demo/simple/in"; 
  38.                   
  39.                    myConfig.OutputFolder = "/demo/simple/out"; 
  40.                   
  41.                   
  42.                   
  43.                    //connect to cluster 
  44.                   
  45.                    Uri myUri = new Uri("http://localhost"); 
  46.                   
  47.                    string userName = "hadoop"; 
  48.                   
  49.                    string passWord = null; 
  50.                   
  51.                    IHadoop myCluster = Hadoop.Connect(myUri, userName, passWord); 
  52.                   
  53.                    hadoopTime += sw.ElapsedMilliseconds; 
  54.                   
  55.                    Console.WriteLine(" Hadoop Connect End."); 
  56.                   
  57.                    Console.WriteLine(" Hadoop Connect time:" + sw.ElapsedMilliseconds); 
  58.                   
  59.                    #endregion 
  60.                   
  61.                   
  62.                    #region hadoopmapreduce 
  63.                   
  64.                    sw.Reset(); 
  65.                    sw.Start(); 
  66.                   
  67.                    Console.WriteLine(" Hadoop MapReduce Strating ...."); 
  68.                   
  69.                    //execute mapreduce job 
  70.                   
  71.                    MapReduceResult jobResult = 
  72.                   
  73.                    myCluster.MapReduceJob.Execute<MySimpleMapper, MySimpleReducer>(myConfig); 
  74.                   
  75.                    hadoopTime += sw.ElapsedMilliseconds; 
  76.                   
  77.                    Console.WriteLine(" Hadoop MapReduce End."); 
  78.                    Console.WriteLine(" Hadoop MapReduce Time:"+sw.ElapsedMilliseconds); 
  79.                   
  80.                    #endregion 
  81.                   
  82.                    #region Hadoop End 
  83.                   
  84.                    sw.Reset(); 
  85.                    sw.Start(); 
  86.                   
  87.                    Console.WriteLine(" Hadoop Endprocess Strating ...."); 
  88.                   
  89.                    //write job result to console 
  90.                   
  91.                    int exitCode = jobResult.Info.ExitCode; 
  92.                   
  93.                   
  94.                   
  95.                    string exitStatus = "Failure"; 
  96.                   
  97.                    if (exitCode == 0) exitStatus = "Success"; 
  98.                   
  99.                    exitStatus = exitCode + " (" + exitStatus + ")"; 
  100.                   
  101.                    Console.WriteLine(); 
  102.                   
  103.                    Console.Write("Exit Code = " + exitStatus); 
  104.                   
  105.                    Console.WriteLine(" Hadoop Endprocess End."); 
  106.                    hadoopTime += sw.ElapsedMilliseconds; 
  107.                    Console.WriteLine(" Hadoop Exit Time:" + sw.ElapsedMilliseconds); 
  108.                    Console.WriteLine(" Hadoop Process All Time:" + hadoopTime); 
  109.                    #endregion 
  110.                   
  111.                    #endregion 
  112.                   
  113.                   
  114.                    #region Normal time 
  115.                    //start Normal 
  116.                    Console.WriteLine(" Normal Process Strating ...."); 
  117.                   
  118.                    sw.Reset(); 
  119.                    sw.Start(); 
  120.                   
  121.                    //normal process 
  122.                    #region Normal Process 
  123.                   
  124.                    int myevenCount = 0; 
  125.                    int myeventSum = 0; 
  126.                   
  127.                    int myoddCount = 0; 
  128.                    int myoddSum = 0; 
  129.                   
  130.                    StreamReader fs = new StreamReader(@"c:\TEMP\integers.txt"); 
  131.                   
  132.                    while (fs.Peek() >= 0) 
  133.                    { 
  134.                          string strTemp = fs.ReadLine(); 
  135.                          if (Int32.Parse(strTemp) % 2 == 0) 
  136.                          { 
  137.                                myevenCount++; 
  138.                                myeventSum += Int32.Parse(strTemp); 
  139.                          } 
  140.                          else 
  141.                          { 
  142.                                myoddCount++; 
  143.                                myoddSum += Int32.Parse(strTemp); 
  144.                          } 
  145.                    } 
  146.                    //MemoryMappedFile m = MemoryMappedFile. 
  147.                    Console.WriteLine("even:" + "\t" + myevenCount + "\t" + myeventSum); 
  148.                    Console.WriteLine("odd:" + "\t" + myoddCount + "\t" + myoddSum); 
  149.                   
  150.                    #endregion 
  151.                   
  152.                    Console.WriteLine(" Normal Process End."); 
  153.                   
  154.                    normalTime += sw.ElapsedMilliseconds; 
  155.                    Console.WriteLine(" Normal Exit Time:" + sw.ElapsedMilliseconds); 
  156.                    Console.WriteLine(" Normal Process All Time:" + normalTime); 
  157.                   
  158.                    #endregion 
  159.                   
  160.                    sw.Stop(); 
  161.                   
  162.                    Console.Read(); 
  163.                   
  164.              } 
  165.             
  166.        } 
  167.       
  168.        public class MySimpleMapper : MapperBase 
  169.        { 
  170.             
  171.              public override void Map(string inputLine, MapperContext context) 
  172.              { 
  173.                   
  174.                    //interpret the incoming line as an integer value 
  175.                   
  176.                    int value = int.Parse(inputLine); 
  177.                   
  178.                    //determine whether value is even or odd 
  179.                   
  180.                    string key = (value % 2 == 0) ? "even" : "odd"; 
  181.                   
  182.                    //output key assignment with value 
  183.                   
  184.                    context.EmitKeyValue(key, value.ToString()); 
  185.                   
  186.              } 
  187.             
  188.        } 
  189.       
  190.        public class MySimpleReducer : ReducerCombinerBase 
  191.        { 
  192.             
  193.              public override void Reduce( 
  194.             
  195.              string key, IEnumerable<string> values, ReducerCombinerContext context 
  196.             
  197.              ) 
  198.              { 
  199.                   
  200.                    //initialize counters 
  201.                   
  202.                    int myCount = 0; 
  203.                   
  204.                    int mySum = 0; 
  205.                   
  206.                   
  207.                   
  208.                    //count and sum incoming values 
  209.                   
  210.                    foreach (string value in values) 
  211.                    { 
  212.                         
  213.                          mySum += int.Parse(value); 
  214.                         
  215.                          myCount++; 
  216.                         
  217.                    } 
  218.                   
  219.                   
  220.                   
  221.                    //output results 
  222.                   
  223.                    context.EmitKeyValue(key, myCount + "\t" + mySum); 
  224.                   
  225.              } 
  226.             
  227.             
  228.        } 
  229. } 


三、测试结果

北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论