[转帖]C# Hadoop学习笔记(一)—环境安装 ._Hadoop,ERP及大数据讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Hadoop,ERP及大数据讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3489 | 回复: 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.        class Program 
  14.        { 
  15.              static void Main(string[] args) 
  16.              { 
  17.                    Stopwatch sw = new Stopwatch(); 
  18.                    long hadoopTime=0; 
  19.                    long normalTime=0; 
  20.                   
  21.                    sw.Start(); 
  22.                   
  23.                    //start hadoop 
  24.                    Console.WriteLine(" Hadoop Process Strating ...."); 
  25.                   
  26.                    #region Hadoop time 
  27.                   
  28.                    #region hadoopconnet 
  29.                   
  30.                    Console.WriteLine(" Hadoop Connect Strating ...."); 
  31.                   
  32.                    //establish job configuration 
  33.                   
  34.                    HadoopJobConfiguration myConfig = new HadoopJobConfiguration(); 
  35.                   
  36.                    myConfig.InputPath = "/demo/simple/in"; 
  37.                   
  38.                    myConfig.OutputFolder = "/demo/simple/out"; 
  39.                   
  40.                   
  41.                   
  42.                    //connect to cluster 
  43.                   
  44.                    Uri myUri = new Uri("http://localhost"); 
  45.                   
  46.                    string userName = "hadoop"; 
  47.                   
  48.                    string passWord = null; 
  49.                   
  50.                    IHadoop myCluster = Hadoop.Connect(myUri, userName, passWord); 
  51.                   
  52.                    hadoopTime += sw.ElapsedMilliseconds; 
  53.                   
  54.                    Console.WriteLine(" Hadoop Connect End."); 
  55.                   
  56.                    Console.WriteLine(" Hadoop Connect time:" + sw.ElapsedMilliseconds); 
  57.                   
  58.                    #endregion 
  59.                   
  60.                   
  61.                    #region hadoopmapreduce 
  62.                   
  63.                    sw.Reset(); 
  64.                    sw.Start(); 
  65.                   
  66.                    Console.WriteLine(" Hadoop MapReduce Strating ...."); 
  67.                   
  68.                    //execute mapreduce job 
  69.                   
  70.                    MapReduceResult jobResult = 
  71.                   
  72.                    myCluster.MapReduceJob.Execute<MySimpleMapper, MySimpleReducer>(myConfig); 
  73.                   
  74.                    hadoopTime += sw.ElapsedMilliseconds; 
  75.                   
  76.                    Console.WriteLine(" Hadoop MapReduce End."); 
  77.                    Console.WriteLine(" Hadoop MapReduce Time:"+sw.ElapsedMilliseconds); 
  78.                   
  79.                    #endregion 
  80.                   
  81.                    #region Hadoop End 
  82.                   
  83.                    sw.Reset(); 
  84.                    sw.Start(); 
  85.                   
  86.                    Console.WriteLine(" Hadoop Endprocess Strating ...."); 
  87.                   
  88.                    //write job result to console 
  89.                   
  90.                    int exitCode = jobResult.Info.ExitCode; 
  91.                   
  92.                   
  93.                   
  94.                    string exitStatus = "Failure"; 
  95.                   
  96.                    if (exitCode == 0) exitStatus = "Success"; 
  97.                   
  98.                    exitStatus = exitCode + " (" + exitStatus + ")"; 
  99.                   
  100.                    Console.WriteLine(); 
  101.                   
  102.                    Console.Write("Exit Code = " + exitStatus); 
  103.                   
  104.                    Console.WriteLine(" Hadoop Endprocess End."); 
  105.                    hadoopTime += sw.ElapsedMilliseconds; 
  106.                    Console.WriteLine(" Hadoop Exit Time:" + sw.ElapsedMilliseconds); 
  107.                    Console.WriteLine(" Hadoop Process All Time:" + hadoopTime); 
  108.                    #endregion 
  109.                   
  110.                    #endregion 
  111.                   
  112.                   
  113.                    #region Normal time 
  114.                    //start Normal 
  115.                    Console.WriteLine(" Normal Process Strating ...."); 
  116.                   
  117.                    sw.Reset(); 
  118.                    sw.Start(); 
  119.                   
  120.                    //normal process 
  121.                    #region Normal Process 
  122.                   
  123.                    int myevenCount = 0; 
  124.                    int myeventSum = 0; 
  125.                   
  126.                    int myoddCount = 0; 
  127.                    int myoddSum = 0; 
  128.                   
  129.                    StreamReader fs = new StreamReader(@"c:\TEMP\integers.txt"); 
  130.                   
  131.                    while (fs.Peek() >= 0) 
  132.                    { 
  133.                          string strTemp = fs.ReadLine(); 
  134.                          if (Int32.Parse(strTemp) % 2 == 0) 
  135.                          { 
  136.                                myevenCount++; 
  137.                                myeventSum += Int32.Parse(strTemp); 
  138.                          } 
  139.                          else 
  140.                          { 
  141.                                myoddCount++; 
  142.                                myoddSum += Int32.Parse(strTemp); 
  143.                          } 
  144.                    } 
  145.                    //MemoryMappedFile m = MemoryMappedFile. 
  146.                    Console.WriteLine("even:" + "\t" + myevenCount + "\t" + myeventSum); 
  147.                    Console.WriteLine("odd:" + "\t" + myoddCount + "\t" + myoddSum); 
  148.                   
  149.                    #endregion 
  150.                   
  151.                    Console.WriteLine(" Normal Process End."); 
  152.                   
  153.                    normalTime += sw.ElapsedMilliseconds; 
  154.                    Console.WriteLine(" Normal Exit Time:" + sw.ElapsedMilliseconds); 
  155.                    Console.WriteLine(" Normal Process All Time:" + normalTime); 
  156.                   
  157.                    #endregion 
  158.                   
  159.                    sw.Stop(); 
  160.                   
  161.                    Console.Read(); 
  162.                   
  163.              } 
  164.             
  165.        } 
  166.       
  167.        public class MySimpleMapper : MapperBase 
  168.        { 
  169.             
  170.              public override void Map(string inputLine, MapperContext context) 
  171.              { 
  172.                   
  173.                    //interpret the incoming line as an integer value 
  174.                   
  175.                    int value = int.Parse(inputLine); 
  176.                   
  177.                    //determine whether value is even or odd 
  178.                   
  179.                    string key = (value % 2 == 0) ? "even" : "odd"; 
  180.                   
  181.                    //output key assignment with value 
  182.                   
  183.                    context.EmitKeyValue(key, value.ToString()); 
  184.                   
  185.              } 
  186.             
  187.        } 
  188.       
  189.        public class MySimpleReducer : ReducerCombinerBase 
  190.        { 
  191.             
  192.              public override void Reduce( 
  193.             
  194.              string key, IEnumerable<string> values, ReducerCombinerContext context 
  195.             
  196.              ) 
  197.              { 
  198.                   
  199.                    //initialize counters 
  200.                   
  201.                    int myCount = 0; 
  202.                   
  203.                    int mySum = 0; 
  204.                   
  205.                   
  206.                   
  207.                    //count and sum incoming values 
  208.                   
  209.                    foreach (string value in values) 
  210.                    { 
  211.                         
  212.                          mySum += int.Parse(value); 
  213.                         
  214.                          myCount++; 
  215.                         
  216.                    } 
  217.                   
  218.                   
  219.                   
  220.                    //output results 
  221.                   
  222.                    context.EmitKeyValue(key, myCount + "\t" + mySum); 
  223.                   
  224.              } 
  225.             
  226.             
  227.        } 


三、测试结果

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




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