2015-06-22 111 views
1

呼吁纵观上https://azure.microsoft.com/en-us/documentation/articles/hdinsight-hbase-tutorial-get-started/#use-the-net-hbase-rest-api-client-library给出的示例代码,.NET HBase的REST API客户端库 - 从MVC5控制器

我试图从一个MVC控制器连接到HBase的如下:

using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Threading.Tasks; 
using System.Web.Http; 

using Microsoft.HBase.Client; 
using org.apache.hadoop.hbase.rest.protobuf.generated; 

namespace MyHBaseTest.Controllers 
{ 
    [RoutePrefix("api/myhbasetestcontroller")] 
    public class MyHBaseTestController : ApiController 
    { 
     HBaseReader hbase = new HBaseReader(); 

     [HttpGet] 
     [Route("")] 
     public IHttpActionResult Index() 
     { 

      string clusterURL = "https://<yourHBaseClusterName>.azurehdinsight.net"; 
      string hadoopUsername = "<yourHadoopUsername>"; 
      string hadoopUserPassword = "<yourHadoopUserPassword>"; 

      // Create a new instance of an HBase client. 
      ClusterCredentials creds = new ClusterCredentials(new Uri(clusterURL), hadoopUsername, hadoopUserPassword); 
      HBaseClient hbaseClient = new HBaseClient(creds); 

      // Retrieve the cluster version 
      var version = hbaseClient.GetVersion(); 
      Console.WriteLine("The HBase cluster version is " + version); 

      return Ok(); 
     } 
    } 
} 

当我在浏览器中以调试模式运行时,尝试查看URL/api/myhbasetestcontroller时,它会一直加载页面,而不会在Visual Studio中抛出任何异常或任何内容。我等了15-20分钟,但没有任何变化。

当我把尝试做相同的控制台应用程序,它就会在几秒钟的事,虽然版本信息:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

using Microsoft.HBase.Client; 
using org.apache.hadoop.hbase.rest.protobuf.generated; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string clusterURL = "https://<yourHBaseClusterName>.azurehdinsight.net"; 
      string hadoopUsername= "<yourHadoopUsername>"; 
      string hadoopUserPassword = "<yourHadoopUserPassword>"; 

      // Create a new instance of an HBase client. 
      ClusterCredentials creds = new ClusterCredentials(new Uri(clusterURL), hadoopUsername, hadoopUserPassword); 
      HBaseClient hbaseClient = new HBaseClient(creds); 

      // Retrieve the cluster version 
      var version = hbaseClient.GetVersion(); 
      Console.WriteLine("The HBase cluster version is " + version); 
     } 
    } 
} 

我只是不明白它是如何有差别真的。

你能请指教吗?

非常感谢。

+0

当它挂起时,你可以采取线程转储,所以你可以检查它到底在哪里等待? –

+0

如果我的回答回答了问题,请标记为答案。 –

回答

0

您正在使用阻塞同步API,这在MVC/Web应用程序的上下文中不起作用(由于默认使用了错误的异步上下文)。您需要使用方法的异步版本。例如。为GetVersion使用GetVersionAsync。

2

截至今天,您需要在后台线程上运行您的调用。我遇到了同样的问题。我的呼叫在一个功能下合并。我在后台线程上运行该功能,一切都很好。

 // POST: api/Vizzini 
    [ResponseType(typeof(string))] 
    public async Task<IHttpActionResult> GetResponse(string tweet) 
    { 
     string s = await Task.Run(() => 
     { 
      return ResponseEngine.GetBestResponse(tweet); 
     }); 
     return Ok(s); 
    }