环境:
Win764bit
Eclipse Version: Kepler Service Release 1
java version "1.7.0_40"

第一步:Eclipse中新建Maven项目。编辑pom.xml并更新下载jar包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>fulong.bigdata</groupId>
    <artifactId>myHbase</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>0.96.2-hadoop2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>jdk.tools</groupId>
            <artifactId>jdk.tools</artifactId>
            <version>1.7</version>
            <scope>system</scope>
            <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
        </dependency>
    </dependencies>
</project> 

第二步:将目标集群的Hadoop和HBase配置文件复制到project中
目的是为了让project能找到Zookeeper及Hbase Master。
配置文件在project中的路径为:
/src/main/resources/hadoop
/src/main/resources/hbase
然后将这两个文件夹加入进project的classpath中:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDk2NzM4Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />

终于文件夹结构例如以下:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDk2NzM4Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />


第三步:hbase-site.xml中加入
    <property>
        <name>fs.hdfs.impl</name>
        <value>org.apache.hadoop.hdfs.DistributedFileSystem</value>

    </property>  


第四步:编写Java程序调用Hbase接口
该代码包括了部分经常使用HBase接口。
package myHbase;
import java.io.IOException;
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.TableName;
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.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseDAO { static Configuration conf = HBaseConfiguration.create();
/**
* create a table :table_name(columnFamily)
* @param tablename
* @param columnFamily
* @throws Exception
*/
public static void createTable(String tablename, String columnFamily) throws Exception {
HBaseAdmin admin = new HBaseAdmin(conf);
if(admin.tableExists(tablename)) {
System.out.println("Table exists!");
System.exit(0);
}
else {
HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tablename));
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
admin.createTable(tableDesc);
System.out.println("create table success!");
}
admin.close(); } /**
* delete table ,caution!!!!!! ,dangerous!!!!!!
* @param tablename
* @return
* @throws IOException
*/
public static boolean deleteTable(String tablename) throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
if(admin.tableExists(tablename)) {
try {
admin.disableTable(tablename);
admin.deleteTable(tablename);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
admin.close();
return false;
}
}
admin.close();
return true;
}
/**
* put a cell data into a row identified by rowKey,columnFamily,identifier
* @param HTable, create by : HTable table = new HTable(conf, "tablename")
* @param rowKey
* @param columnFamily
* @param identifier
* @param data
* @throws Exception
*/
public static void putCell(HTable table, String rowKey, String columnFamily, String identifier, String data) throws Exception{
Put p1 = new Put(Bytes.toBytes(rowKey));
p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(identifier), Bytes.toBytes(data));
table.put(p1);
System.out.println("put '"+rowKey+"', '"+columnFamily+":"+identifier+"', '"+data+"'");
} /**
* get a row identified by rowkey
* @param HTable, create by : HTable table = new HTable(conf, "tablename")
* @param rowKey
* @throws Exception
*/
public static Result getRow(HTable table, String rowKey) throws Exception {
Get get = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get);
System.out.println("Get: "+result);
return result;
} /**
* delete a row identified by rowkey
* @param HTable, create by : HTable table = new HTable(conf, "tablename")
* @param rowKey
* @throws Exception
*/
public static void deleteRow(HTable table, String rowKey) throws Exception {
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
System.out.println("Delete row: "+rowKey);
} /**
* return all row from a table
* @param HTable, create by : HTable table = new HTable(conf, "tablename")
* @throws Exception
*/
public static ResultScanner scanAll(HTable table) throws Exception {
Scan s =new Scan();
ResultScanner rs = table.getScanner(s);
return rs;
} /**
* return a range of rows specified by startrow and endrow
* @param HTable, create by : HTable table = new HTable(conf, "tablename")
* @param startrow
* @param endrow
* @throws Exception
*/
public static ResultScanner scanRange(HTable table,String startrow,String endrow) throws Exception {
Scan s =new Scan(Bytes.toBytes(startrow),Bytes.toBytes(endrow));
ResultScanner rs = table.getScanner(s);
return rs;
}
/**
* return a range of rows filtered by specified condition
* @param HTable, create by : HTable table = new HTable(conf, "tablename")
* @param startrow
* @param filter
* @throws Exception
*/
public static ResultScanner scanFilter(HTable table,String startrow, Filter filter) throws Exception {
Scan s =new Scan(Bytes.toBytes(startrow),filter);
ResultScanner rs = table.getScanner(s);
return rs;
} public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub HTable table = new HTable(conf, "apitable"); // ResultScanner rs = HBaseDAO.scanRange(table, "2013-07-10*", "2013-07-11*");
// ResultScanner rs = HBaseDAO.scanRange(table, "100001", "100003");
ResultScanner rs = HBaseDAO.scanAll(table); for(Result r:rs) {
System.out.println("Scan: "+r);
}
table.close(); // HBaseDAO.createTable("apitable", "testcf");
// HBaseDAO.putRow("apitable", "100001", "testcf", "name", "liyang");
// HBaseDAO.putRow("apitable", "100003", "testcf", "name", "leon");
// HBaseDAO.deleteRow("apitable", "100002");
// HBaseDAO.getRow("apitable", "100003");
// HBaseDAO.deleteTable("apitable"); }
}

【甘道夫】Eclipse+Maven搭建HBase开发环境及HBaseDAO代码演示样例的更多相关文章

  1. 构造Scala开发环境并创建ApiDemos演示样例项目

    从2011年開始写Android ApiDemos 以来.Android的版本号也更新了非常多,眼下的版本号已经是4.04. ApiDemos中的样例也添加了不少,有必要更新Android ApiDe ...

  2. 使用IntelliJ IDEA和Maven管理搭建Web开发环境(以Spring MVC为例)(二)

    前言:在使用IntelliJ IDEA和Maven管理搭建Web开发环境(以Spring MVC为例)(一)中已经介绍了如何对web基础环境进行搭建,这里主要演示,如何对spring环境进行搭建,然后 ...

  3. 使用IntelliJ IDEA和Maven管理搭建Web开发环境(以Spring MVC为例)(一)

    前言:原来一直使用MyEclipse,换工作后,新公司使用IDEA,初识IDEA发现,哇,它的快捷键可真多啊,但是一路用下来,觉得非常的好用,特别是利用Maven管理,那简直叫一个爽.当然笔者在使用过 ...

  4. [转]在Eclipse中搭建Python开发环境

    在Eclipse中搭建Python开发环境 来自: http://hi.baidu.com/hqwfreefly/blog/item/2543181d0afd9604314e150e.html 前言 ...

  5. Intellij IDEA使用Maven搭建spark开发环境(scala)

    如何一步一步地在Intellij IDEA使用Maven搭建spark开发环境,并基于scala编写简单的spark中wordcount实例. 1.准备工作 首先需要在你电脑上安装jdk和scala以 ...

  6. 在Eclipse中搭建Python开发环境

    在Eclipse中搭建Python开发环境 来自: http://hi.baidu.com/hqwfreefly/blog/item/2543181d0afd9604314e150e.html 前言 ...

  7. Windows+QT+Eclipse+MinGW搭建QT开发环境详细教程

     Windows+QT+Eclipse+MinGW搭建QT开发环境详细教程 一.准备工具: QT-SDK for Windows:http://get.qt.nokia.com/qtsdk/qt-sd ...

  8. 在Eclipse下搭建Android开发环境教程

    我们昨天向各位介绍了<在NetBeans上搭建Android SDK环境>,前不久也介绍过<在MyEclipse 8.6上搭建Android开发环境>, 都受到了读者的欢迎.但 ...

  9. Eclipse中搭建Android开发环境

    一.搭建Android开发环境 准备工作:下载Eclipse.JDK.Android SDK.ADT插件 下载地址:Eclipse:http://www.eclipse.org/downloads/ ...

随机推荐

  1. POJ 3171 区间覆盖最小值&&线段树优化dp

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4715   Accepted: 1590 D ...

  2. Codeforces Gym101572 B.Best Relay Team (2017-2018 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2017))

    2017-2018 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2017) 今日份的训练,题目难度4颗星,心态被打崩了,会的算法太少了,知 ...

  3. PostgreSQL 二进制安装

    一.安装前准备工作 新建用户 sudo groupadd sql sudo useradd -g sql postgressudo passwd postgres 创建数据及日志目录,并做相应授权 s ...

  4. Codeforces 877E Danil and a Part-time Job(dfs序 + 线段树)

    题目链接   Danil and a Part-time Job 题意    给出一系列询问或者修改操作 $pow$ $x$表示把以$x$为根的子树的所有结点的状态取反($0$变$1$,$1$变$0$ ...

  5. PyTorch学习问题记录

    Q1:def train() 中的model.train()的作用是什么?为什么要写? A1:class torch.nn.Module中 train(mode=True) Sets the modu ...

  6. 如何设置tomcat服务器编码为utf-8编码

    原文:http://blog.csdn.net/u014079773/article/details/52637057 在实际开发中我们经常遇到request请求的中文乱码,那么如何解决中文乱码问题? ...

  7. iOS开发之分段控制器(UISegmentedControl)

    今天我们来说下iOS中的分段选择控制器UISegmentedControl,这一控件有什么作用呢 每个segment都能被点击,相当于集成了多个button 通常我们会点击不同的segment来切换不 ...

  8. 深度排序与alpha混合 【转】

      翻译:李现民 最后修改:2012-07-03 原文:Depth sorting alpha blended objects 先说个题外话,本来我想回答在 Creators Club论坛上的一个常见 ...

  9. python 列表结构更新的奇妙问题

    使用python + plt 画图遇到了一个奇怪的问题 应该出来的是这样: 结果做出来以后是这样: 为什么画到一起了...... 这个锅python列表背 a=[1,2]b=a  这样  改变b ,a ...

  10. Highcharts常用的最核心的参数选项配置详细说明

    Highcharts提供大量的选项配置参数,您可以轻松定制符合用户要求的图表,目前官网只提供英文版的开发配置说明文档,而中文版的文档网上甚少,且零散不全.这里,我把Highcharts常用的最核心的参 ...