亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

在.NET平臺下使用C#通過Thrift訪問Cassandra

系統(tǒng) 2734 0

?

幾家做seo無恥公司名單

?

http://www.byywee.com?
http://www.cosdiv.com

?

通過在自已網(wǎng)站加入別人網(wǎng)站連接,這種網(wǎng)站專門對新開通的網(wǎng)站做連接,從而達到宣傳他們目的,

?

http://www.renrenaj.com/about/copyright.html


1、下載Thrift

兩個文件:

thrift-0.7.0.tar.gz

Thrift compiler for Windows

2、獲取Thrift.dll

解壓后,找到源代碼:

thrift-0.7.0\lib\csharp\src,在Visual Studio中打開Thrift.csproj,重新編譯生成dll。

3、生成C#代碼

將thrift-0.7.0.exe復制到Cassandra安裝目錄的interface目錄中。

在命令提示符工具中進入interface目錄,執(zhí)行以下命令:

    thrift-0.7.0.exe --gen csharp cassandra.thrift
  

完畢后會在這個目錄中生成一個文件夾:gen-csharp。

4、獲取Apache.Cassandra.dll

新建一個類庫項目,把這些文件加到這個項目中,編譯生成一個dll文件。

cassandra thrift 在.NET平臺下使用C#通過Thrift訪問Cassandra

別忘了添加引用上邊生成的Thrift.dll文件。

5、編寫測試程序

在Visual Studio中創(chuàng)建一個項目,這里以用戶令牌為例,給出兩個方法:

(1)、插入數(shù)據(jù)

    public string SetUserToken()

        {

            string keySpaceName = "UserTokenSpace";

            string columnFamilyName = "UserToken";

            string columnName = "Token";

            string key = "1001";

            string token = "we9g872m9f5l";

            Encoding utf8Encoding = Encoding.UTF8;

            long timeStamp = DateTime.Now.Ticks;



            TTransport frameTransport = null;

            try

            {

                // 通過Thrift建立到Cassandra的連接

                frameTransport = new TFramedTransport(new TSocket("localhost", 9160));

                TProtocol protocol = new TBinaryProtocol(frameTransport);

                TProtocol frameProtocol = new TBinaryProtocol(frameTransport);

                Cassandra.Client client = new Cassandra.Client(protocol, frameProtocol);

                frameTransport.Open();



                // 先刪除已經(jīng)創(chuàng)建的KeySpace,以便于測試

                try

                {

                    client.system_drop_keyspace(keySpaceName);

                }

                catch

                {

                }



                // KeySpace 定義

                KsDef ks = new KsDef()

                {

                    Name = keySpaceName,

                    Replication_factor = 1,

                    Strategy_class = "org.apache.cassandra.locator.SimpleStrategy",



                    // Column Family 定義

                    Cf_defs = new List<CfDef>() {

                        new CfDef(){

                                        Name = columnFamilyName,

                                        Keyspace = keySpaceName,

                                        Comparator_type = "BytesType",



                                        // Column 定義

                                        Column_metadata = new List<ColumnDef>(){

                                            new ColumnDef(){

                                                Index_name = columnName,

                                                Index_type = IndexType.KEYS,

                                                Name = utf8Encoding.GetBytes("Token"),

                                                Validation_class = "BytesType"

                                            }

                                        }

                                    }

                    }

                };



                // 添加KeySpace

                client.system_add_keyspace(ks);



                //設置當前KeySpace

                client.set_keyspace(keySpaceName);



                // 要插入數(shù)據(jù)的路徑

                ColumnParent tokenColumnParent = new ColumnParent()

                {

                    Column_family = columnFamilyName

                };



                // 要插入數(shù)據(jù)的Column

                Column tokenColume = new Column()

                {

                    Name = utf8Encoding.GetBytes(columnName),

                    Value = utf8Encoding.GetBytes(token),

                    Timestamp = timeStamp

                };



                //插入數(shù)據(jù)

                client.insert(utf8Encoding.GetBytes(key), tokenColumnParent, tokenColume, ConsistencyLevel.ONE);

            }

            finally

            {

                if (frameTransport != null)

                {

                    frameTransport.Close();

                }

            }



            return token;

        }
  

(2)、獲取數(shù)據(jù)

    public static string GetUserToken(string userId)

        {

            string keySpaceName = "UserTokenSpace";

            string columnFamilyName = "UserToken";

            string columnName = "Token";

            System.Text.Encoding utf8Encoding = System.Text.Encoding.UTF8;

            long timeStamp = DateTime.Now.Millisecond;



            TTransport frameTransport = null;

            try

            {

                // 通過Thrift建立到Cassandra的連接

                frameTransport = new TFramedTransport(new TSocket("localhost", 9160));

                TProtocol protocol = new TBinaryProtocol(frameTransport);

                TProtocol frameProtocol = new TBinaryProtocol(frameTransport);

                Cassandra.Client client = new Cassandra.Client(protocol, frameProtocol);

                frameTransport.Open();



                // 設置當前KeySpace

                client.set_keyspace(keySpaceName);



                // 查找路徑

                ColumnPath nameColumnPath = new ColumnPath()

                {

                    Column_family = columnFamilyName,

                    Column = utf8Encoding.GetBytes(columnName)

                };



                // 獲取Column

                ColumnOrSuperColumn returnedColumn = client.get(utf8Encoding.GetBytes(userId), nameColumnPath, ConsistencyLevel.ONE);

                return utf8Encoding.GetString(returnedColumn.Column.Value);

            }

            catch

            {

            }

            finally

            {

                // 別忘了關閉連接

                if (frameTransport != null)

                {

                    frameTransport.Close();

                }

            }



            return string.Empty;

        }
  

?

使用ThriftAPI進行數(shù)據(jù)操作還是很繁瑣的,nosql的操作規(guī)范沒有sql那么好啊,不過效率肯定有保證。接下來的文章會介紹一些高級API的使用。

在.NET平臺下使用C#通過Thrift訪問Cassandra


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久久久久久久久久96av | 国产精品99久久99久久久看片 | 殴美一级片 | 啪啪综合网 | 婷婷亚洲综合五月天在线 | 国产日韩不卡免费精品视频 | 亚洲高清资源 | 亚洲视屏在线 | 99久久久精品 | 天天干天天色综合网 | 福利视频网| 免费观看久久 | 免费香蕉依人在线视频久 | 377p欧洲最大胆艺术 | 国产成人亚洲精品大帝 | 国产在线成人一区二区 | 亚洲成人在线播放视频 | 国产精品亚洲欧美日韩久久 | 99热久久只有精品6国产32 | 国内女高中生一级毛片 | 久久激情综合色丁香 | 99国产成人精品2021 | 狠狠色噜噜狠狠狠97影音先锋 | 亚洲va欧美va国产va天堂影 | 九色福利 | 日韩成人免费在线视频 | 欧美日韩亚洲国产精品一区二区 | 污影院| 成人国产精品毛片 | 免费看一毛一级毛片视频 | 久久97精品久久久久久久看片 | 久久精品欧美一区二区 | 国产探花视频在线观看 | 全部精品孕妇色视频在线 | 久久99精品久久久久久 | 免费观看毛片视频 | 亚洲免费福利 | 日本另类αv欧美另类aⅴ | 精品视频免费播放 | 狠狠色噜噜狠狠狠狠狠色综合久久 | 久热re这里只有精品视频 |