HBase基础编程

文章目录

  • HBase基础编程
    • 一、实验目标
    • 二、实验要求及注意事项
    • 三、实验内容及步骤
  • 附:系列文章

HBase基础编程

一、实验目标

  1. 掌握如何通过HBase shell命令来设计HBase表结构实例,从而理解HBase的列式存储结构
  2. 掌 握 java编程创建HBase表和删除HBase表。

二、实验要求及注意事项

  1. 给出每个实验的主要实验步骤、实现代码和测试效果截图。
  2. 对本次实验工作进行全面的总结分析。
  3. 所有程序需要本地测试和集群测试,给出相应截图。
  4. 建议工程名,类名或包名等做适当修改,显示个人学号或者姓名

三、实验内容及步骤

实验任务1:HBase表设计。通过HBase shell命令来设计并创建三张相关的表,其中后两张表可以关联起来,例如店铺与商品表。建议自拟表名和表内容。

HBase基础编程_第1张图片

主要实现步骤和运行效果图:

完整程序

create 'shop','info','item'
put 'shop','s_01','item:item_id','i_01'
put 'shop','s_01','info:name','iphone'
put 'shop', 's_01','info:address','tianmao'
put 'shop','s_01','info:regdate','11-11'
create 'Item','info','item'
put 'Item','i_01','item:shop_id','s_01'
put 'Item','i_01','info:name','iphone'
put 'Item','i_01','info:price','4534'
put 'Item','i_01','info:detail','ios10.3.2'
put 'Item','i_01','info:title','phoneOfApple'

程序分析

这是一组HBase的命令,主要是创建和操作两个表格——‘shop’和’Item’。

首先,在’shop’表格中添加一行数据,该行的行键为’s_01’,列限定符为’item:item_id’,值为’i_01’。然后,再在’shop’表格中添加三个列:‘info:name’,值为’iphone’;‘info:address’,值为’tianmao’;‘info:regdate’,值为’11-11’。

接下来,创建’Item’表格,并添加一行数据。该行的行键为’i_01’,列限定符为’item:shop_id’,值为’s_01’。然后,再添加四个列:‘info:name’,值为’iphone’;‘info:price’,值为’4534’;‘info:detail’,值为’ios10.3.2’;‘info:title’,值为’phoneOfApple’。

运行结果

HBase基础编程_第2张图片
HBase基础编程_第3张图片

实验任务2:使用Java编程创建表和删除表,表名和列族自拟。

主要实现步骤和运行效果图:

完整程序

WjwCreateTable

package hbase;

import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.hbase.*;

public class WjwCreateTable {
	
	static Configuration conf;
	static{
		conf = new Configuration();
	}
	
	public static void createtb(String tbname, String[] tbfamily) throws Exception{
		HBaseAdmin admin = new HBaseAdmin(conf);
		HTableDescriptor hd = new HTableDescriptor(tbname);
		for(int i=0;i<tbfamily.length;i++){
			hd.addFamily(new HColumnDescriptor(tbname));
		}
		if(admin.tableExists(tbname)){
			System.out.println(tbname);
		}else{
			admin.createTable(hd);
			System.out.println("create table success");
		}
	}
	public static void deltb(String tbname) throws Exception{
		HBaseAdmin admin = new HBaseAdmin(conf);
		admin.disableTable(tbname);
		admin.deleteTable(tbname);
		if(admin.tableExists(tbname)){
			System.out.println(tbname+"is not exists");
		}else{
			System.out.println("success del");
		}
	}
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str[] = {"cf1"};
		createtb("wjw01", str);
	}
}

这是一个使用HBase Java API创建和删除表格的示例程序。

首先,在静态代码块中创建了一个Configuration对象,这是一个包含HBase的配置信息的对象。

然后,定义了一个名为’createtb’的方法,该方法接收两个参数:表格名和表格列族名。在该方法中,首先通过HBaseAdmin类创建了一个HBaseAdmin对象,然后通过HTableDescriptor类创建了一个HTableDescriptor对象,用于描述表格的结构。接着,通过循环遍历表格列族名数组,添加每个列族并将其加入HTableDescriptor对象中。最后,判断该表是否已经存在,如果存在则打印出表格名,否则调用HBaseAdmin的createTable方法创建表格,并打印出"create table success"。

然后,定义了一个名为’deltb’的方法,该方法接收一个参数:表格名。在该方法中,同样通过HBaseAdmin类创建了一个HBaseAdmin对象,并调用其disableTable方法禁用该表格,然后调用其deleteTable方法删除该表格。最后,判断该表是否已经被删除,如果未删除则打印出表格名和"is not exists",否则打印出"success del"。

最后,在main方法中调用createtb方法创建了一个名为"wjw01",列族名为"cf1"的表格。

WjwDeleteTable

package hbase;
import java. io. IOException;
import org. apache. hadoop. conf.*;
import org. apache. hadoop. hbase.HBaseConfiguration;
import org. apache. hadoop. hbase. MasterNotRunningException;
import org. apache. hadoop. hbase. ZooKeeperConnectionException;
import org. apache. hadoop. hbase. client. HBaseAdmin;
import org. apache. hadoop. hbase. client. HTable;
public class WjwDeleteTable {
	public Configuration conf;
	public HTable table;
	public HBaseAdmin admin;
	public void HBaseTest() throws MasterNotRunningException,
		ZooKeeperConnectionException, IOException {
		conf = HBaseConfiguration. create();
		conf. set("hbase. master", "master:60000");
		System. out. println(conf. get("hbase. master"));
		conf. set("hadoop. zkk. property. clientPort", "2181");
		System. out. println(conf. get("hadoop. zkk. property. clientPort"));
		conf. set("hbase. zookeeper. quorum", "master");
		System. out. println(conf. get("hbase. zookeeper. quorum"));
		admin= new HBaseAdmin(conf);
		table = new HTable(conf, "test01");
	}
	public static void main(String[] args) throws Exception {
		WJW02 hc = new WjwDeleteTable();
		hc. HBaseTest();
	}
}

这是一个使用HBase Java API删除表的示例程序。

首先,在HBaseTest方法中创建了一个Configuration对象,然后通过该对象设置了HBase集群的master和zookeeper相关参数,包括hbase.master、hadoop.zkk.property.clientPort和hbase.zookeeper.quorum。然后,创建了一个HBaseAdmin对象和一个HTable对象。

接着,在main方法中创建了一个WjwDeleteTable对象,并调用其HBaseTest方法进行HBase连接测试。

运行结果

HBase基础编程_第4张图片
HBase基础编程_第5张图片

附:系列文章

实验 文章目录 直达链接
实验01 Hadoop安装部署 https://want595.blog.csdn.net/article/details/132767284
实验02 HDFS常用shell命令 https://want595.blog.csdn.net/article/details/132863345
实验03 Hadoop读取文件 https://want595.blog.csdn.net/article/details/132912077
实验04 HDFS文件创建与写入 https://want595.blog.csdn.net/article/details/133168180
实验05 HDFS目录与文件的创建删除与查询操作 https://want595.blog.csdn.net/article/details/133168734
实验06 SequenceFile、元数据操作与MapReduce单词计数 https://want595.blog.csdn.net/article/details/133926246
实验07 MapReduce编程:数据过滤保存、UID 去重 https://want595.blog.csdn.net/article/details/133947981
实验08 MapReduce 编程:检索特定群体搜索记录和定义分片操作 https://want595.blog.csdn.net/article/details/133948849
实验09 MapReduce 编程:join操作和聚合操作 https://want595.blog.csdn.net/article/details/133949148
实验10 MapReduce编程:自定义分区和自定义计数器 https://want595.blog.csdn.net/article/details/133949522

你可能感兴趣的:(#,《,大数据技术,》,hbase,数据库,大数据)