1.eclipse中新建普通java工程
2.导入相关jar包
将HBase解压后根目录下的hbase-0.94.0.jar、hbase-0.94.0-tests.jar和lib子目录下所有jar 包添加到本工程的Classpath下以及hadoop解压根目录下的hadoop-*.jar
3.客户端java程序
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
//定位到student表
HTable table = new HTable(conf, "student");
//设置key值 wangwu
Put put = new Put(Bytes.toBytes("wangwu"));
//列族info 列age:50 sex:male
put.add(Bytes.toBytes("info"), Bytes.toBytes("age"),
Bytes.toBytes("50")); put.add(Bytes.toBytes("info"),
Bytes.toBytes("sex"), Bytes.toBytes("male"));
//插入表
table.put(put);
table.close();
}
4.拷贝Hbase服务器端配置文件hbase-site.xml添加到本工程的Classpath中(src目录下)
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://hbase服务器ip:9000/hbase</value><!-- 与hbase服务器端配置一样即可 -->
<description>The directory shared by region servers.</description>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>false</value>
<description>The mode the cluster will be in. Possible values are
false: standalone and pseudo-distributed setups with managed
Zookeeper
true: fully-distributed with unmanaged Zookeeper Quorum (see
hbase-env.sh)
</description>
</property>
<property>
<name>hbase.master</name>
<value>hdfs://hbase服务器ip:60000</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>zookeeper服务器ip</value>
<description>Comma separated list of servers in the ZooKeeper Quorum.
For example,
"host1.mydomain.com,host2.mydomain.com,host3.mydomain.com". By
default this is set to localhost for local and pseudo-distributed
modes of operation. For a fully-distributed setup, this should be set
to a full list of ZooKeeper quorum servers. If HBASE_MANAGES_ZK is
set in hbase-env.sh this is the list of servers which we will
start/stop ZooKeeper on.
</description>
</property>
<property>
<name>hbase.zookeeper.prpperty.clientPort</name>
<value>2181</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/home/hadoop/log/zookeeper</value>
<description>Property from ZooKeeper's config zoo.cfg.
The directory
where the snapshot is stored.
</description>
</property>
</configuration>
修改相应的ip地址
5.修改host文件
hbase服务器端:
vim /etc/hosts
本机ip master
服务器端的hbase-site.xml中
<property>
<name>hbase.zookeeper.quorum</name>
<value>master</value><!--这里不能写ip-->
</property>
客户端,即windows下修改hosts文件,配置与服务器端相同
6.运行java程序
INFO zookeeper.ClientCnxn: EventThread shut down
出现以上这句,说明执行完成,在hbase shell中查看数据插入student表是否成功(以上执行的前提是hbase中已经创建了student表)
ps:Will not attempt to authenticate using SASL (无法定位登录配置)
这是由于没有启用用户验证导致的,不影响使用,但影响安全性。SASL 即:Simple Authentication and Security Layer