hadoop hbase的安装及Java api的使用

运行环境 hadoop-2.7.3.tar.gz,hbase-1.2.6-bin.tar.gz,zookeeper-3.4.8.tar.gz
解压安装包

tar -zxvf hbase-1.2.6-bin.tar.gz -C /root/apps/

解压后在/conf下配置hbase-site.xml文件、hbase-env.sh文件和regionservers文件

配置hbase-env.sh文件:

//修改JAVA_HOME变量
export JAVA_HOME=/root/apps/jdk

配置hbase-site.xml文件

<configuration>
<property>
//设置hbase:Mate文件存放路径
//我这里实现了HA,所以写了服务地址
<name>hbase.rootdir</name>
<value>hdfs://ns1/hbase</value>
</property>
<property>
//设置zookeeper集群节点
<name>hbase.zookeeper.quorum</name>
<value>hadoop05:2181,hadoop06:2181,hadoop07:2181</value>
</property>
<property>
//打开hbase分布式开关
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
</configuration>

配置regionservers文件

//配置hbase分布式的regionserver节点所在的主机地址
hadoop05
hadoop06
hadoop07

配置完成!

在/bin中start-hbase.sh启动hbase

然后再在/bin中hbase shell打开hbase linux客户端。
里面的具体用法可以打开客户端后进行help查看帮助文档。

Java Api客户端编写

public class DDLHbase {

		
		public static void main(String[] args) throws Exception {
			Configuration conf = HBaseConfiguration.create();
			conf.set("hbase.zookeeper.quorum", "hadoop05:2181,hadoop06:2181,hadoop07:2181");
			
//			创建hbase连接
			Connection connection = ConnectionFactory.createConnection(conf);
//			创建admin客户端。
			Admin admin = connection.getAdmin();
//			创建表名
			TableName tableName = TableName.valueOf("test");
//			创建表	
			HTableDescriptor test = new HTableDescriptor(tableName); 
//			创建列族
			HColumnDescriptor hcd = new HColumnDescriptor("datas");
//			在表里添加列族
			test.addFamily(hcd);
//			创建表
			admin.createTable(test);
//			获取列表名
			TableName[] listTableNames = admin.listTableNames();
//			遍历表名
			for(TableName tn : listTableNames) {
//				tn.getNameAsString()   获取表名
//				getNamespaceAsString()	命名空间。有点像RDBMS上的database
				System.out.println("Table\t"+tn.getNameAsString()+"\t"+tn.getNamespaceAsString()+"\t"+tn.getNameWithNamespaceInclAsString());
			}
			
//			通过表名,获取表
			Table tastTable = connection.getTable(tableName);
			
			for(int i=1;i<=3;i++) {
//				创建一条记录
				Put put = new Put(Bytes.toBytes("row"+i));
//				在该记录下添加列族,列名和值
				put.addColumn(Bytes.toBytes("datas"), Bytes.toBytes("column"+i), Bytes.toBytes("value"+i));
//				将该条记录添加到表中
				tastTable.put(put);
				
			}
//			创建名为row1的一条记录
			Get get = new Get(Bytes.toBytes("row1"));
//			查询表为test、行名为row1的记录
			Result result = tastTable.get(get);
			
			System.out.println("get\t"+result);
			
//			创建Scan对象
			Scan scan = new Scan();
//			扫描表名为test的所有记录
			ResultScanner scanner = tastTable.getScanner(scan);
			
			for(Result rs : scanner) {
				System.out.println(rs);
			}
			
			scanner.close();
//			停用表名为test的表
			admin.disableTable(tableName);
//			删除表名为test的表
			admin.deleteTable(tableName);
//			关闭连接
			tastTable.close();
//			关闭连接
			admin.close();
			
		}
}
public class HbaseDemo {

	private Connection con;

	@Before
	public void init() throws Exception {
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "hadoop05:2181,hadoop06:2181,hadoop07:2181");
		con = ConnectionFactory.createConnection(conf);

	}

	/*
	 * 创建命名空间
	 */
	@Test
	public void createNamespace() throws Exception {

		Admin admin = con.getAdmin();

		NamespaceDescriptor namespace = NamespaceDescriptor.create("ns1").build();
		admin.createNamespace(namespace);
		admin.close();
		con.close();
	}

	/*
	 * 创建表和列族
	 */
	@Test
	public void createTable() throws Exception {

		Admin admin = con.getAdmin();

		TableName tname = TableName.valueOf("ns1:table1");
		HTableDescriptor table = new HTableDescriptor(tname);

		HColumnDescriptor cf = new HColumnDescriptor("cf");

		table.addFamily(cf);

		admin.createTable(table);
		admin.close();
		con.close();
	}

	/*
	 * 向列族中添加列和值
	 */
	@Test
	public void input() throws Exception {

		TableName tableName = TableName.valueOf("ns1:table1");
		Table table1 = con.getTable(tableName);

		List<Put> ls = new ArrayList<>();
		Put put = null;
		for (int i = 1; i <= 100; i++) {

			put = new Put(Bytes.toBytes(i));
			put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("no"), Bytes.toBytes(i));
			put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("name" + i));
			put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("age"), Bytes.toBytes("10" + i));
			ls.add(put);
		}

		table1.put(ls);

		table1.close();
		con.close();
	}

	/*
	 * 查看表中的值
	 */
	@Test
	public void scan() throws Exception {

		TableName tname = TableName.valueOf("ns1:table1");
		Table table = con.getTable(tname);
		Scan scan = new Scan(Bytes.toBytes(10), Bytes.toBytes(80));
		ResultScanner scanner = table.getScanner(scan);

		/*
		 * for(Result rs : scanner) {
		 * 
		 * rs.getValue(family, qualifier)
		 * 
		 * }
		 */

		Iterator<Result> it = scanner.iterator();

		while (it.hasNext()) {

			Result next = it.next();

			System.out.println(Bytes.toInt(next.getValue(Bytes.toBytes("cf"), Bytes.toBytes("no"))) + "\t"
					+ Bytes.toString(next.getValue(Bytes.toBytes("cf"), Bytes.toBytes("name"))) + "\t"
					+ Bytes.toString(next.getValue(Bytes.toBytes("cf"), Bytes.toBytes("age"))));

		}
		table.close();

		con.close();
	}

	/*
	 * 获取表中的单条记录
	 */
	@Test
	public void gets() throws Exception {

		TableName tname = TableName.valueOf("ns1:table1");
		Table table = con.getTable(tname);

		Get get = new Get(Bytes.toBytes(30));

		Result next = table.get(get);

		System.out.println(Bytes.toInt(next.getValue(Bytes.toBytes("cf"), Bytes.toBytes("no"))) + "\t"
				+ Bytes.toString(next.getValue(Bytes.toBytes("cf"), Bytes.toBytes("name"))) + "\t"
				+ Bytes.toString(next.getValue(Bytes.toBytes("cf"), Bytes.toBytes("age"))));

		table.close();

		con.close();
	}

	/*
	 * 修改表中的记录
	 */
	@Test
	public void inputData() throws Exception {

		TableName tname = TableName.valueOf(Bytes.toBytes("ns1:table1"));

		Table table = con.getTable(tname);

		Put put = new Put(Bytes.toBytes(10));
		put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("jierui"));

		table.put(put);

		Get get = new Get(Bytes.toBytes(10));

		Result next = table.get(get);

		System.out.println(Bytes.toInt(next.getValue(Bytes.toBytes("cf"), Bytes.toBytes("no"))) + "\t"
				+ Bytes.toString(next.getValue(Bytes.toBytes("cf"), Bytes.toBytes("name"))) + "\t"
				+ Bytes.toString(next.getValue(Bytes.toBytes("cf"), Bytes.toBytes("age"))));

		table.close();

		con.close();

	}

	/*
	 * 删除表中的某条数据
	 */
	@Test
	public void delete() throws Exception {
		
		Table table = con.getTable(TableName.valueOf(Bytes.toBytes("ns1:table1")));
		
		Delete d = new Delete(Bytes.toBytes(10));
		d.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"));
		
		table.delete(d);
		
		table.close();

		con.close();

	}

	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值