Hadoop(六)之实验二HBase使用:使用Java API操作HBase上的数据

文章目录


Hadoop(五)之实验二HBase使用:HBase shell
Hadoop(六)之实验二HBase使用:使用Java API操作HBase上的数据


在这里插入图片描述

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;

public class Chapter4{
	public static Configuration configuration;
	public static Connection connection;
	public static Admin admin;
	
	public static void main(String[] args)throws IOException{
		init();
		createTable("student",new String[]{"score"});
		insertData("student","zhangsan","score","English","69");
		insertData("student","zhangsan","score","Math","86");
		insertData("student","zhangsan","score","Computer","77");
		close();
	}

	//建立连接
	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();
		}
	}

	/**
	 * 创建表
	 * @param myTableName 表名
	 * @param colFamily列族数组
	 * @throws Exception
	 */
	public static void createTable(String myTableName,String[] colFamily) throws IOException {
		TableName tableName = TableName.valueOf(myTableName);
		if(admin.tableExists(tableName))
		{
			System.out.println("table exists!");
		}
		else
		{
			HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
			for(String str: colFamily)
			{
				HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
				hTableDescriptor.addFamily(hColumnDescriptor);
			}
			admin.createTable(hTableDescriptor);
		}
	}
	
	/**
	 * 添加数据
	 * @param tableName 表名
	 * @param rowKey 行键
	 * @param colFamily 列族
	 * @param col 列限定符
	 * @param val 数据
	 * @throws Exception
	 */
	public static void insertData(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();
	}
}

样例:银行职员的信息,包括工号number,姓名name,级别level,工资wage,工作年限workingLife,分行名称bankName

在这里插入图片描述

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;

public class Chapter4{
	public static Configuration configuration;
	public static Connection connection;
	public static Admin admin;
	
	public static void main(String[] args)throws IOException{
		init();
		createTable("bank",new String[]{"info"});
		
		insertData("bank","1","info","name","JiangXiaoYu");
		insertData("bank","1","info","level","1");
		insertData("bank","1","info","wage","");
		insertData("bank","1","info","workingLife","1");
		insertData("bank","1","info","bankName","JiaoTong");
				
		insertData("bank","2","info","name","JiangXiaoYu");
		insertData("bank","2","info","level","2");
		insertData("bank","2","info","wage","2000");
		insertData("bank","2","info","workingLife","5");
		insertData("bank","2","info","bankName","NongHang");
	
		insertData("bank","1","info","name","ZhangDaoXian");
		insertData("bank","1","info","level","7");
		insertData("bank","1","info","wage","7000");
		insertData("bank","1","info","workingLife","7");
		insertData("bank","1","info","bankName","GongYe");
		
		close();
	}

	//建立连接
	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();
		}
	}

	/**
	 * 创建表
	 * @param myTableName 表名
	 * @param colFamily列族数组
	 * @throws Exception
	 */
	public static void createTable(String myTableName,String[] colFamily) throws IOException {
		TableName tableName = TableName.valueOf(myTableName);
		if(admin.tableExists(tableName))
		{
			System.out.println("table exists!");
		}
		else
		{
			HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
			for(String str: colFamily)
			{
				HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
				hTableDescriptor.addFamily(hColumnDescriptor);
			}
			admin.createTable(hTableDescriptor);
		}
	}
	
	/**
	 * 添加数据
	 * @param tableName 表名
	 * @param rowKey 行键
	 * @param colFamily 列族
	 * @param col 列限定符
	 * @param val 数据
	 * @throws Exception
	 */
	public static void insertData(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();
	}
}
hbase(main):002:0> scan 'bank'
ROW 					COLUMN+CELL
 1						column=info:name, timestamp=1573051237635,  value=JiangXiaoYu
 1						column=info:level, timestamp=1538030949219,  value=1
 1						column=info:wage, timestamp=1538030949174,  value=1000
 1						column=info:workingLife, timestamp=1538030949194,  value=1
 1						column=info:bankName, timestamp=1538030949261,  value=JiaoTong
 2						column=info:name, timestamp=1538030949393,  value=JiangXiaoYu
 2						column=info:level, timestamp=1538030949314,  value=2
 2						column=info:wage, timestamp=1538030949350,  value=2000
 2						column=info:workingLife, timestamp=1538030950752,  value=5
 2						column=info:bankName, timestamp=1538030949448,  value=NongHang
 3						column=info:name, timestamp=1538030949487,  value=ZhangDaoXian
 3						column=info:level, timestamp=1538030949481,  value=7
 3						column=info:wage, timestamp=1538030949375,  value=7000
 3						column=info:workingLife, timestamp=1538030949799,  value=7
 3						column=info:bankName, timestamp=1538030953023,  value=GongYe
3 row(s) in 0.8710 seconds
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值