HBase搭建并使用Java调用HBaseAPI

一、HBase的搭建

1.1下载HBase0.94.14

1.2修改 conf/hbase-site.xml

<configuration>

 <property>
    <name>hbase.rootdirname>
    <value>file:///root/test/hbasevalue>
  property>

<property>
    <name>hbase.zookeeper.property.dataDirname>
    <value>/root/test/zookeepervalue>
  property>

  <property>
   <name>hbase.zookeeper.quorumname>
   <value>127.0.0.1:2181value>
 property>
configuration>

1.3修改hbase-env.sh

改成自己本地下的java的路径

 export JAVA_HOME=/usr/local/java

1.4启动HBase

root@fonxian-desktop:~/hbase/bin# start-hbase.sh

测试有没有启动成功,输入命令

root@fonxian-desktop:~/hbase/bin# jps
16787 Jps
9653 HMaster
9131 JobTracker
9963 Main
8715 NameNode
9292 TaskTracker
9037 SecondaryNameNode
8878 DataNode

有HMaster说明配置成功

二、HBase脚本命令的使用

2.1、打开命令行交互

root@fonxian-desktop:~/hbase/bin# ./hbase shell

2.2、创建表

其中’mytable’是表名,’cf’是列族名
HBase中,每个表必须至少要有一个列族(column family)

create 'mytable','cf'

2.3查看表

scan 'mytablee' 

2.4插入记录

在’first’行的’cf:message’列,写入数据

 put 'mytable','first','cf:message','hello HBase'

2.5读取记录

get 'mytable','first'

2.6查看表的结构

describe 'mytable'

2.7列出数据库

list

2.8删除表

首先先要让表无效,否则无法直接删除

disable 'tablename'
drop 'tablename'

删除列

delete 'tablename','rowkey','family:column'

三、Java调用HBaseAPI

3.1将hbase-site.xml导入到项目下或直接新建一个hbase-site.xml



<configuration>
 <property>
    <name>hbase.rootdirname>
    <value>file:///root/test/hbasevalue>
  property>
<property>
<name>hbase.cluster.distributedname>
<value>flasevalue>
property>

<property>
    <name>hbase.zookeeper.property.dataDirname>
    <value>/root/test/zookeepervalue>
  property>

  <property>
   <name>hbase  .zookeeper.quorumname>
   <value>127.0.0.1:2181value>
 property>

<property skipInDoc="true">
<name>hbase.defaults.for.versionname>
<value>0.94.14value>
property>
configuration>

3.2配置HBase

将HBase/lib下的jar包都导入到eclipse中,不导入的话,会出现诸多类找不到的错误

private static Configuration conf = null;

    /**
     * HBaseConfiguration作用:对HBase进行配置
     */
    static {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("hbase.zookeeper.quorum", "127.0.0.1");
        conf.set("hbase.master", "127.0.0.1:60010");
    }

3.3创建表

/**
     * 创建表操作
     * 
     * @throws IOException
     *             HBaseAdmin作用:提供HBase数据库中表相关信息的接口
     */
    public void createTable(String tablename, String[] cfs) throws IOException {
        HBaseAdmin admin = new HBaseAdmin(conf);
        if (admin.tableExists(tablename)) {
            System.out.println("表已经存在!");
        } else {
            HTableDescriptor tableDesc = new HTableDescriptor(tablename);
            for (int i = 0; i < cfs.length; i++) {
                tableDesc.addFamily(new HColumnDescriptor(cfs[i]));
            }
            admin.createTable(tableDesc);
            System.out.println("表创建成功!");
        }
    }

3.4插入记录

/**
     * 插入一行记录
     * 
     * @param tablename
     * @param cfs
     *            Put的作用:向表 tablename 添加 “family,qualifier,value”指定的值。 
     *            put 'tableName','rowKey','family:qualifier','value',
     */
    public void writeRow(String tableName, String rowKey, String family, String qualifier, String value) {
        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("插入记录成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3.5查询表中所有行

/**
     * 查询表中所有行
     * 
     * @param tablename
     *            HTable的作用是与HBase中的表进行通信
     */
public void scaner(String tablename) {
        try {
            HTable table = new HTable(conf, tablename);
            Scan s = new Scan();
            ResultScanner rs = table.getScanner(s);
            for (Result r : rs) {
                KeyValue[] kv = r.raw();
                for (int i = 0; i < kv.length; i++) {
                    System.out.print(new String(kv[i].getRow()) + "");
                    System.out.print(new String(kv[i].getFamily()) + ":");
                    System.out.print(new String(kv[i].getQualifier()) + "");
                    System.out.print(kv[i].getTimestamp() + "");
                    System.out.println(new String(kv[i].getValue()));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3.6删除单行记录

/**
     * 删除一行记录
     * 
     * @param tablename
     * @param rowkey
     * @throws IOException
     */
    public void deleteRow(String tablename, String rowkey) throws IOException {
        HTable table = new HTable(conf, tablename);
        List list = new ArrayList();
        Delete d1 = new Delete(rowkey.getBytes());
        list.add(d1);
        table.delete(list);
        System.out.println("删除行成功!");
    }

3.7主函数测试

public static void main(String[] args) throws Exception {
        HBaseTest t = new HBaseTest();
        // 创建表
        String tableName = "scores";
        String[] cfs = { "grade", "course" };
        t.createTable(tableName, cfs);
        // 添加记录     
        t.writeRow(tableName, "second", "grade", "message", "87");
        // 打印表中数据
        t.scaner("scores");
        //删除记录
        t.deleteRow("scores", "second");
    }

你可能感兴趣的:(HADOOP,Nutch实战)