开发环境:
hadoop: hadoop-1.1.2
hbase: hbase-0.94.11-security
eclipse:Juno Service Release 2
配置Eclipse
通过 Eclipse 创建一个新 Java 工程,右击项目根目录,选择“Properties> Java Build Path> Library> Add External JARs”,将 HBase 安装文件解压后根目录下的hbase-0.94.11-security.jar、hbase-0.94.11-security-tests.jar 和 lib 子目录下所有的 jar 包添加到本工程的 Build Path下。
示例一:输出表“users”的列族名称
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.client.HBaseAdmin; import org.apache.hadoop.hbase.util.Bytes; public class ExampleClient { public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "master"); conf.set("hbase.zookeeper.property.clientPort", "2181"); HBaseAdmin admin = new HBaseAdmin(conf); HTableDescriptor tableDescriptor = admin.getTableDescriptor(Bytes.toBytes("users")); byte[] name = tableDescriptor.getName(); System.out.println("result:"); System.out.println("table name: "+ new String(name)); HColumnDescriptor[] columnFamilies = tableDescriptor .getColumnFamilies(); for(HColumnDescriptor d : columnFamilies){ System.out.println("column Families: "+ d.getNameAsString()); } } }
结果:
2013-09-09 15:58:51,890 WARN conf.Configuration (Configuration.java:<clinit>(195)) - DEPRECATED: hadoop-site.xml found in the classpath. Usage of hadoop-site.xml is deprecated. Instead use core-site.xml, mapred-site.xml and hdfs-site.xml to override properties of core-default.xml, mapred-default.xml and hdfs-default.xml respectively 2013-09-09 15:58:55,031 INFO zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT 2013-09-09 15:58:55,031 INFO zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:host.name=w253245.ppp.asahi-net.or.jp 2013-09-09 15:58:55,031 INFO zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.version=1.6.0_10-rc2 2013-09-09 15:58:55,031 INFO zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.vendor=Sun Microsystems Inc. 2013-09-09 15:58:55,031 INFO zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.home=C:Javajre6 2013-09-09 15:58:55,031 INFO zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.class.path=D:workspaceEclipse-jeeHadoop_APPs_U_thtin;D:workspaceEclipse-jeehadoop-1.1.2uildeclipseclasses-main;D:workspaceEclipse-jeehadoop-1.1.2uildeclipseclasses-test;D:workspaceEclipse-jeehadoop-1.1.2uildeclipseclasses-test-generated;D:workspaceEclipse-jeehadoop-1.1.2uildeclipseclasses-tools;D:workspaceEclipse-jeehadoop-1.1.2uildeclipseclasses-example;C:apache-ant-1.9.2libant.jar;D:workspaceEclipse-jeehadoop-1.1.2libasm-3.2.jar;D:workspaceEclipse-jeehadoop-1.1.2libaspectjrt-1.6.11.jar;D:workspaceEclipse-jeehadoop-1.1.2libaspectjtools-1.6.11.jar;D:workspaceEclipse-jeehadoop-1.1.2libcommons-beanutils-1.7.0.jar;D:workspaceEclipse-jeehadoop-1.1.2libcommons-beanutils-core-1.8.0.jar;D:workspaceEclipse-jeehadoop-1.1.2libcommons-cli-1.2.jar;D:workspaceEclipse-jeehadoop-1.1.2libcommons-codec-1.4.jar;D:workspaceEclipse-jeehadoop-1.1.2libcommons-collections-3.2.1.jar;D:workspaceEclipse-jeehadoop-1.1.2libcommons-configuration-1.6.jar;D:workspaceEclipse-jeehadoop-1.1.2libcommons-daemon-1.0.1.jar;D:workspaceEclipse-jeehadoop-1.1.2libcommons-digester-1.8.jar;D:workspaceEclipse-jeehadoop-1.1.2libcommons-el-1.0.jar;D:workspaceEclipse-jeehadoop-1.1.2libcommons-httpclient-3.0.1.jar;D:workspaceEclipse-jeehadoop-1.1.2libcommons-io-2.1.jar;D:workspaceEclipse-jeehadoop-1.1.2libcommons-lang-2.4.jar;D:workspaceEclipse-jeehadoop-1.1.2libcommons-logging-1.1.1.jar;D:workspaceEclipse-jeehadoop-1.1.2libcommons-logging-api-1.0.4.jar;D:workspaceEclipse-jeehadoop-1.1.2libcommons-math-2.1.jar;D:workspaceEclipse-jeehadoop-1.1.2libcommons-net-3.1.jar;D:workspaceEclipse-jeehadoop-1.1.2libcore-3.1.1.jar;D:workspaceEclipse-jeehadoop-1.1.2libhadoop-capacity-scheduler-1.1.2.jar;D:workspaceEclipse-jeehadoop-1.1.2libhadoop-fairscheduler-1.1.2.jar;D:workspaceEclipse-jeehadoop-1.1.2libhadoop-thriftfs-1.1.2.jar;D:workspaceEclipse-jeehadoop-1.1.2libhsqldb-1.8.0.10.jar;D:workspaceEclipse-jeehadoop-1.1.2libjackson-core-asl-1.8.8.jar;D:workspaceEclipse-jeehadoop-1.1.2libjackson-mapper-asl-1.8.8.jar;D:workspaceEclipse-jeehadoop-1.1.2libjasper-compiler-5.5.12.jar;D:workspaceEclipse-jeehadoop-1.1.2libjasper-runtime-5.5.12.jar;D:workspaceEclipse-jeehadoop-1.1.2libjdeb-0.8.jar;D:workspaceEclipse-jeehadoop-1.1.2libjersey-core-1.8.jar;D:workspaceEclipse-jeehadoop-1.1.2libjersey-json-1.8.jar;D:workspaceEclipse-jeehadoop-1.1.2libjersey-server-1.8.jar;D:workspaceEclipse-jeehadoop-1.1.2libjets3t-0.6.1.jar;D:workspaceEclipse-jeehadoop-1.1.2libjetty-6.1.26.jar;D:workspaceEclipse-jeehadoop-1.1.2libjetty-util-6.1.26.jar;D:workspaceEclipse-jeehadoop-1.1.2libjsch-0.1.42.jar;D:workspaceEclipse-jeehadoop-1.1.2libjsp-2.1jsp-2.1.jar;D:workspaceEclipse-jeehadoop-1.1.2libjsp-2.1jsp-api-2.1.jar;D:workspaceEclipse-jeehadoop-1.1.2libjunit-4.5.jar;D:workspaceEclipse-jeehadoop-1.1.2libkfs-0.2.2.jar;D:workspaceEclipse-jeehadoop-1.1.2liblog4j-1.2.15.jar;D:workspaceEclipse-jeehadoop-1.1.2libmockito-all-1.8.5.jar;D:workspaceEclipse-jeehadoop-1.1.2liboro-2.0.8.jar;D:workspaceEclipse-jeehadoop-1.1.2libservlet-api-2.5-20081211.jar;D:workspaceEclipse-jeehadoop-1.1.2libslf4j-api-1.4.3.jar;D:workspaceEclipse-jeehadoop-1.1.2libslf4j-log4j12-1.4.3.jar;D:workspaceEclipse-jeehadoop-1.1.2libxmlenc-0.52.jar;D:workspaceEclipse-jeehadoop-1.1.2src estlibftplet-api-1.0.0-SNAPSHOT.jar;D:workspaceEclipse-jeehadoop-1.1.2src estlibftpserver-core-1.0.0-SNAPSHOT.jar;D:workspaceEclipse-jeehadoop-1.1.2src estlibftpserver-server-1.0.0-SNAPSHOT.jar;D:workspaceEclipse-jeehadoop-1.1.2src estlibmina-core-2.0.0-M2-20080407.124109-12.jar;D:workspaceEclipse-jeehadoop-1.1.2conf;D:workspaceEclipse-jeehadoop-1.1.2uildeclipse est-resources;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildactivation-1.1.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildasm-3.1.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildavro-1.5.3.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildavro-1.7.4.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildavro-ipc-1.5.3.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildcommons-beanutils-1.7.0.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildcommons-beanutils-core-1.8.0.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildcommons-cli-1.2.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildcommons-codec-1.4.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildcommons-collections-3.2.1.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildcommons-configuration-1.6.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildcommons-digester-1.8.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildcommons-el-1.0.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildcommons-httpclient-3.1.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildcommons-io-2.1.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildcommons-lang-2.5.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildcommons-logging-1.1.1.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildcommons-math-2.1.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildcommons-net-1.4.1.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildcore-3.1.1.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildguava-11.0.2.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildhadoop-common-0.23.9-javadoc.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildhamcrest-all-1.3.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildhamcrest-core-1.3.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildhbase-0.94.11-security.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildhbase-0.94.11-security-tests.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildhigh-scale-lib-1.1.1.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildhttpclient-4.1.2.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildhttpcore-4.1.3.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjackson-core-asl-1.8.8.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjackson-jaxrs-1.8.8.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjackson-mapper-asl-1.8.8.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjackson-xc-1.8.8.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjamon-runtime-2.3.1.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjasper-compiler-5.5.23.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjasper-runtime-5.5.23.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjaxb-api-2.1.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjaxb-impl-2.2.3-1.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjersey-core-1.8.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjersey-json-1.8.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjersey-server-1.8.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjettison-1.1.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjetty-6.1.26.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjetty-util-6.1.26.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjruby-complete-1.6.5.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjsp-2.1-6.1.14.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjsp-api-2.1-6.1.14.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjsr305-1.3.9.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildjunit-4.10-HBASE-1.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildlibthrift-0.8.0.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildlog4j-1.2.16.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildmetrics-core-2.1.2.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildmrunit-1.0.0-hadoop2.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildmrunit-1.0.0-javadoc.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuild etty-3.2.4.Final.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildprotobuf-java-2.4.0a.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildservlet-api-2.5-6.1.14.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildslf4j-api-1.4.3.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildslf4j-log4j12-1.4.3.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildsnappy-java-1.0.3.2.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildstax-api-1.0.1.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildvelocity-1.7.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildxmlenc-0.52.jar;D:workspaceEclipse-jeeHadoop_APPs_U_thtuildzookeeper-3.4.5.jar 2013-09-09 15:58:55,031 INFO zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.library.path=D:workspaceEclipse-jeehadoop-1.1.21lib ative;D:workspaceEclipse-jeehadoop-1.1.21lib ative 2013-09-09 15:58:55,031 INFO zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.io.tmpdir=C:DOCUME~1ADMINI~1LOCALS~1Temp 2013-09-09 15:58:55,031 INFO zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.compiler=<NA> 2013-09-09 15:58:55,031 INFO zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:os.name=Windows XP 2013-09-09 15:58:55,031 INFO zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:os.arch=x86 2013-09-09 15:58:55,031 INFO zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:os.version=5.1 2013-09-09 15:58:55,031 INFO zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:user.name=hadoop 2013-09-09 15:58:55,031 INFO zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:user.home=C:Documents and SettingsAdministrator 2013-09-09 15:58:55,031 INFO zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:user.dir=D:workspaceEclipse-jeeHadoop_APPs_U_tht 2013-09-09 15:58:55,031 INFO zookeeper.ZooKeeper (ZooKeeper.java:<init>(438)) - Initiating client connection, connectString=master:2181 sessionTimeout=180000 watcher=hconnection 2013-09-09 15:58:56,171 INFO zookeeper.RecoverableZooKeeper (RecoverableZooKeeper.java:<init>(104)) - The identifier of this process is 6032@tht 2013-09-09 15:58:56,234 INFO zookeeper.ClientCnxn (ClientCnxn.java:logStartConnect(966)) - Opening socket connection to server master/121.1.253.251:2181. Will not attempt to authenticate using SASL (无法定位登录配置) 2013-09-09 15:58:56,296 INFO zookeeper.ClientCnxn (ClientCnxn.java:primeConnection(849)) - Socket connection established to master/121.1.253.251:2181, initiating session 2013-09-09 15:58:56,484 INFO zookeeper.ClientCnxn (ClientCnxn.java:onConnected(1207)) - Session establishment complete on server master/121.1.253.251:2181, sessionid = 0x141011ad7db000e, negotiated timeout = 180000 result: table name: users column Families: address column Families: info column Families: user_id