设为首页 收藏本站
查看: 646|回复: 0

[经验分享] Hadoop系列之学习笔记(一)

[复制链接]

尚未签到

发表于 2016-12-11 07:47:27 | 显示全部楼层 |阅读模式
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();
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-312494-1-1.html 上篇帖子: hadoop和传统rdbms的比较(翻译) 下篇帖子: hadoop集群目录配置与注意事项
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表