大数据之Hadoop学习(十)HBase Java API编程

一、任务要求:

(1)createTable(String tableName, String[] fields)

创建表,参数tableName为表的名称,字符串数组fields为存储记录各个域名称的数组。要求当HBase已经存在名为tableName的表的时候,先删除原有的表,然后再创建新的表。
主要代码:
在这里插入图片描述
运行结果:
在这里插入图片描述
用HBase shell中查看
在这里插入图片描述

(2)addRecord(String tableName, String row, String[] fields, String[] values)

向表tableName、行row(用S_Name表示)和字符串数组files指定的单元格中添加对应的数据values。其中fields中每个元素如果对应的列族下还有相应的列限定符的话,用“columnFamily:column”表示。例如,同时向“Math”、“Computer Science”、“English”三列添加成绩时,字符串数组fields为{“Score:Math”,”Score;Computer Science”,”Score:English”},数组values存储这三门课的成绩。

主要代码:

在这里插入图片描述
运行结果:
在这里插入图片描述
用HBase shell中查看
在这里插入图片描述
确实添加了三组数据

(3)scanColumn(String tableName, String column)

浏览表tableName某一列的数据,如果某一行记录中该列数据不存在,则返回null。要求当参数column为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据;当参数column为某一列具体名称(例如“Score:Math”)时,只需要列出该列的数据。
主要代码:
在这里插入图片描述
运行结果:
在这里插入图片描述
与用HBase shell中查看的结果一致。

(4)modifyData(String tableName, String row, String column)

修改表tableName,行row(可以用学生姓名S_Name表示),列column指定的单元格的数据。
主要代码:
在这里插入图片描述

运行结果用HBase shell查看:
在这里插入图片描述
Math的分数由100变成了60,修改成功。

(5)deleteRow(String tableName, String row)

删除表tableName中row指定的行的记录。

主要代码:

在这里插入图片描述
运行结果用HBase shell 查看
在这里插入图片描述
没有数据,成功删除了。

二、完整代码实现
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.Scanner;

public class HBaseAPI {
	public static Configuration configuration;
	public static Connection connection;
	public static Admin admin;
	public static long ts;

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

		Scanner in = new Scanner(System.in);
		while (true) {
			System.out
					.println("******************HBase对数据库的操作******************");
			System.out.println("1.创建一个表");
			System.out.println("2.添加数据");
			System.out.println("3.浏览数据");
			System.out.println("4.修改数据");
			System.out.println("5.删除表中row指定的行的记录");
			int a = in.nextInt();
			switch (a) {
			case 1:
				createTable("tableName", new String[] { "Score" });
				break;

			case 2:
				String[] fields = { "Score:Math", "Score:Computer Science",
						"Score:English" };
				String[] values = { "100", "90", "80" };
				addRecord("tableName", "Score", fields, values);
				break;

			case 3:
				scanColumn("tableName", "Score");
				break;

			case 4:
				modifyData("tableName", "Score", "Math", "60");
				break;

			case 5:
				deleteRow("tableName", "Score");
				break;
			}
		}

	}

	// 建立连接
	public static void init() {
		configuration = HBaseConfiguration.create();
		configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
		try {
			connection = ConnectionFactory.createConnection(configuration);
			admin = connection.getAdmin();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 关闭连接
	public static void close() {
		try {
			if (admin != null) {
				admin.close();
			}
			if (null != connection) {
				connection.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 创建表
	public static void createTable(String myTableName, String[] fields)
			throws IOException {

		init();
		TableName tableName = TableName.valueOf(myTableName);

		if (admin.tableExists(tableName)) {
			System.out.println("talbe is exists!");
		} else {
			HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
			for (String str : fields) {
				HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
				hTableDescriptor.addFamily(hColumnDescriptor);
			}
			admin.createTable(hTableDescriptor);
			System.out.println("create table success");
		}
		close();
	}

	// 添加数据
	public static void addRecord(String tableName, String row, String[] fields,
			String[] values) throws IOException {
		init();
		Table table = connection.getTable(TableName.valueOf(tableName));
		for (int i = 0; i != fields.length; i++) {
			Put put = new Put(row.getBytes());
			String[] cols = fields[i].split(":");
			put.addColumn(cols[0].getBytes(), cols[1].getBytes(),
					values[i].getBytes());
			table.put(put);
		}
		table.close();
		close();
	}

	// 浏览数据
	public static void scanColumn(String tableName, String column)
			throws IOException {
		init();
		Table table = connection.getTable(TableName.valueOf(tableName));
		Scan scan = new Scan();
		scan.addFamily(Bytes.toBytes(column));
		ResultScanner scanner = table.getScanner(scan);
		for (Result result = scanner.next(); result != null; result = scanner
				.next()) {
			showCell(result);
		}
		table.close();
		close();
	}

	public static void showCell(Result result) {
		Cell[] cells = result.rawCells();
		for (Cell cell : cells) {
			System.out.println("RowName:" + new String(CellUtil.cloneRow(cell))
					+ " ");
			System.out.println("Timetamp:" + cell.getTimestamp() + " ");
			System.out.println("column Family:"
					+ new String(CellUtil.cloneFamily(cell)) + " ");
			System.out.println("row Name:"
					+ new String(CellUtil.cloneQualifier(cell)) + " ");
			System.out.println("value:" + new String(CellUtil.cloneValue(cell))
					+ " ");
		}
	}

	// 修改数据
	public static void modifyData(String tableName, String row, String column,
			String val) throws IOException {

		init();
		Table table = connection.getTable(TableName.valueOf(tableName));
		Put put = new Put(row.getBytes());
		Scan scan = new Scan();
		ResultScanner resultScanner = table.getScanner(scan);
		for (Result r : resultScanner) {
			for (Cell cell : r
					.getColumnCells(row.getBytes(), column.getBytes())) {
				ts = cell.getTimestamp();
			}
		}
		put.addColumn(row.getBytes(), column.getBytes(), ts, val.getBytes());
		table.put(put);
		table.close();
		close();
	}

	//删除表中row指定的行的记录
	public static void deleteRow(String tableName, String row)
			throws IOException {
		init();
		Table table = connection.getTable(TableName.valueOf(tableName));
		Delete delete = new Delete(row.getBytes());
		table.delete(delete);
		table.close();
		close();
	}

}

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大数据Hadoop是一个开源的分布式计算框架,用于存储和处理大规模数据集。想要从入门到精通大数据Hadoop,需要以下几个步骤。 首先,了解Hadoop的基本概念和架构。HadoopHadoop分布式文件系统(HDFS)和Hadoop分布式计算框架(MapReduce)组成。HDFS用于存储大规模数据集,而MapReduce用于分布式计算。了解这些基本概念对于理解Hadoop的运行方式至关重要。 其次,学习Hadoop的安装和配置。Hadoop的安装可以通过官方网站提供的二进制文件进行,同时需要配置相关环境变量和属性文件。熟悉Hadoop的配置能够更好地适应不同的需求和环境。 第三,学习Hadoop编程模型和APIHadoop使用Java编程语言来实现分布式计算任务。了解Hadoop编程模型和API可以帮助我们编写MapReduce程序实现数据的分布式处理和并行计算。 第四,了解Hadoop生态系统中的其他组件。Hadoop生态系统包括HBase、Hive、Pig、Spark等多个组件,它们可以与Hadoop一起使用,提供更强大的数据处理和分析能力。了解这些组件的基本概念和用法,能够更好地解决实际的数据处理问题。 最后,通过实践项目来提升技能。只有通过实践,才能真正掌握Hadoop使用和应用。可以通过解决实际的数据问题,运行和调优MapReduce程序,深入理解Hadoop的工作原理和性能优化。 总结起来,要想从入门到精通大数据Hadoop,需要了解基本概念和架构,学习安装配置,掌握编程模型和API,了解Hadoop生态系统中的其他组件,并通过实践项目来提升技能。这些步骤将帮助我们更好地理解和运用Hadoop实现大数据的存储和处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值