【问题标题】:Connecting from C# to Accumulo从 C# 连接到 Accumulo
【发布时间】:2013-12-13 18:13:20
【问题描述】:

我刚开始使用 Accumulo。我需要通过 C# 从远程 Accumulo 读取/写入数据。 我发现的唯一 C# 代码示例/文档是 - Accumulo createBatchScanner range not working as expected

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

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

错误 CS0246:类型或命名空间名称 AccumuloProxy' could not be found. Are you missingorg.apache.accumulo.proxy.thrift' using 指令? (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);

【问题讨论】:

    标签: c# macos mono thrift accumulo


    【解决方案1】:

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

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

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

    C.具有 C# 绑定的 Accumulo 代理:
    在运行 Accumulo 的节点上执行以下操作
    1. 安装 Mono 3.2.5
    2. 安装 Thrift 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. C#项目中需要gen-csharp下的文件,在下面的D节中。
    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)

    这里有一些示例代码,用于读取数据并尝试此功能。

    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);
                }
            //}}
    
    
    
        }
    }
    }
    

    【讨论】:

      【解决方案2】:

      将 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,它不一定是最新的稳定版本。正如 cmets 中提到的 elserj,Accumulo 1.4.x 依赖于 Thrift 0.6.1,Accumulo 1.5.x 及更高版本依赖于 Thrift 0.9.0。

      【讨论】:

      • 关于版本,Accumulo 1.4.x 依赖于 Thrift 0.6.1,而 Accumulo 1.5.x 及更高版本依赖于 Thrift 0.9.0。
      • 不应使用 core/src/main/thrift 中的 thrift IDL 文件,除非您想重新实现所有 Java 客户端代码。这个答案应该指向proxy/src/main/thrift 中的thrift 代理IDL 文件。无论如何,这就是原始海报似乎正在使用的东西。还应该提到的是,实际上应该启动代理。
      • 谢谢,Christopher 和 elserj。根据需要编辑了我的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-16
      • 2014-08-17
      • 2014-08-27
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多