hbase standalone模式部署和API测试

1、配置JDK

export JAVA_HOME=/apps/svr/jdk7
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:/apps/svr/jdk7/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export HBASE_HOME=/apps/svr/hbase
export PATH=${HBASE_HOME}/bin:${PATH}

2、修改配置文件

<configuration>
<property>
  <name>hbase.rootdir</name>
  <value>file:///apps/svr/hbase/data</value>
</property>

<property>
  <name>hbase.zookeeper.property.dataDir</name>
  <value>/apps/svr/hbase/data/hbase</value>
</property>

<property>
  <name>zookeeper.znode.parent</name>
  <value>/hbase</value>
</property>

</configuration>

检查xml文件格式是否正确
xmllint hbase-site.xml –noout

3、启动hbase

sh bin/start-hbase.sh 

4、创建hbase表

hbase shell
> create 'chenqun', {NAME => 'cf'}

5、使用API读写hbase
目前部署的是hbase v1.0.0版本,Maven使用的客户端也是响应的版本。hbase-client-1.0.0 API和之前版本发生了很多变化。
pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<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>dba.vipshop.com</groupId>
    <artifactId>test-hbase</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
</project>

读写代码:

/**
 * Created by sylar.chen on 2016/1/19.
 */

1
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.Date;

public class Chen {
    public static void main(String[] args) throws IOException {
        Configuration HBASE_CONFIG =  HBaseConfiguration.create();

        HBASE_CONFIG.set("hbase.zookeeper.quorum","10.199.234.106");
        HBASE_CONFIG.set("zookeeper.znode.parent","/hbase");
        Connection connection = ConnectionFactory.createConnection(HBASE_CONFIG);
        //Admin admin = connection.getAdmin();
        String tableName = "chenqun";
        Table  table = connection.getTable(TableName.valueOf(tableName));

        Put put = new Put(Bytes.toBytes(new Date().getTime()));
        put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"),Bytes.toBytes("sylar.chen"));
        table.put(put);
    }
}
此条目发表在hbase分类目录,贴了, 标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>