HBase linux下java操作。
安装的是JDK1.7.0_65 安装目录
[root@wangxin jdk1.7.0_65]# pwd
/usr/java/jdk1.7.0_65
环境配置
[root@wangxin jdk1.7.0_65]# cat /etc/profile
export JAVA_HOME=/usr/java/jdk1.7.0_65
export JAR_HOME=/usr/java/jdk1.7.0_65/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JAR_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JAR_HOME/bin:$PATH
[root@wangxin jdk1.7.0_65]# java -version
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)
HBase 安装的是 hbase-0.98.5-hadoop2-bin.tar.gz 版本
安装目录是
[root@wangxin hbase]# pwd
/usr/local/hbase
[root@wangxin hbase]# ls
bin CHANGES.txt conf docs hbase-webapps lib LICENSE.txt logs NOTICE.txt README.txt
仅修改 conf/hbase-site.xml
<configuration>
<property>
<name>hbase.rootdir</name>
<value>file:///hbase_log/tmp/hbase</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/hbase_log/tmp/zookeeper</value>
</property>
通关脚本启动
[root@wangxin /]# start-hbase.sh
starting master, logging to /usr/local/hbase/logs/hbase-root-master-wangxin.out
[root@wangxin /]# hbase shell
2014-08-27 06:54:52,498 INFO [main] Configuration.deprecation: hadoop.native.lib is deprecated. Instead, use io.native.lib.available
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 0.98.5-hadoop2, rUnknown, Mon Aug 4 23:58:06 PDT 2014
hbase(main):001:0>
在这里可以进行一系列操作,说明HBase安装是没有问题的。也可以通过60010端口在浏览器打开。
现在写一段java代码,通过java api去操作HBase
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.KeyValue;
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.HTablePool;
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.util.Bytes;
public class HBase_c2java{
static HBaseConfiguration cfg = null;
static {
Configuration configuration = new Configuration();
cfg = new HBaseConfiguration(configuration);
}
public static void createTable(String tableName, String columnFaily)throws Exception{
HBaseAdmin admin = new HBaseAdmin(cfg);
if(admin.tableExists(tableName)){
System.out.println(tableName + "It's not existed!");
System.exit(0);
}
else{
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
tableDesc.addFamily(new HColumnDescriptor(columnFaily+":"));
System.out.println("create table success!");
}
}
public static void main(String[] args){
try{
String tableName = "student";
HBase_c2java.createTable(tableName, "c1");
}catch(Exception e){
e.printStackTrace();
}
}
}
在安装的hbse路径的lib下有很多.jar包
[root@wangxin lib]# ls
activation-1.1.jar hadoop-mapreduce-client-core-2.2.0.jar javax.servlet-api-3.0.1.jar
aopalliance-1.0.jar hadoop-mapreduce-client-jobclient-2.2.0.jar jaxb-api-2.2.2.jar
asm-3.1.jar hadoop-mapreduce-client-shuffle-2.2.0.jar jaxb-impl-2.2.3-1.jar
avro-1.7.4.jar hadoop-yarn-api-2.2.0.jar jersey-client-1.9.jar