大数据技术原理与应用实验七

实验七:

实验题目:熟悉常用的HBase操作-Java API

1.实验目的

·  理解HBase在Hadoop体系结构中的角色;

·  熟悉HBase操作常用的Java API。

2.实验平台

· Hbase 1.1.5以上

3.实验内容和要求:

· 现有以下关系型数据库中的表和数据,要求将其转换为适合于HBase存储的表并插入数据:

学生表(Student)


 请使用Hbase Java Eclipse API完成以下指定功能:

  (1)创建新表

(2)根据表名,进行删除表格

(3)查看所有表格(列出表名)

(4)以列式插入数据。

(5)删除数据

(6)查看表格信息

4、实验要求:

(1) 程序要添加适当的注释,规范写代码;

(2)  运行代码后附加打印的结果和图片;

(3) 根据实验报告模板详细书写实验报告。

代码实现:

package hbase.xbox.com;

import java.io.IOException;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseDemo02 {

	public static Configuration conf;
	public static Connection connection;
	public static Admin admin;
	
	public static void init() throws IOException {
		conf = HBaseConfiguration.create();
		conf.set("hbase.rootdir", "hdfs://hadoop01/hbase");
		conf.set("hbase.zookeeper.quorum", "hadoop01");
		connection = ConnectionFactory.createConnection(conf);
		admin = connection.getAdmin();
	}
	
	public static void close() throws IOException {
		if(admin != null) {
			admin.close();
		}
		if(connection != null) {
			connection.close();
		}
	}
	
	public static void createTbale(String myTableName, String[] colFamily) throws IOException {
		TableName tableName = TableName.valueOf(myTableName);
		if(admin.tableExists(tableName)) {
			System.out.println("Table Exists");
		}else {
			HTableDescriptor htd = new HTableDescriptor(tableName);
			for(String col : colFamily) {
				HColumnDescriptor hcd = new HColumnDescriptor(col);
				htd.addFamily(hcd);
			}
			admin.createTable(htd);
			System.out.println("Table have been created.");
		}
	}
	
	public static void delTable(String myTableName) throws IOException {
		TableName tableName = TableName.valueOf(myTableName);
		if(admin.tableExists(tableName)) {
			admin.disableTable(tableName);
			admin.deleteTable(tableName);
			System.out.println("Table have been deleted.");
		}else {
			System.out.println("Table is not exists.");
		}
	}
	
	public static void listTable() throws IOException {
		TableName[] nameCollection = admin.listTableNames();
		int i = 1;
		for(TableName tableName : nameCollection) {
			System.out.println("第"+ i + "张表:" +tableName);
			i++;
		}
	}
	
	public static void putData(String tableName, String rowKey, String colFamily, String col, 
			String val) throws IOException {
		Table table = connection.getTable(TableName.valueOf(tableName));
		Put put = new Put(Bytes.toBytes(rowKey));
		put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
		table.put(put);
		table.close();
		System.out.println("The data has insert!");
	}
	
	public static void delData(String tableName, String rowKey, String colFamily, String col) 
			throws IOException {
		Table table = connection.getTable(TableName.valueOf(tableName));
		Delete del = new Delete(rowKey.getBytes());
		del.addColumn(colFamily.getBytes(), col.getBytes());
		table.delete(del);
		table.close();
		System.out.println("Data is delete!");
	}
	
	public static void scanTable(String tableName) {
		try {
			Table table = connection.getTable(TableName.valueOf(tableName));
			ResultScanner resScan = table.getScanner(new Scan());
			Result rs = resScan.next();
			for(;rs != null;rs = resScan.next()) {
				for(KeyValue kv : rs.list()) {
					System.out.println("--------------");
		            System.out.println("rowkey:"+ new String(kv.getRow()));
		            System.out.println("Column Family: "+ new String(kv.getFamily()));
		            System.out.println("Column :" + new String(kv.getQualifier ()));
		            System.out.println("value :"+ new String(kv.getValue()));
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void main(String[] args) throws IOException {
		init();
		//创建student表
		String[] colFamily = new String[]{"msg","name"};
		createTbale("Student", colFamily);
		delTable("stu");
		listTable();
		putData("Student", "2015001", "name", "", "Zhangsan");
		putData("Student", "2015001", "msg", "sex", "male");
		putData("Student", "2015001", "msg", "age", "23");
		putData("Student", "2015002", "name", "", "Mary");
		putData("Student", "2015002", "msg", "sex", "female");
		putData("Student", "2015002", "msg", "age", "22");
		putData("Student", "2015003", "name", "", "Lisi");
		putData("Student", "2015003", "msg", "sex", "male");
		putData("Student", "2015003", "msg", "age", "24");
		delData("Student", "2015001", "msg", "sex");
		scanTable("Student");
		listTable();
		close();
	}

}
发布了80 篇原创文章 · 获赞 69 · 访问量 8929

猜你喜欢

转载自blog.csdn.net/qq_43437122/article/details/105516840
今日推荐