威凡网全力打造:网页编程、软件开发编程、平面设计、服务器端开发、操作系统等在线学习平台!学编程,上威凡网!
PHP教程>> PHP基础 PHP技巧 PHP实例 PHP文摘 PHP模板 PHP总结
当前位置:首页 > PHP教程 > PHP总结
上一节 下一节
 JAVA操作Hbase基础例子

package com.cma.hbase.test; import java.io.bufferedinputstream; import java.io.bufferedreader; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.filereader; import java.io.ioexception

package com.cma.hbase.test;

import java.io.bufferedinputstream;
import java.io.bufferedreader;
import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.filereader;
import java.io.ioexception;
import java.io.inputstream;
import java.io.inputstreamreader;
import java.io.reader;
import java.util.arraylist;
import java.util.list;

import org.apache.commons.io.fileutils;
import org.apache.commons.lang.stringutils;
import org.apache.hadoop.conf.configuration;
import org.apache.hadoop.hbase.hcolumndescriptor;
import org.apache.hadoop.hbase.htabledescriptor;
import org.apache.hadoop.hbase.keyvalue;
import org.apache.hadoop.hbase.masternotrunningexception;
import org.apache.hadoop.hbase.zookeeperconnectionexception;
import org.apache.hadoop.hbase.client.delete;
import org.apache.hadoop.hbase.client.get;
import org.apache.hadoop.hbase.client.hbaseadmin;
import org.apache.hadoop.hbase.client.htable;
import org.apache.hadoop.hbase.client.put;
import org.apache.hadoop.hbase.client.result;
import org.apache.hadoop.hbase.util.bytes;

import com.cma.hbase.constants;
import com.cma.hbase.entity.datamodel;
import com.cma.hbase.tools.hbaseutils;

public class hbasetest {

?? ?public static void main(string[] args) {
?? ??? ?创建一张表
?? ??? ?createtable("hello_baby",new string[]{"code","ws","wd","t","ps","rh","vis","r"});
?? ??? ?写入一条数据
?? ??? ?writerecord("hello_baby","row1","code","","code");
?? ??? ?writerecord("hello_baby","row1","ws","","ws");
?? ??? ?writerecord("hello_baby","row1","wd","","wd");
?? ??? ?writerecord("hello_baby","row1","t","","t");
?? ??? ?writerecord("hello_baby","row1","ps","","ps");
?? ??? ?writerecord("hello_baby","row1","rh","","rh");
?? ??? ?writerecord("hello_baby","row1","vis","","vis");
?? ??? ?writerecord("hello_baby","row1","r","","r");
?? ??? ?写入一组数据
?? ??? ?writerecordlist("hello_baby");
?? ??? ?//查询出一条数据
?? ??? ?getrecord("hello_baby","row1");
?? ??? ?删除一行
?? ??? ?deleterecord("hello_baby","row1");
?? ??? ?删除一张表
?? ??? ?droptable("hello_baby");
?? ??? ?清空一张表
?? ??? ?cleartable("hello_baby");
?? ?}

?? ?/**
?? ? * 清空一张表
?? ? * @param string
?? ? */
?? ?private static void cleartable(string tablename) {
?? ??? ?configuration cfg = hbaseutils.getcfg();
?? ??? ?
?? ??? ?try {
?? ??? ??? ?hbaseadmin admin = new hbaseadmin(cfg);
?? ??? ??? ?
?? ??? ?} catch (masternotrunningexception e) {
?? ??? ??? ?e.printstacktrace();
?? ??? ?} catch (zookeeperconnectionexception e) {
?? ??? ??? ?e.printstacktrace();
?? ??? ?} catch (ioexception e) {
?? ??? ??? ?e.printstacktrace();
?? ??? ?}finally{
?? ??? ??? ?system.out.println("tablename: "+tablename+" drop over!");
?? ??? ?}
?? ?}

?? ?/**
?? ? * 写入一组数据
?? ? * @param tablename
?? ? */
?? ?private static void writerecordlist(string tablename) {
?? ??? ?long start = system.currenttimemillis();
?? ??? ?configuration cfg = hbaseutils.getcfg();
?? ??? ?try {
?? ??? ??? ?htable htable = new htable(cfg,tablename);
?? ??? ??? ?list puts = getputs();
?? ??? ??? ?system.out.println(puts.size());
?? ??? ??? ?htable.put(puts);
?? ??? ?} catch (ioexception e) {
?? ??? ??? ?e.printstacktrace();
?? ??? ?}finally{
?? ??? ??? ?system.out.println("tablename:"+tablename+" write over!");
?? ??? ?}
?? ??? ?long end = system.currenttimemillis();
?? ??? ?system.out.println("cost time: "+(end -start));
?? ?}

?? ?private static list getputs() {
?? ??? ?list putlist = new arraylist();
?? ??? ?list putrecord = null;
?? ??? ?try {
?? ??? ??? ?list lines = fileutils.readlines(new file("/home/guest/data/201307310800.csv"));
?? ??? ??? ?for(int i = 1 ;i < lines.size();i++){
?? ??? ??? ??? ?putrecord = getputsbyline(lines.get(i));
?? ??? ??? ??? ?putlist.addall(putrecord);
?? ??? ??? ?}
?? ??? ?} catch (ioexception e) {
?? ??? ??? ?e.printstacktrace();
?? ??? ?}
?? ??? ?return putlist;
?? ?}

?? ?/**
?? ? * 获得一组put
?? ? * @param line
?? ? * @return
?? ? */
?? ?private static list getputsbyline(string line) {
?? ??? ?list puts = new arraylist();
?? ??? ?put put = null;
?? ??? ?
?? ??? ?if(stringutils.isnotblank(line)){
?? ??? ??? ?string[] columns = line.split(",");
?? ??? ??? ?string[] families = constants.families;
?? ??? ??? ?string rowkey = "201307310800"+columns[0];
?? ??? ??? ?for(int i = 0;i < columns.length;i++){
?? ??? ??? ??? ?string family = families[i];
?? ??? ??? ??? ?string qualifier = "";
?? ??? ??? ??? ?string value = columns[i];
?? ??? ??? ??? ?put = getput(rowkey,family,qualifier,value);
?? ??? ??? ??? ?puts.add(put);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return puts;
?? ?}

?? ?/**
?? ? * 组装一个put
?? ? * @param rowkey
?? ? * @param family
?? ? * @param qualifier
?? ? * @param value
?? ? * @return
?? ? */
?? ?private static put getput(string rowkey, string family, string qualifier,
?? ??? ??? ?string value) {
?? ??? ?put put = new put(bytes.tobytes(rowkey));
?? ??? ?put.add(bytes.tobytes(family), bytes.tobytes(qualifier), bytes.tobytes(value));
?? ??? ?return put;
?? ?}

?? ?/**
?? ? * 查询出一条数据
?? ? * @param tablename
?? ? * @param rowkey
?? ? */
?? ?private static void getrecord(string tablename, string rowkey) {
?? ??? ?configuration cfg = hbaseutils.getcfg();
?? ??? ?try {
?? ??? ??? ?htable htable = new htable(cfg,tablename);
?? ??? ??? ?get get = new get(bytes.tobytes(rowkey));
?? ??? ??? ?result rs = htable.get(get);
?? ??? ??? ?
?? ??? ??? ?for(keyvalue kv : rs.raw()){
?? ??? ??? ??? ?system.out.print(new string(kv.getrow())+" *** ");
?? ??? ??? ??? ?system.out.print(new string(kv.getfamily())+" *** ");
?? ??? ??? ??? ?system.out.print(new string(kv.getqualifier())+" *** ");
?? ??? ??? ??? ?system.out.print(new string(kv.getvalue()));
?? ??? ??? ??? ?system.out.println();
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ?} catch (ioexception e) {
?? ??? ??? ?e.printstacktrace();
?? ??? ?}
?? ?}

?? ?/**
?? ? * 删除一张表
?? ? * @param tablename
?? ? */
?? ?private static void droptable(string tablename) {
?? ??? ?configuration cfg = hbaseutils.getcfg();
?? ??? ?
?? ??? ?try {
?? ??? ??? ?hbaseadmin admin = new hbaseadmin(cfg);
?? ??? ??? ?admin.disabletable(tablename);
?? ??? ??? ?admin.deletetable(tablename);
?? ??? ?} catch (masternotrunningexception e) {
?? ??? ??? ?e.printstacktrace();
?? ??? ?} catch (zookeeperconnectionexception e) {
?? ??? ??? ?e.printstacktrace();
?? ??? ?} catch (ioexception e) {
?? ??? ??? ?e.printstacktrace();
?? ??? ?}finally{
?? ??? ??? ?system.out.println("tablename: "+tablename+" drop over!");
?? ??? ?}
?? ?}

?? ?/**
?? ? * 删除一行
?? ? * @param tablename
?? ? * @param rowkey
?? ? */
?? ?private static void deleterecord(string tablename, string rowkey) {
?? ??? ?configuration cfg = hbaseutils.getcfg();
?? ??? ?
?? ??? ?try {
?? ??? ??? ?htable htable = new htable(cfg,tablename);
?? ??? ??? ?delete del = new delete(bytes.tobytes(rowkey));
?? ??? ??? ?htable.delete(del);
?? ??? ?} catch (ioexception e) {
?? ??? ??? ?e.printstacktrace();
?? ??? ?}finally{
?? ??? ??? ?system.out.println("rowkey: "+rowkey+" delete over!");
?? ??? ?}
?? ?}

?? ?/**
?? ? * 存储一列
?? ? * @param tablename
?? ? * @param rowkey
?? ? * @param family
?? ? * @param qualifier
?? ? * @param value
?? ? */
?? ?private static void writerecord(string tablename,string rowkey,string family,string qualifier,string value) {
?? ??? ?configuration cfg = hbaseutils.getcfg();
?? ??? ?try {
?? ??? ??? ?htable htable = new htable(cfg,tablename);
?? ??? ??? ?put put = new put(bytes.tobytes(rowkey));
?? ??? ??? ?put.add(bytes.tobytes(family), bytes.tobytes(qualifier), bytes.tobytes(value));
?? ??? ??? ?htable.put(put);
?? ??? ??? ?
?? ??? ?} catch (ioexception e) {
?? ??? ??? ?e.printstacktrace();
?? ??? ?}finally{
?? ??? ??? ?system.out.println("family: "+family+" put over!");
?? ??? ?}
?? ?}

?? ?/**
?? ? * 创建一张表
?? ? * @param tablename
?? ? * @param families
?? ? */
?? ?private static void createtable(string tablename,string[] families) {
?? ??? ?hbaseadmin admin = null;
?? ??? ?try {
?? ??? ??? ?configuration cfg = hbaseutils.getcfg();
?? ??? ??? ?system.out.println(cfg);
?? ??? ??? ?admin = new hbaseadmin(cfg);
?? ??? ??? ?if(admin.tableexists(tablename)){
?? ??? ??? ??? ?system.out.println("表:"+tablename+" 已经存在!");
?? ??? ??? ?}else{
?? ??? ??? ??? ?htabledescriptor tabledesc = new htabledescriptor(tablename);
?? ??? ??? ??? ?for(string column : families){
?? ??? ??? ??? ??? ?tabledesc.addfamily(new hcolumndescriptor(column));
?? ??? ??? ??? ?}
?? ??? ??? ??? ?admin.createtable(tabledesc);
?? ??? ??? ?}
?? ??? ?} catch (masternotrunningexception e) {
?? ??? ??? ?e.printstacktrace();
?? ??? ?} catch (zookeeperconnectionexception e) {
?? ??? ??? ?e.printstacktrace();
?? ??? ?} catch (ioexception e) {
?? ??? ??? ?e.printstacktrace();
?? ??? ?}finally{
?? ??? ??? ?system.out.println("over!");
?? ??? ?}
?? ?}
?? ?

}

申明:本教程内容由威凡网编辑整理并提供IT程序员分享学习,如文中有侵权行为,请与站长联系(QQ:254677821)!
上一节 下一节
相关教程  
其他教程  
PHP基础
PHP技巧
PHP实例
PHP文摘
PHP模板
PHP总结

违法和不良信息举报中心】邮箱:254677821@qq.com
Copyright©威凡网 版权所有 苏ICP备2023020142号
站长QQ:254677821