HBase初探

string hbaseCluster = "https://charju.azurehdinsight.net";
string hadoopUsername = "账户名字";
string hadoopPassword = "密码";

ClusterCredentials creds = new ClusterCredentials(new Uri(hbaseCluster), hadoopUsername, hadoopPassword);
var hbaseClient = new HBaseClient(creds);

// No response when GetVersion
var version = hbaseClient.GetVersion();

Console.WriteLine(Convert.ToString(version));
View Code

首先上代码,这个太特么的坑爹了!代码在winform中是无法运行滴!!!在命令行应用中是可以的!!!(浪费了老子好几天的时间……)

在winform中,通过windbg调试,发现在GetVersion的时候,主线程起了一个Task,然后等待Task的完成。在Task运行初期(大概1分钟内),会有另外一个线程,在WaitHandle,然后等一段时间,该线程消失。主线程中开始Retries调用,然后,就没有然后了……

 

Anyway,命令行中,代码是OK的。

我的例子,是利用新浪上的API来得到股票信息,比如说:http://hq.sinajs.cn/list=sz000977,sh600718,我每秒钟调用一次,然后这些数据刷到hbase里面去。

 

股票的实体类定义

public class StockEntity
    {
        public string Name { get; set; }
        public double TodayOpeningPrice { get; set; }
        public double YesterdayClosingPrice { get; set; }
        public double CurrentPrice { get; set; }
        public double TodayMaxPrice { get; set; }
        public double TodayMinPrice { get; set; }
        public double BidPriceBuy { get; set; }
        public double BidPriceSell { get; set; }
        public int FixtureNumber { get; set; }
        public double FixtureAmount { get; set; }
        public int Buy1Number { get; set; }
        public double Buy1Price { get; set; }
        public int Buy2Number { get; set; }
        public double Buy2Price { get; set; }
        public int Buy3Number { get; set; }
        public double Buy3Price { get; set; }
        public int Buy4Number { get; set; }
        public double Buy4Price { get; set; }
        public int Buy5Number { get; set; }
        public double Buy5Price { get; set; }
        public int Sell1Number { get; set; }
        public double Sell1Price { get; set; }
        public int Sell2Number { get; set; }
        public double Sell2Price { get; set; }
        public int Sell3Number { get; set; }
        public double Sell3Price { get; set; }
        public int Sell4Number { get; set; }
        public double Sell4Price { get; set; }
        public int Sell5Number { get; set; }
        public double Sell5Price { get; set; }

        public DateTime TransactionTime { get; set; }
    }
View Code

 

数据拉下来之后,新开一个线程,让它去写到hbase中。

ThreadPool.QueueUserWorkItem(new WaitCallback(SaveStockDataToHbase), se);

 

具体干活代码如下:

 1 private void SaveStockDataToHbase(object state)
 2         {
 3             StockEntity se = state as StockEntity;
 4 
 5             // Insert data into the HBase table.
 6             string rowKey = Guid.NewGuid().ToString();
 7 
 8             CellSet cellSet = new CellSet();
 9             CellSet.Row cellSetRow = new CellSet.Row { key = Encoding.UTF8.GetBytes(rowKey) };
10             cellSet.rows.Add(cellSetRow);
11 
12 
13             Type t = typeof(StockEntity);
14 
15             foreach (string colname in stockEntityColumns)
16             {
17                 var pi = t.GetProperty(colname);
18                 object val = pi.GetValue(se);
19 
20                 Cell value = new Cell { column = Encoding.UTF8.GetBytes("charju:" + colname), data = Encoding.UTF8.GetBytes(Convert.ToString(val)) };
21                 cellSetRow.values.Add(value);
22             }
23 
24             try
25             {
26                 hbaseClient.StoreCells(hbaseStockTableName, cellSet);
27             }
28             catch (Exception ex)
29             {
30                 Console.WriteLine(ex.Message);
31             }
32         }

6~10行,是生成一个新Row。20行,是反射实体类的每一个Property 定义,来取对应的值(否则我要写一坨重复的代码)。21行,把对应的该列数据写到这个行上。

26行,就是真正的放到hbase中。

 

上面20行,你可能会注意到:charju,这是我的column family的名字。回过头来,看看hbase中的表是怎么建立的

string hbaseCluster = "https://charju.azurehdinsight.net";
string hadoopUsername = "<your name>";
string hadoopPassword = "<your password>";
string hbaseStockTableName = "StockInformation";
HBaseClient hbaseClient;

public void CreateHbaseTable()
{

            // Create a new HBase table. - StockInformation
            TableSchema stockTableSchema = new TableSchema();
            stockTableSchema.name = hbaseStockTableName;
            stockTableSchema.columns.Add(new ColumnSchema() { name = "charju" });
            hbaseClient.CreateTable(stockTableSchema);

}

 

而hbaseClient的实例化,是在这里:

ClusterCredentials creds = new ClusterCredentials(new Uri(hbaseCluster), hadoopUsername, hadoopPassword);
hbaseClient = new HBaseClient(creds);

 

数据写入后,我们可以有几个方式来。一是在hbase中配置一下,允许RDP,然后remote上去跑hbase shell命令,可惜我虚机里面RDP总失败,不知道为啥。第二种方式,就是用HIVE来查。

连接到hbase的网站后,在hive editor那个界面中,先创建对应的表

CREATE EXTERNAL TABLE StockInformation(rowkey STRING, TodayOpeningPrice STRING, YesterdayClosingPrice STRING, CurrentPrice STRING, TodayMaxPrice STRING, TodayMinPrice STRING, BidPriceBuy STRING, BidPriceSell STRING, FixtureNumber STRING, FixtureAmount STRING, Buy1Number STRING, Buy1Price STRING, Buy2Number STRING, Buy2Price STRING, Buy3Number STRING, Buy3Price STRING, Buy4Number STRING, Buy4Price STRING, Buy5Number STRING, Buy5Price STRING, Sell1Number STRING, Sell1Price STRING, Sell2Number STRING, Sell2Price STRING, Sell3Number STRING, Sell3Price STRING, Sell4Number STRING, Sell4Price STRING, Sell5Number STRING, Sell5Price STRING, TransactionTime STRING)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ('hbase.columns.mapping' = ':key,charju:TodayOpeningPrice ,charju:YesterdayClosingPrice ,charju:CurrentPrice ,charju:TodayMaxPrice ,charju:TodayMinPrice ,charju:BidPriceBuy ,charju:BidPriceSell ,charju:FixtureNumber ,charju:FixtureAmount ,charju:Buy1Number ,charju:Buy1Price ,charju:Buy2Number ,charju:Buy2Price ,charju:Buy3Number ,charju:Buy3Price ,charju:Buy4Number ,charju:Buy4Price ,charju:Buy5Number ,charju:Buy5Price ,charju:Sell1Number ,charju:Sell1Price ,charju:Sell2Number ,charju:Sell2Price ,charju:Sell3Number ,charju:Sell3Price ,charju:Sell4Number ,charju:Sell4Price ,charju:Sell5Number ,charju:Sell5Price ,charju:TransactionTime')
TBLPROPERTIES ('hbase.table.name' = 'StockInformation');

创建成功后,然后就可以跑SQL了,比如说:

select * from StockInformation where buy1number=9800 order by transactiontime

今天小浪的最大一笔买入。当然,类似于select count(0) 之类的更OK了。

 

 

有用的连接:

https://azure.microsoft.com/en-us/documentation/articles/hdinsight-hbase-tutorial-get-started/

转载于:https://www.cnblogs.com/juqiang/p/4748632.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Docker HBase 是 Docker 容器化技术与 Apache HBase 数据存储系统结合的一种应用。HBase 是一个分布式、列式、可伸缩的 NoSQL 数据库,主要用于大规模数据存储和实时查询。将 HBase 安装在 Docker 镜像中,可以简化部署流程,使得开发者可以在本地或生产环境中快速启动并管理 HBase 实例,特别是对于那些需要频繁迭代和测试的大数据场景非常有用。 具体来说,使用 Docker HBase 的好处包括: 1. **快速部署**:Docker 提供了一种轻量级的方式来打包软件及其依赖环境,这使得部署 HBase 变得更快且一致。 2. **资源隔离**:每个 HBase 容器都是独立运行的,这意味着它们之间不会相互影响,有助于更好地管理资源。 3. **易于扩展**:如果需要增加更多的处理能力,可以通过增加容器实例来水平扩展 HBase。 4. **一致性保证**:由于 Docker 的镜像模式,HBase 的环境配置保持一致,减少了配置问题。 5. **开发环境统一**:开发人员可以在本地开发环境中使用相同的 Docker 镜像构建和测试 HBase 应用。 要开始使用 Docker HBase,你需要做以下几步: 1. **安装 Docker**:确保你的机器上已经安装了 Docker 并运行正常。 2. **查找 Docker HBase 镜像**:在 Docker Hub 上搜索 "hbase" 或者 "apache/hbase",选择官方镜像或者适合你的版本。 3. **运行容器**:使用 `docker run` 命令启动 HBase 容器,并指定所需的参数,如端口映射、持久卷等。 4. **连接到 HBase**:通过 Docker 集成的工具(如 `docker exec`)或者专门的客户端工具(如 HBase shell)连接到容器内部的 HBase 服务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值