Hadoop系列之学习笔记(一)

HBase学习笔记(一)
1.HTable是HBase与用户接口交互的最核心的类.
org.apache.hadoop.hbase.client.HTable
2.HTable-->HTablePool
3.Bytes Class的常用方法有
1).byte[] toBytes(String s);
2).byte[] toBytes(boolean b);
3).byte[] toBytes(Long l);
4).String toString(final byte [] b);
5).static long toLong(byte[] bytes);
4.Put Class
构造时一般需要指定具体的行Key
5.HTable-->put(Put put)-->Put Class-->RowKey
6.HBaseConfiguration继承于Configuration
1).在初始化会根据系统配置找到hbase-default.xml与hbase-site.xml这二个文件并加载。
2).在加载文件后,可以通过config.set("属性名","值")来修改相关配置。
7.查询行的各版本
$hbase>scan 'test',{VERSIONS=>3}
系统默认只记录最近三次的版本(timestamp)内容.
8.KeyValue Class 有三个重要参数getFamily(),getQualifier(),getTimestamp()
描述了具体的数据模型。
9.HTable默认AutoFlush=true,可以修改为false,与之对应的方法是isAutoFlush(),flushCommits()
10.HTable中的put方法,可以直接put list内容,代码:
List<Put> puts=new ArrayList<Put>();
hTable.put(puts);
11.Get Class 示例代码:
Get aGet = new Get(Bytes.toBytes("1"));
aGet.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("列名"));
Result result = hTable.get(aGet);
12.Result Class 查询结果
13.Result result.getMap();取得数据集内的信息,包括Family,Qualifier,Timestamp内的内容;
14.result.getNoVersionMap();不包含版本信息的,相对结构要简单一些。

部分示例代码:
构造内容:

HTable table = null;
Configuration conf = HBaseConfiguration.create();
conf.addResource(new Path("/usr/java/hbase-0.94.2/conf/hbase-site.xml"));
// conf.set("hbase.zookeeper.property.clientPort", "2222");
// conf.set("hbase.zookeeper.quorum","jcnep5422");
try {
table = new HTable(conf, "CusActive");
} catch (IOException e) {
e.printStackTrace();
}

测试函数:

public void test01() {
String newColumn = "AAAA";
Get myGet = new Get(Bytes.toBytes("1"));
// 指定Get函数要查看的列内容,有点象 SQL中的Select列内容定义
myGet.addColumn(Bytes.toBytes("cf"), Bytes.toBytes(newColumn));
myGet.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("ID"));
Put put = new Put(Bytes.toBytes("1"));
Result result = null;
try {
put.add(Bytes.toBytes("cf"), Bytes.toBytes(newColumn), Bytes.toBytes("1981"));
table.put(put);
result = table.get(myGet);
if (!result.isEmpty()) {
KeyValue keyValue = result.getColumnLatest(Bytes.toBytes("cf"), Bytes.toBytes("ID"));
System.out.println("getFamily=" + Bytes.toString(keyValue.getFamily()));
System.out.println("getKey=" + Bytes.toString(keyValue.getKey()));
System.out.println("getRow=" + Bytes.toString(keyValue.getRow()));
System.out.println("getQualifier=" + Bytes.toString(keyValue.getQualifier()));

NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result.getMap();
Iterator<Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>>> it = map.entrySet().iterator();
while (it.hasNext()) {
Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> ii = it.next();
System.out.println("列簇=" + Bytes.toString(ii.getKey()));

NavigableMap<byte[], NavigableMap<Long, byte[]>> cfmap = ii.getValue();
Iterator<Entry<byte[], NavigableMap<Long, byte[]>>> cfit = cfmap.entrySet().iterator();
while (cfit.hasNext()) {
Entry<byte[], NavigableMap<Long, byte[]>> x1 = cfit.next();
System.out.println(" 列=" + Bytes.toString(x1.getKey()));
NavigableMap<Long, byte[]> qvmap = x1.getValue();
Iterator<Entry<Long, byte[]>> q1 = qvmap.entrySet().iterator();
while (q1.hasNext()) {
Entry<Long, byte[]> qv = q1.next();
System.out.print(" 版本=" + qv.getKey());
System.out.println("," + Bytes.toString(qv.getValue()));
}
}
}
List<KeyValue> kvlist= result.list();
for (KeyValue keyValue2 : kvlist) {
System.out.print(Bytes.toString( keyValue2.getFamily())+",");
System.out.print(Bytes.toString(keyValue2.getQualifier())+",");
System.out.print(keyValue2.getTimestamp()+",");
System.out.println(Bytes.toString(keyValue2.getValue()));
}
byte[] v1 = result.getValue(Bytes.toBytes("cf"), Bytes.toBytes(newColumn));
byte[] v2 = result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("ID"));
System.out.println(newColumn + "=" + Bytes.toString(v1));
System.out.println(newColumn + "=" + Bytes.toString(v2));
} else {
System.out.println("对象为空");
}
} catch (IOException e) {
e.printStackTrace();
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值