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

首先上代码,这个太特么的坑爹了!代码在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; }
}

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

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

具体干活代码如下:

 private void SaveStockDataToHbase(object state)
{
StockEntity se = state as StockEntity; // Insert data into the HBase table.
string rowKey = Guid.NewGuid().ToString(); CellSet cellSet = new CellSet();
CellSet.Row cellSetRow = new CellSet.Row { key = Encoding.UTF8.GetBytes(rowKey) };
cellSet.rows.Add(cellSetRow); Type t = typeof(StockEntity); foreach (string colname in stockEntityColumns)
{
var pi = t.GetProperty(colname);
object val = pi.GetValue(se); Cell value = new Cell { column = Encoding.UTF8.GetBytes("charju:" + colname), data = Encoding.UTF8.GetBytes(Convert.ToString(val)) };
cellSetRow.values.Add(value);
} try
{
hbaseClient.StoreCells(hbaseStockTableName, cellSet);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

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/

HBase初探的更多相关文章

  1. python搞搞大数据之hbase——初探

    使用python链接mysql读入一个表并把它再写到hbase 里去(九头蛇万岁) 先声明一下需要用的库: 俩!!: happybase    (写这个的老哥真的happy) pymysql 建议使用 ...

  2. HBase 监控 | HBase Metrics 初探(一)

    前言:对于任意一个系统而言,做好监控都是非常重要的,HBase也不例外.经常,我们会从JMX中获取相关指标来做展示.对HBase进行监控,那这些指标是怎么生成的呢?如果你想自定义自己的监控指标又该怎么 ...

  3. YCSB之HBase性能测试

    1.YCSB背景 YCSB,全称为“Yahoo!Cloud Serving Benchmark”,是雅虎开发的用来对云服务进行基础测试的工具,其内部涵盖了常见的NoSQL数据库产品,如Cassandr ...

  4. Hadoop初探

    本文转自:https://blog.csdn.net/column/details/14334.html 前言 Hadoop是什么? 用百科上的话说:“Hadoop是一个由Apache基金会所开发的分 ...

  5. 沉淀再出发:kafka初探

    沉淀再出发:kafka初探 一.前言 从我们接触大数据开始,可能绕在耳边的词汇里面出现的次数越来越多的就包括kfaka了.kafka的设计初衷是希望作为一个统一的信息收集平台,能够实时的收集反馈信息, ...

  6. OceanBase 架构初探

    OceanBase 架构初探 原创衣舞晨风 发布于2018-11-13 08:44:14 阅读数 1417  收藏 展开 1.设计思路 OceanBase的目标是支持数百TB的数据量以及数十万TPS. ...

  7. Mapreduce的文件和hbase共同输入

    Mapreduce的文件和hbase共同输入 package duogemap;   import java.io.IOException;   import org.apache.hadoop.co ...

  8. Redis/HBase/Tair比较

    KV系统对比表 对比维度 Redis Redis Cluster Medis Hbase Tair 访问模式    支持Value大小 理论上不超过1GB(建议不超过1MB) 理论上可配置(默认配置1 ...

  9. Hbase的伪分布式安装

    Hbase安装模式介绍 单机模式 1> Hbase不使用HDFS,仅使用本地文件系统 2> ZooKeeper与Hbase运行在同一个JVM中 分布式模式– 伪分布式模式1> 所有进 ...

随机推荐

  1. SQL Server调优系列进阶篇(查询优化器的运行方式)

    前言 前面我们的几篇文章介绍了一系列关于运算符的基础介绍,以及各个运算符的优化方式和技巧.其中涵盖:查看执行计划的方式.几种数据集常用的连接方式.联合运算符方式.并行运算符等一系列的我们常见的运算符. ...

  2. Tomcat源码分析之—容器整体结构

    Tomcat有多个容器组成,而Container也就是容器与Connecter连接器是Tomcat最核心的两个模块,Connecter连接器接收客户端的请求,并根据客户端的请求传递给Container ...

  3. flock — 轻便的咨询文件锁定

    bool flock  ( resource $handle  , int $operation  [, int &$wouldblock  ] ) handle  文件系统指针,是典型地由 ...

  4. Android Paint和Color类绘画实例

    要绘图,首先得调整画笔,待画笔调整好之后,再将图像绘制到画布上,这样才可以显示在手机屏幕上.Android 中的画笔是 Paint类,Paint 中包含了很多方法对其属性进行设置,主要方法如下: se ...

  5. 2016.11.17 NOI plus day0

    今天很乱乱乱乱 根本不想写代码 玩了一早上了 昨晚失眠了 今天又懵逼了 中午就要走了 明天就要考试了 考完试回来就要补文化课了 现在我的内心很平静 因为已经紧张的冻结了 你知道什么叫彷徨么? 机房里的 ...

  6. [No000061]"别人"凭什么要帮你?&理解中国人的人际和谐&外人、自己人与另一半

    你出身平凡家庭:你毕业于普通大学:你没有田晓霞这样的妻子或者普京这样的丈夫:在权力.金钱乃至能力积累上,你才刚刚上路.你很年轻,你渴望成功,那么,"别人"凭什么帮你? " ...

  7. 渗透中Necat的另类用法

    Necat 是一个伟大而实用的用于 TCP 和 UPD 网络连接协议的读写程序.同时 Necat 也被誉为网络中的瑞士军刀,在许多黑客教程中 Necat 也被广泛使用.Necat 最常见用途是设置反向 ...

  8. Ubuntu的Mysql指南

    安装MySQL sudo apt-get install mysql-server 这个应该很简单了,而且我觉得大家在安装方面也没什么太大问题,所以也就不多说了,下面我们来讲讲配置. 配置MySQL ...

  9. 今天看了shell大神的写的一个统计脚本

    通过nginx日志统计接口耗时排行 grep '/bigbox?' access_log | awk '{print $7"&process="$NF}'| sed -r ...

  10. Java集合系列:-----------06List的总结(LinkedList,ArrayList等使用场景和性能分析)

    现在,我们再回头看看总结一下List.内容包括:第1部分 List概括第2部分 List使用场景第3部分 LinkedList和ArrayList性能差异分析第4部分 Vector和ArrayList ...