windows下C#通過Thrift操作HBase

来源:http://www.cnblogs.com/smallcroco-blog/archive/2017/05/10/6836158.html
-Advertisement-
Play Games

1。到apache官網下載Thrift源碼, http://thrift.apache.org/download,我這裡下載的是thrift-0.10.0 2。到apach官網下載thrift.exe, http://thrift.apache.org/download, 這裡下載的是thrift- ...


1。到apache官網下載Thrift源碼, http://thrift.apache.org/download,我這裡下載的是thrift-0.10.0

2。到apach官網下載thrift.exe, http://thrift.apache.org/download, 這裡下載的是thrift-0.10.0.exe

3。將下載下來的thrift-0.10.0.exe放到thrift-0.10.0/tutorial/目錄下去

4。在windows命令行運行如下這兩條命令:

  thrift-0.10.0.exe --gen csharp tutorial.thrift

  thrift-0.10.0.exe --gen csharp shared.thrift

運行完以後就會在當前目錄下生成一個名為gen-csharp的目錄,到時候需要把這下麵的源碼文件添加到項目中去

 

5。用vs打開thrift-0.10.0/lib/csharp/src/Thrift.sln解決方案,並編譯得到Thrift.dll

6。用vs新建自己的項目,將之前生成的源碼添加進來,並引入Thrift.dll庫。

下麵就是Client端的代碼

using System;
using Thrift;
using Thrift.Protocol;
using Thrift.Server;
using Thrift.Transport;

namespace CSharpTutorial
{
    public class CSharpClient
    {
        public static void Main()
        {
            try
            {
                TTransport transport = new TSocket("localhost", 9090);
                TProtocol protocol = new TBinaryProtocol(transport);
                Calculator.Client client = new Calculator.Client(protocol);

                transport.Open();
                try
                {
                    client.ping();
                    Console.WriteLine("ping()");

                    int sum = client.add(1, 1);
                    Console.WriteLine("1+1={0}", sum);

                    Work work = new Work();

                    work.Op = Operation.DIVIDE;
                    work.Num1 = 1;
                    work.Num2 = 0;
                    try
                    {
                        int quotient = client.calculate(1, work);
                        Console.WriteLine("Whoa we can divide by 0");
                    }
                    catch (InvalidOperation io)
                    {
                        Console.WriteLine("Invalid operation: " + io.Why);
                    }

                    work.Op = Operation.SUBTRACT;
                    work.Num1 = 15;
                    work.Num2 = 10;
                    try
                    {
                        int diff = client.calculate(1, work);
                        Console.WriteLine("15-10={0}", diff);
                    }
                    catch (InvalidOperation io)
                    {
                        Console.WriteLine("Invalid operation: " + io.Why);
                    }

                    SharedStruct log = client.getStruct(1);
                    Console.WriteLine("Check log: {0}", log.Value);

                }
                finally
                {
                    transport.Close();
                }
            }
            catch (TApplicationException x)
            {
                Console.WriteLine(x.StackTrace);
            }

        }
    }
}

下麵是Server端代碼

using System;
using System.Collections.Generic;
using Thrift.Server;
using Thrift.Transport;

namespace CSharpTutorial
{
    public class CalculatorHandler : Calculator.Iface
    {
        Dictionary<int, SharedStruct> log;

        public CalculatorHandler()
        {
            log = new Dictionary<int, SharedStruct>();
        }

        public void ping()
        {
            Console.WriteLine("ping()");
        }

        public int add(int n1, int n2)
        {
            Console.WriteLine("add({0},{1})", n1, n2);
            return n1 + n2;
        }

        public int calculate(int logid, Work work)
        {
            Console.WriteLine("calculate({0}, [{1},{2},{3}])", logid, work.Op, work.Num1, work.Num2);
            int val = 0;
            switch (work.Op)
            {
                case Operation.ADD:
                    val = work.Num1 + work.Num2;
                    break;

                case Operation.SUBTRACT:
                    val = work.Num1 - work.Num2;
                    break;

                case Operation.MULTIPLY:
                    val = work.Num1 * work.Num2;
                    break;

                case Operation.DIVIDE:
                    if (work.Num2 == 0)
                    {
                        InvalidOperation io = new InvalidOperation();
                        io.WhatOp = (int)work.Op;
                        io.Why = "Cannot divide by 0";
                        throw io;
                    }
                    val = work.Num1 / work.Num2;
                    break;

                default:
                    {
                        InvalidOperation io = new InvalidOperation();
                        io.WhatOp = (int)work.Op;
                        io.Why = "Unknown operation";
                        throw io;
                    }
            }

            SharedStruct entry = new SharedStruct();
            entry.Key = logid;
            entry.Value = val.ToString();
            log[logid] = entry;

            return val;
        }

        public SharedStruct getStruct(int key)
        {
            Console.WriteLine("getStruct({0})", key);
            return log[key];
        }

        public void zip()
        {
            Console.WriteLine("zip()");
        }
    }

    public class CSharpServer
    {
        public static void Main()
        {
            try
            {
                CalculatorHandler handler = new CalculatorHandler();
                Calculator.Processor processor = new Calculator.Processor(handler);
                TServerTransport serverTransport = new TServerSocket(9090);
                TServer server = new TSimpleServer(processor, serverTransport);

                // Use this for a multithreaded server
                // server = new TThreadPoolServer(processor, serverTransport);

                Console.WriteLine("Starting the server...");
                server.Serve();
            }
            catch (Exception x)
            {
                Console.WriteLine(x.StackTrace);
            }
            Console.WriteLine("done.");
        }
    }
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • --20170505 --塗聚文 Geovin Du CREATE DATABASE DuMailSystem GO USE DuMailSystem GO --1查詢表的及備註說明 SELECT Sysobjects.name AS TABLE_NAME, syscolumns.Id, sysco... ...
  • 1、 關掉mysql服務 右鍵“我的電腦”,選擇“管理”,打開電腦管理,選擇“服務” 右鍵MySQL服務,選擇“停止” 2、 卸載mysql程式 開始菜單->控制面板->程式和功能 3、 刪除電腦上的殘留文件 (1) 刪除 C盤-》programData->mysql文件夾,programDat ...
  • 概述:INNER JOIN、LEFT JOIN、LIGHT JOIN、FULL JOIN. 一、INNER JOIN INNER JOIN 關鍵字在表中存在至少一個匹配時返回行。 語法: 此查詢會返回兩表中存在匹配項的值。 內連接: 現有一學生成績表:(Achievement) 現需查出 “語文成績 ...
  • 連接查詢: 將多張表(可以大於2張)進行記錄的連接(按照某個指定的條件進行數據拼接); 最終結果是: 記錄數有可能變化, 欄位數一定會增加(至少兩張表的合併)! 連接查詢的意義: 在用戶查看數據的時候,需要顯示的數據來自多張表. SQL中將連接查詢分成四類:交叉連接,內連接,外連接和自然連接。 在介 ...
  • 寫了個存儲過程,中間用到了類似這種寫法 其中'1,2,3'是從外面傳進來的參數,就這樣執行報錯:'1,2,3'轉換為int類型出錯,因為ID是int類型的 想了個比較笨的解決方法:思路-迴圈將傳進來的參數'1,2,3'分割並轉換為int然後存儲到臨時表,之後在In裡面Select ID這樣就不報錯了 ...
  • SQL(Structured Query Language)即結構化查詢語言。SQL 註入,就是把 SQL 命令插入到 Web 表單的輸入域或頁面請求參數的查詢字元串中,在 Web表單向 Web 伺服器提交 GET 或 POST 請求時,如果伺服器端未嚴格驗證參數的有效性和合法性,將導致資料庫伺服器 ...
  • 1.將一個資料庫的表複製到另一個資料庫,複製表結構及數據; 前提:兩個資料庫在同一個伺服器 語法:SELECT * INTO db1.dbo.tb1 FROM db2.dbo.tb2 說明:db1--目標資料庫;tb1--目標表,且目標資料庫無此表;db2--源資料庫;tb2--源表(需複製的表) ...
  • --單表遞歸 由於項目中經常用到 , 隨筆以作下次使用 例如:找ProductType表 下ID為1的分類的所有子級 with result as --result為別名( select * from TB_ProductType where Id=1 --查詢ID為1 的數據 union all ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...