Eclipse远程访问HBase

注意:实践过程中并不需要winutils.exe.

HBase安装请参考:https://blog.csdn.net/xiaof22a/article/details/80213064

1. 打开Eclipse, 新建工程HBase

2. 将%HBASE_HOME%\lib文件下的所有lib文件添加到 HBase的dependencies中:


3. 测试代码:

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.TableName;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class Test {
	public static Configuration conf = null;

	public static void main(String[] args) throws Exception {

		conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "master");// 使用eclipse时必须添加这个,否则无法定位
		conf.set("hbase.zookeeper.property.clientPort", "2181");
		HBaseAdmin admin = new HBaseAdmin(conf);// 新建一个数据库管理员//新api
		String[] familys=new String[] {"cf1"};
		String tableName="blog";
		if (admin.tableExists(tableName)) {
			System.out.println("table already exists!");
		} else {
			HTableDescriptor tableDesc = new HTableDescriptor(tableName);
			for (int i = 0; i < familys.length; i++) {
				tableDesc.addFamily(new HColumnDescriptor(familys[i]));
			}
			admin.createTable(tableDesc);
			System.out.println("create table " + tableName + " ok.");
		}

	}
}

输出结果:


猜你喜欢

转载自blog.csdn.net/xiaof22a/article/details/80215605
今日推荐