工学1号馆

home

« | 返回首页 | »

使用Eclipse搭建HBase开发环境

By Wu Yudong on March 14, 2016

在eclipse里新建一个java项目(命名为twitbase)

然后选择项目属性,在Libraries->Add External JARs...,然后选择/hbase/lib下相关的JAR包

如果只是测试用的话,就简单一点,将所有的JAR选上。

接着选择hbase文件夹下的hbase-0.92.1.jar和hbase-0.92.1-tests.jar包

项目下新建conf文件夹,将/hbase-0.92.1/conf文件夹下的内容复制进去

package HBaseIA.TwitBase;

import java.io.IOException;

import javax.naming.spi.DirStateFactory.Result;

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.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.client.*;

//import com.google.common.primitives.Bytes;

public class ExampleClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		Configuration config = HBaseConfiguration.create();

		// create table
		HBaseAdmin admin = new HBaseAdmin(config);
		HTableDescriptor htd = new HTableDescriptor("test");
		HColumnDescriptor hcd = new HColumnDescriptor("data");
		htd.addFamily(hcd);
		admin.createTable(htd);
		byte[] tablename = htd.getName();
		HTableDescriptor[] tables = admin.listTables();
		if (tables.length != 1 && Bytes.equals(tablename, tables[0].getName())) {
			throw new IOException("Failed create of table");
		}
		HTable table = new HTable(config, tablename);
		byte[] row1 = Bytes.toBytes("row1");
		Put p1 = new Put(row1);
		byte[] databytes = Bytes.toBytes(" data ");
		p1.add(databytes, Bytes.toBytes("1"), Bytes.toBytes("value1"));
		table.put(p1);
		Get g = new Get(row1);
		org.apache.hadoop.hbase.client.Result result = table.get(g);
		System.out.println("Get: " + result);
		Scan scan = new Scan();
		ResultScanner scanner = table.getScanner(scan);
		try {
			for (org.apache.hadoop.hbase.client.Result scannerResult : scanner) {
				System.out.println(" Scan: " + scannerResult);
			}
		} finally {
			scanner.close();
		}
		admin.disableTable(tablename);
		admin.deleteTable(tablename);
	}
}

运行程序,出现警告:

16/03/14 04:21:19 INFO zookeeper.ZooKeeper: Client environment:zookeeper.version=3.4.3-1240972, built on 02/06/2012 10:48 GMT
16/03/14 04:21:19 INFO zookeeper.ZooKeeper: Client environment:host.name=ubuntu
16/03/14 04:21:19 INFO zookeeper.ZooKeeper: Client environment:java.version=1.6.0_37
16/03/14 04:21:19 INFO zookeeper.ZooKeeper: Client environment:java.vendor=Sun Microsystems Inc.
16/03/14 04:21:19 INFO zookeeper.ZooKeeper: Client environment:java.home=/home/wu/opt/jdk1.6.0_37/jre
16/03/14 04:21:19 INFO zookeeper.ZooKeeper: Client environment:java.class.path=/home/wu/workspace/twitbase/bin:/home/wu/opt/hbase-0.92.1/lib/activation-1.1.jar:/home/wu/opt/hbase-0.92.1/lib/asm-3.1.jar:/home/wu/opt/hbase-0.92.1/lib/avro-1.5.3.jar:/home/wu/opt/hbase-0.92.1/lib/avro-ipc-1.5.3.jar:/home/wu/opt/hbase-0.92.1/lib/commons-beanutils-1.7.0.jar:/home/wu/opt/hbase-0.92.1/lib/commons-beanutils-core-1.8.0.jar:/home/wu/opt/hbase-0.92.1/lib/commons-cli-1.2.jar:/home/wu/opt/hbase-0.92.1/lib/commons-codec-1.4.jar:/home/wu/opt/hbase-0.92.1/lib/commons-collections-3.2.1.jar:/home/wu/opt/hbase-0.92.1/lib/commons-configuration-1.6.jar:/home/wu/opt/hbase-0.92.1/lib/commons-digester-1.8.jar:/home/wu/opt/hbase-0.92.1/lib/commons-el-1.0.jar:/home/wu/opt/hbase-0.92.1/lib/commons-httpclient-3.1.jar:/home/wu/opt/hbase-0.92.1/lib/commons-lang-2.5.jar:/home/wu/opt/hbase-0.92.1/lib/commons-logging-1.1.1.jar:/home/wu/opt/hbase-0.92.1/lib/commons-math-2.1.jar:/home/wu/opt/hbase-0.92.1/lib/commons-net-1.4.1.jar:/home/wu/opt/hbase-0.92.1/lib/core-3.1.1.jar:/home/wu/opt/hbase-0.92.1/lib/guava-r09.jar:/home/wu/opt/hbase-0.92.1/lib/hadoop-core-1.0.0.jar:/home/wu/opt/hbase-0.92.1/lib/high-scale-lib-1.1.1.jar:/home/wu/opt/hbase-0.92.1/lib/httpclient-4.0.1.jar:/home/wu/opt/hbase-0.92.1/lib/httpcore-4.0.1.jar:/home/wu/opt/hbase-0.92.1/lib/jackson-core-asl-1.5.5.jar:/home/wu/opt/hbase-0.92.1/lib/jackson-jaxrs-1.5.5.jar:/home/wu/opt/hbase-0.92.1/lib/jackson-mapper-asl-1.5.5.jar:/home/wu/opt/hbase-0.92.1/lib/jackson-xc-1.5.5.jar:/home/wu/opt/hbase-0.92.1/lib/jamon-runtime-2.3.1.jar:/home/wu/opt/hbase-0.92.1/lib/jasper-compiler-5.5.23.jar:/home/wu/opt/hbase-0.92.1/lib/jasper-runtime-5.5.23.jar:/home/wu/opt/hbase-0.92.1/lib/jaxb-api-2.1.jar:/home/wu/opt/hbase-0.92.1/lib/jaxb-impl-2.1.12.jar:/home/wu/opt/hbase-0.92.1/lib/jersey-core-1.4.jar:/home/wu/opt/hbase-0.92.1/lib/jersey-json-1.4.jar:/home/wu/opt/hbase-0.92.1/lib/jersey-server-1.4.jar:/home/wu/opt/hbase-0.92.1/lib/jettison-1.1.jar:/home/wu/opt/hbase-0.92.1/lib/jetty-6.1.26.jar:/home/wu/opt/hbase-0.92.1/lib/jetty-util-6.1.26.jar:/home/wu/opt/hbase-0.92.1/lib/jruby-complete-1.6.5.jar:/home/wu/opt/hbase-0.92.1/lib/jsp-2.1-6.1.14.jar:/home/wu/opt/hbase-0.92.1/lib/jsp-api-2.1-6.1.14.jar:/home/wu/opt/hbase-0.92.1/lib/libthrift-0.7.0.jar:/home/wu/opt/hbase-0.92.1/lib/log4j-1.2.16.jar:/home/wu/opt/hbase-0.92.1/lib/netty-3.2.4.Final.jar:/home/wu/opt/hbase-0.92.1/lib/protobuf-java-2.4.0a.jar:/home/wu/opt/hbase-0.92.1/lib/servlet-api-2.5.jar:/home/wu/opt/hbase-0.92.1/lib/servlet-api-2.5-6.1.14.jar:/home/wu/opt/hbase-0.92.1/lib/slf4j-api-1.5.8.jar:/home/wu/opt/hbase-0.92.1/lib/slf4j-log4j12-1.5.8.jar:/home/wu/opt/hbase-0.92.1/lib/snappy-java-1.0.3.2.jar:/home/wu/opt/hbase-0.92.1/lib/stax-api-1.0.1.jar:/home/wu/opt/hbase-0.92.1/lib/velocity-1.7.jar:/home/wu/opt/hbase-0.92.1/lib/xmlenc-0.52.jar:/home/wu/opt/hbase-0.92.1/lib/zookeeper-3.4.3.jar:/home/wu/opt/hbase-0.92.1/hbase-0.92.1.jar:/home/wu/opt/hbase-0.92.1/hbase-0.92.1-tests.jar
16/03/14 04:21:19 INFO zookeeper.ZooKeeper: Client environment:java.library.path=/home/wu/opt/jdk1.6.0_37/jre/lib/i386/client:/home/wu/opt/jdk1.6.0_37/jre/lib/i386:/home/wu/opt/jdk1.6.0_37/jre/../lib/i386:/home/wu/opt/jdk1.6.0_37/jre/lib/i386/client:/home/wu/opt/jdk1.6.0_37/jre/lib/i386::/usr/java/packages/lib/i386:/lib:/usr/lib
16/03/14 04:21:19 INFO zookeeper.ZooKeeper: Client environment:java.io.tmpdir=/tmp
16/03/14 04:21:19 INFO zookeeper.ZooKeeper: Client environment:java.compiler=<NA>
16/03/14 04:21:19 INFO zookeeper.ZooKeeper: Client environment:os.name=Linux
16/03/14 04:21:19 INFO zookeeper.ZooKeeper: Client environment:os.arch=i386
16/03/14 04:21:19 INFO zookeeper.ZooKeeper: Client environment:os.version=3.16.0-30-generic
16/03/14 04:21:19 INFO zookeeper.ZooKeeper: Client environment:user.name=wu
16/03/14 04:21:19 INFO zookeeper.ZooKeeper: Client environment:user.home=/home/wu
16/03/14 04:21:19 INFO zookeeper.ZooKeeper: Client environment:user.dir=/home/wu/workspace/twitbase
16/03/14 04:21:19 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=localhost:2181 sessionTimeout=180000 watcher=hconnection
16/03/14 04:21:19 INFO zookeeper.ClientCnxn: Opening socket connection to server /127.0.0.1:2181
16/03/14 04:21:19 INFO zookeeper.RecoverableZooKeeper: The identifier of this process is 5294@ubuntu
16/03/14 04:21:19 WARN client.ZooKeeperSaslClient: SecurityException: java.lang.SecurityException: Unable to locate a login configuration occurred when trying to find JAAS configuration.
16/03/14 04:21:19 INFO client.ZooKeeperSaslClient: Client will not SASL-authenticate because the default JAAS configuration section 'Client' could not be found. If you are not using SASL, you may ignore this. On the other hand, if you expected SASL to work, please fix your JAAS configuration.
16/03/14 04:21:19 INFO zookeeper.ClientCnxn: Socket connection established to localhost/127.0.0.1:2181, initiating session
16/03/14 04:21:19 INFO zookeeper.ClientCnxn: Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x15373ea1d370005, negotiated timeout = 40000
Exception in thread "main" java.io.IOException: Failed create of table
at HBaseIA.TwitBase.ExampleClient.main(ExampleClient.java:37)

在终端执行list命令:

hbase(main):001:0> list
TABLE
test
users
2 row(s) in 0.5430 seconds

说明test表创建成功!

进一步:

hbase(main):002:0> describe 'test'
DESCRIPTION ENABLED
{NAME => 'test', FAMILIES => [{NAME => 'data', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => true
'0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => '2147483647', BLOC
KSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}]}
1 row(s) in 0.0460 seconds

 

如果文章对您有帮助,欢迎点击下方按钮打赏作者

Comments

No comments yet.
To verify that you are human, please fill in "七"(required)