zoukankan      html  css  js  c++  java
  • windows平台下用C#访问HBase

    Hadoop中的HBase有多种数据访问方式,ubuntu里可以用hbase shell查看操作hbase数据库,但windows平台下需要用thrift对它进行访问。

    例如hadoop安装在/usr/local/hadoop,hbase在/usr/local/hbase,thrift在/usr/local/thrift,则hbase.thrift文件应该使用 hbase/src/main/resources/org/apache/hadoop/hbase/thrift/hbase.thrift这个文件。thrift2目录下的hbase.thrift生成的方法不同,不好用。

    1. 到thrift目录下,运行

    bin/thrift -gen csharp /usr/local/hbasesrc/main/resources/org/apache/hadoop/hbase/thrift/hbase.thrift

    会在当前目录生成gen-csharp文件夹,运行 zip -r cs.zip gen-csharp 打包到cs.zip,用ftp传输给windows。

    2. 把thrift目录里的thrift c#访问类代码也打包传输到windows。

    /usr/local/thrift/lib/csharp,src目录里有Thrift.csproj

    3. 建立TestHbaseClient控制台程序,把Thrift项目加入solution。建立HBaseCommon项目,把gen-csharp里的文件都加入到此项目,最好给每个文件都加上命名空间。HBaseCommon引用Thrift,TestHbaseClient引用其它两个项目。

    在Main函数里:

     1 TTransport transport = new TSocket("192.168.16.105", 9090);
     2 TProtocol tProtocol = new TBinaryProtocol(transport);
     3 var client = new Hbase.Client(tProtocol);
     4 transport.Open();
     5 
     6 List<byte[]> tbs =  client.getTableNames();
     7 foreach (byte[] tb in tbs)
     8 {
     9     string tn = Encoding.UTF8.GetString(tb);
    10     Console.WriteLine("table:" + tn);
    11 }
    12 
    13 List<TRowResult> reslut = client.getRow(Encoding.UTF8.GetBytes("case_info"), Encoding.UTF8.GetBytes("row1"), null);
    14 foreach (var key in reslut)
    15 {
    16     Console.WriteLine(Encoding.UTF8.GetString(key.Row)); 
    17     foreach (var k in key.Columns)
    18     {
    19         Console.Write(Encoding.UTF8.GetString(k.Key) + "	");
    20         Console.WriteLine(Encoding.UTF8.GetString(k.Value.Value));
    21     }
    22 }

    4. 运行这段代码,会报错,连不上服务器,需要到hbase里设置一下,并启动thrift服务。

    修改/usr/local/hbase/conf/hbase-site.xml,先加个伪分布式进行测试:

    <property>

    <name>hbase.rootdir</name>

    <value>/home/hadoop/hbasedir</value>

    </property>

    到/usr/local/hbase目录下重启hbase服务

    bin/hbase-start.sh

    再运行bin/hbase-daemon.sh start thrift

    现在就可以用C#去访问hbase数据库的内容了。

  • 相关阅读:
    BIOS/MBR UEFI/GPT关系与区别-资料整理
    raid 简单了解
    MBR主引导记录
    linux 安装vscode
    chrome 获得点击按钮时的事件
    python计算纪念日相关
    python error: curl: (1) Protocol "'https" not supported or disabled in libcurl
    linux go with vscode
    postman 进阶技巧
    mysql常用时间函数与类型转换
  • 原文地址:https://www.cnblogs.com/panyee/p/3245362.html
Copyright © 2011-2022 走看看