.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. PCL—低层次视觉—点云分割(最小割算法)

    1.点云分割的精度 在之前的两个章节里介绍了基于采样一致的点云分割和基于临近搜索的点云分割算法.基于采样一致的点云分割算法显然是意识流的,它只能割出大概的点云(可能是杯子的一部分,但杯把儿肯定没分割出 ...

  2. JS模块化编程

    AMD:异步模块定义,适合客户端环境,不会阻塞运行.客户端受网络影响比较大. CommonJs:适用于服务器端规范,可以同步加载,只受硬盘读写的影响.

  3. 客户视角:Oracle ETL工具ODI

    客户视角:Oracle ETL工具ODI 数据集成已成为企业在追求市场份额中的关键技术组件,与依靠手工编码的方式不同,越来越多的企业选择完整的数据集成解决方案来支持其IT战略,从大数据分析到云平台的集 ...

  4. Codeforces 672

    题目链接:http://codeforces.com/contest/672/problem A. Summer Camp(打表) 题意:123456789...一串字符串,问第n个是什么数字. 塞一 ...

  5. 函数 page_dir_get_n_heap

    查看某page中含有的记录个数 #define PAGE_N_HEAP 4 /* number of records in the heap, bit =flag: new-style compact ...

  6. 函数xdes_init

    /**********************************************************************//** Inits an extent descript ...

  7. asp.net正则表达式过滤标签和数据提取

    无论什么语言,正则表达式的处理方法都是非常灵活.高效的,尤其是对某些字符串的抓取.过滤方面,更显其优势. 正则表达式的写法通常比较简单,几行短代码便能轻松完成看似很复杂的事情,更值得称赞的是,它的执行 ...

  8. 建立tracert路由列表的方法

    建立tracert路由列表的方法:电脑屏幕左下方 选择开始选项运行 输入 CMD在DOS命令行下输入:tracert (你的网站域名)   运行结果中如出现了“*     *     *    req ...

  9. Linux shell下批量创建缩略图

    一.背景 今天,突然发现手机客户端上的最新新闻缩略图都不显示了,上服务器上看了看, 发现新的新闻图片根本没有生成缩略图. 这套新闻发布系统是很老的程序了,查了一下,问题的原因是不支持png格式的图片, ...

  10. Java [leetcode 6] ZigZag Conversion

    问题描述: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...