从C#连接到Accumulo

我不熟悉与Accumulo合作。 我需要通过C#从远程Accumulo读取/写入数据。 我发现C#的唯一代码示例/文档是 – Accumulo createBatchScanner范围不能按预期工作

我试图在Mac上的Xamarin Studio中编译代码。
我遇到的问题是这一行:

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

错误CS0246:找不到类型或命名空间名称AccumuloProxy' could not be found. Are you missing 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); 

感谢大家指点。
能够完成我的项目。
这些是我的笔记。

A.版本:
Accumulo 1.5
节俭0.90
单声道3.2.5

B.用于从C#连接到Accumulo的策略/选项:
Accumulo Proxy API

C.使用C#绑定的Accumulo代理:
在运行Accumulo的节点上执行以下操作
1.安装Mono 3.2.5
2.安装节俭0.90
3.配置Accumulo代理服务
修改了文件$ ACCUMULO_HOME / proxy / proxy.properties;
具体更新了实例名称和zookeeper
4.启动代理守护程序 –

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

5.使用proxy.thrift IDL文件生成c#绑定

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

这导致在$ {ACCUMULO_HOME} / proxy / thrift /下创建名为gen-csharp的目录
6.下面D节中的C#项目需要gen-csharp下的文件。
7.还需要Thrift.dll。

D. C#项目 – Accumulo客户:
1.创建了类型库的项目。
2.将上面步骤C5中gen-csharp下的文件添加到库中
3.添加了对thrift.dll的引用
4.建立图书馆

E.从C#连接到Accumulo
在读取/写入Accumulo的C#项目中,
1.添加了参考 – thrift.dll
2.添加了对上面D节中构建的库的引用
3.在Accumulo服务器上,启动代理(参见上面的步骤C4)

下面是一些示例代码,用于读取数据,以尝试此function。

 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 passwd = new Dictionary(); 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(); 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); } //}} } } } 

将Thrift添加到C#项目涉及两个步骤:

  1. 添加通过Thrift编译器生成的C#代码
  2. 构建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。

Interesting Posts