2013-11-27 32 views
0

我是與Accumulo合作的新手。我需要通過C#從遠程Accumulo讀取/寫入數據。 唯一的代碼示例/ C#的文檔,我發現是 - Accumulo createBatchScanner range not working as expected從C#連接到Accumulo

我試圖編譯Xamarin Studio中的代碼,在Mac上。
我遇到的問題是這一行:

AccumuloProxy.Client client = new AccumuloProxy.Client(protocol); 

錯誤CS0246:類型或命名空間名稱AccumuloProxy' could not be found. Are you missing org.apache.accumulo.proxy.thrift」使用指令? (CS0246)(AccumuloIntegratorPrototype)

我在哪裏可以找到要添加到與AccumuloProxy客戶端相關的CSharp項目的DLL? 有沒有一種方法可以生成相同的?

下面是一個代碼片段:

namespace AccumuloIntegratorPrototype 
{ 
    class MainClass 
    { 
     static byte[] GetBytes(string str) 
     { 
      return Encoding.ASCII.GetBytes(str); 
     } 

     static string GetString(byte[] bytes) 
     { 
      return Encoding.ASCII.GetString(bytes); 
     } 

     public static void Main (string[] args) 
     { 
      try 
      { 
       /** connect **/ 
       TTransport transport = new TSocket("xxx.xx.x.xx", 42424); 
       transport = new TFramedTransport(transport); 
       TCompactProtocol protocol = new TCompactProtocol(transport); 
       transport.Open(); 

       AccumuloProxy.Client client = new AccumuloProxy.Client(protocol); 

回答

1

感謝所有的指針。
能夠完成我的項目。
這些是我的筆記。

A.版本:
Accumulo 1.5
節儉0.90
單聲道3.2.5用於從C#連接到Accumulo

B.策略/選項:
Accumulo代理API

C.與C#綁定的Accumulo代理服務器:
演出運行Accumulo
1.安裝單3.2.5
2.一個節點上執行以下操作安裝節儉0.90
3.配置的Accumulo代理服務
修改文件$ ACCUMULO_HOME /代理/代理。性能;
具體更新實例名稱,和動物園管理員
4.開始代理daemon-

${ACCUMULO_HOME}/bin/accumulo proxy -p ${ACCUMULO_HOME}/proxy/proxy.properties 

5.Generated C#綁定,使用proxy.thrift IDL文件

thrift --gen csharp $ACCUMULO_HOME/proxy/thrift/proxy.thrift 

這導致在$ {ACCUMULO_HOME}/proxy/thrift/
之下創建一個名爲gen-csharp的目錄。6.在C#項目中,下面的D部分需要gen-csharp下的文件。
7. Thrift.dll,也是需要的。

D. C#項目 - Accumulo客戶端:
1.創建一個類型庫的項目。
2.增加了在步驟C5下GEN-CSHARP的文件,上述到庫
3.增加參考thrift.dll
4.內置庫

E.從C#連接到Accumulo
在C#項目,讀取/寫入Accumulo,
1.增加了參考 - thrift.dll
2.增加參考建在部分d圖書館,上述
3.在Accumulo服務器,開始代理(請參閱步驟C4,陽離子)

下面是一些示例代碼,讀取數據,來嘗試這個功能了..

using System; 
using System.Text; 
using System.Collections.Generic; 

using Thrift.Protocol; 
using Thrift.Transport; 


namespace AccumuloIntegratorPrototype 
{ 
class MainClass 
{ 
    static byte[] GetBytes(string str) 
    { 
     return Encoding.ASCII.GetBytes(str); 
    } 


    static string GetString(byte[] bytes) 
    { 
     return Encoding.ASCII.GetString(bytes); 
    } 

    public static void Main (string[] args) 
    { 

     try 
     { 
      String accumuloProxyServerIP = "xxx.xxx.x.xx";//IP 
      int accumuloProxyServerPort = 42424;//Port Number 

      TTransport transport = new TSocket(accumuloProxyServerIP, accumuloProxyServerPort); 
      transport = new TFramedTransport(transport); 
      TCompactProtocol protocol = new TCompactProtocol(transport); 
      transport.Open(); 

      String principal = "root";//Application ID 
      Dictionary<string, string> passwd = new Dictionary<string,string>(); 
      passwd.Add("password", "xxxxx");//Password 

      AccumuloProxy.Client client = new AccumuloProxy.Client(protocol); 
      byte[] loginToken = client.login(principal, passwd);//Login token 


      //{{ 
      //Read a range of rows from Accumulo 
      var bScanner = new BatchScanOptions(); 
      Range range = new Range(); 

      range.Start = new Key(); 
      range.Start.Row = GetBytes("d001"); 

      //Need the \0 only if you need to get a single row back 
      //Otherwise, its not needed 
      range.Stop = new Key(); 
      range.Stop.Row = GetBytes("d001\0"); 

      bScanner.Ranges = new List<Range>(); 
      bScanner.Ranges.Add(range); 

      String scanId = client.createBatchScanner(loginToken, "departments", bScanner); 


      var more = true; 
      while (more) 
      { 
       var scan = client.nextK(scanId, 10); 
       more = scan.More; 

       foreach (var entry in scan.Results) 
       { 
        Console.WriteLine("Row = " + GetString(entry.Key.Row)); 
        Console.WriteLine("{0} {1}:{2} [{3}] {4} {5}", GetString(entry.Key.Row), GetString(entry.Key.ColFamily), GetString(entry.Key.ColQualifier), GetString(entry.Key.ColVisibility), GetString(entry.Value),(long)entry.Key.Timestamp); 
       } 
      } 

      client.closeScanner(scanId); 

      client.Dispose(); 
      transport.Close(); 

      }catch (Exception e) 
      { 
       Console.WriteLine(e); 
      } 
     //}} 



    } 
} 
} 
0

添加儉到C#項目包括兩個步驟:

  1. 添加已經由儉編譯
  2. 生成的裝置所生成的C#代碼Thrift.DLL並將其添加爲您的項目的參考。或者,可以將代碼鏈接到您的項目中,但不建議。

步驟1的C#代碼由Thrift IDL文件生成,該文件通常是項目的一部分。在你的情況下,IDL文件位於Accumulo樹中的proxy/src/main/thrift下。

Thrift編譯器和庫可以從http://thrift.apache.org下載。請注意,有些項目正在使用較舊版本的Apache Thrift,它不一定是最新的穩定版本。正如elserj在評論中提到的,Accumulo 1.4.x取決於Thrift 0.6.1,Accumulo 1.5.x和更高取決於Thrift 0.9.0。

+1

具體關於版本,Accumulo 1.4.x的依賴節儉0.6.1同時Accumulo 1.5.X並且更多取決於Thrift 0.9.0。 – elserj

+1

除非要重新實現所有Java客戶端代碼,否則不應使用core/src/main/thrift中的節點IDL文件。這個答案應該指向proxy/src/main/thrift中的節儉代理IDL文件。這就是原來的海報似乎無論如何使用。還應該提到,應該真正啓動代理。 – Christopher

+0

謝謝,克里斯托弗和elserj。根據需要編輯我的答案。 – JensG