.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

package cn.com.gzkit.hbase; 
 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.hbase.HBaseConfiguration; 
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.Result; 
import org.apache.hadoop.hbase.client.ResultScanner; 
import org.apache.hadoop.hbase.client.Scan; 
import org.apache.hadoop.hbase.client.Put; 
import org.apache.hadoop.hbase.util.Bytes; 
 
public class HBaseBasic {    
    private static Configuration conf = null;         
    static { 
        Configuration HBASE_CONFIG = new Configuration(); 
        //hbase/conf/hbase-site.xmlhbase.zookeeper.quorum  
        HBASE_CONFIG.set("hbase.zookeeper.quorum", "192.168.1.1");         
        //hbase/conf/hbase-site.xmlhbase.zookeeper.property.clientPort 
        HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181"); 
        conf = HBaseConfiguration.create(HBASE_CONFIG);
    }     
 
 
    public static void creatTable(String tableName, String[] familys) throws Exception { 
        HBaseAdmin admin = new HBaseAdmin(conf);         
        if (admin.tableExists(tableName)) { 
            System.out.println("table already exists!");         
        } else { 
            HTableDescriptor tableDesc = new HTableDescriptor(tableName); 
            for(int i=0; i<familys.length; i++){ 
                tableDesc.addFamily(new HColumnDescriptor(familys[i]));             
            }
            
            admin.createTable(tableDesc); 
            System.out.println("create table " + tableName + " ok.");         
        }      
    }        
    
    public static void deleteTable(String tableName) throws Exception {        
        try { 
            HBaseAdmin admin = new HBaseAdmin(conf);         
            admin.disableTable(tableName);         
            admin.deleteTable(tableName); 
            System.out.println("delete table " + tableName + " ok.");        
        } catch (MasterNotRunningException e) {         
            e.printStackTrace(); 
        } catch (ZooKeeperConnectionException e) {         
            e.printStackTrace();        
        }     
    } 
    
    public static void addRecord (String tableName, String rowKey, String family, String qualifier, String value)       throws Exception{      
        try { 
            HTable table = new HTable(conf, tableName);       
            Put put = new Put(Bytes.toBytes(rowKey)); 
            put.add(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value)); 
            table.put(put); 
            System.out.println("insert recored " + rowKey + " to table " + tableName +" ok."); 
        } catch (IOException e) {       
            e.printStackTrace(); 
        }     
    } 
 
    public static void delRecord (String tableName, String rowKey) throws IOException{ 
        HTable table = new HTable(conf, tableName);      
        List list = new ArrayList(); 
        Delete del = new Delete(rowKey.getBytes());      
        list.add(del); 
        table.delete(list); 
        System.out.println("del recored " + rowKey + " ok.");     
    }         
    
    public static void getOneRecord (String tableName, String rowKey) throws IOException{ 
        HTable table = new HTable(conf, tableName);      
        Get get = new Get(rowKey.getBytes());      
        Result rs = table.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(kv.getTimestamp() + " " );       
            System.out.println(new String(kv.getValue()));      
        }     
    }         
    
    public static void getAllRecord (String tableName) {      
        try{ 
            HTable table = new HTable(conf, tableName);           
            Scan s = new Scan(); 
            ResultScanner ss = table.getScanner(s);           
            for(Result r:ss){ 
                for(KeyValue kv : r.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(kv.getTimestamp() + " "); 
                    System.out.println(new String(kv.getValue()));               
                }           
            } 
        } catch (IOException e){       
            e.printStackTrace(); 
        }     
    }    
    
    public static void  main (String [] agrs) {   
        try { 
            String tablename = "student"; 
            String[] familys = {"id", "name","score"};    //HBaseBasic.creatTable(tablename, familys);     
            //add record zkb 
            //HBaseBasic.addRecord(tablename,"2","id","","095832");    //HBaseBasic.addRecord(tablename,"2","name","","zmac");    //HBaseBasic.addRecord(tablename,"2","score","math","97");    //HBaseBasic.addRecord(tablename,"2","score","chinese","87");    //HBaseBasic.addRecord(tablename,"2","score","english","85");    //add record  baoniu     
            System.out.println("===========get one record========");
            HBaseBasic.getOneRecord(tablename, "2");     
            System.out.println("===========show all record========");    
            HBaseBasic.getAllRecord(tablename);     
            System.out.println("===========del one record========");    
            HBaseBasic.delRecord(tablename, "2");    
            HBaseBasic.getAllRecord(tablename);     
        } catch (Exception e) {    
            e.printStackTrace();   
        }  
    } 
    
    
} 
 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

运行会,你将看到首先创建数据库student,其字段为
id,name,score{english,math,chinese},然后向该数据表插入数据,再查询记录,删除记录等

hbase使用-java操作的更多相关文章

  1. HBase(2) Java 操作 HBase 教程

    目录 一.简介 二.hbase-client 引入 三.连接操作 四.表操作 五.运行测试 FAQ 参考文档 一.简介 在上一篇文章 HBase 基础入门 中,我们已经介绍了 HBase 的一些基本概 ...

  2. HBase的java操作,最新API。(查询指定行、列、插入数据等)

    关于HBase环境搭建和HBase的原理架构,请见笔者相关博客. 1.HBase对java有着较优秀的支持,本文将介绍如何使用java操作Hbase. 首先是pom依赖: <dependency ...

  3. 【hbase】——Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

    1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...

  4. Java 操作 HBase 教程

    Java 操作 HBase 教程 一.简介 二.hbase-client 引入 三.连接操作 四.表操作 五.运行测试 相关博文原文地址: 博客园:美码师:HBase(2) Java 操作 HBase ...

  5. Hbase深入学习(六) Java操作HBase

    Hbase深入学习(六) ―― Java操作HBase 本文讲述如何用hbase shell命令和hbase java api对hbase服务器进行操作. 先看以下读取一行记录hbase是如何进行工作 ...

  6. Java操作hbase总结

    用过以后,总得写个总结,不然,就忘喽. 一.寻找操作的jar包. java操作hbase,首先要考虑到使用hbase的jar包. 因为咱装的是CDH5,比较方便,使用SecureCRT工具,远程连接到 ...

  7. HBase篇--HBase操作Api和Java操作Hbase相关Api

    一.前述. Hbase shell启动命令窗口,然后再Hbase shell中对应的api命令如下. 二.说明 Hbase shell中删除键是空格+Ctrl键. 三.代码 1.封装所有的API pa ...

  8. java操作Hbase实例

    所用HBase版本为1.1.2,hadoop版本为2.4 /* * 创建一个students表,并进行相关操作 */ import java.io.IOException; import java.u ...

  9. HBase之四--(1):Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

    1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...

随机推荐

  1. 受限波兹曼机导论Introduction to Restricted Boltzmann Machines

    Suppose you ask a bunch of users to rate a set of movies on a 0-100 scale. In classical factor analy ...

  2. Mybatis中配置Mapper的方法

    在这篇文章中我主要想讲一下Mybatis配置文件中mappers元素的配置.关于基础部分的内容可以参考http://haohaoxuexi.iteye.com/blog/1333271. 我们知道在M ...

  3. WIN7 64位系统搭建WINCE6.0系统遇到的问题

    WIN7 64位系统搭建WINCE6.0系统遇到的问题 安装顺序如下: .先装Visual Studio2005: .安装Visual Studio2005 Service Pack 1: .安装Vi ...

  4. MemSQL start[c]up Round 2 - online version(DP)

    只有小写字母 那>=2600的直接找单字母串长度大于等于100的就可以了 <2600 的dp找最长回文串 #include <iostream> #include<cst ...

  5. 对象不支持“attachEvent”属性或方法的解决办法

    有些脚本在IE11下执行会报错误: 对象不支持“attachEvent”属性或方法 解决办法 解决办法:把attachEvent改为addEventListener即可

  6. PHP查询数据库中满足条件的记录条数(二种实现方法)

    在需要输出网站用户注册数或者插入数据之前判断是否有重复记录时,就需要获取满足条件的MySQL查询的记录数目,接下来介绍两种查询统计方法,感兴趣的朋友可以了解下啊,或许对你有所帮助     在需要输出网 ...

  7. Make AngularJS $http service behave like jQuery.ajax()(转)

    There is much confusion among newcomers to AngularJS as to why the $http service shorthand functions ...

  8. jQuery避免$符和其他JS库冲突的方法对比

    1.如果jquery库在第三方库之后引用.这个时候jquery库会占用$. 解决办法:剔除$符号的使用权. <script type="text/javascript" sr ...

  9. Hibernate向MySQL插入中文数据--乱码解决

    <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/exam?useUnicod ...

  10. MySql表中key的区别

    我们看到Key那一栏,可能会有4种值,即'啥也没有','PRI','UNI','MUL'1. 如果Key是空的, 那么该列值的可以重复, 表示该列没有索引, 或者是一个非唯一的复合索引的非前导列2. ...