HBase中Java操作数据库增删改查——查找数据

本文介绍了一种基于HBase的学生宿舍管理系统,演示了如何通过Java API查询特定学生信息、工作人员信息及根据宿舍号查找宿舍成员的方法。系统利用HBase的高效数据存储和检索能力,实现了对学生信息、宿舍信息及员工信息的快速查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

已经创建了一个学生宿舍管理系统

数据表表名:student_info

列族1:students

列族2:dormitorys

列族3:staff_members

已经添加了数据若干;

查找所有学生信息:

package myhbase;
import java.io.IOException;
import java.util.logging.Filter;

import javax.xml.crypto.dsig.keyinfo.KeyValue;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
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;
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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes;

import com.google.common.collect.Table;
public class GetData{
	public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
		String tableName = "student_info";
		String columnFamily1 = "students";
		String column1 = "student_id";

		String val1 = "123456789";
		System.out.println(val1);
		
		String columnFamily2 = "dormitorys" ;
		String column2 = "dormitory";
		String val2 = "Nanyuan Building4";
		
		getStudentsinfo(tableName,columnFamily1, column1,val1);
		//getStaffsinfo(tableName,columnFamily2, column2,val2);
		//getAll(tableName);
	}

	public static Configuration getConfiguration() {
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
		conf.set("hbase.zookeeper.quorum", "localhost");
		return conf;
	}
	
	public static void getStudentsinfo(String tableName, String columnFamily1, String column1, String val1)
			throws MasterNotRunningException, ZooKeeperConnectionException,
			IOException {
		HTable table =new HTable(getConfiguration(), tableName);
		Scan scan =new Scan();
		SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(columnFamily1),Bytes.toBytes(column1),CompareOp.EQUAL,
				new SubstringComparator(val1));
		filter.setFilterIfMissing(true);
		scan.setFilter(filter);
		ResultScanner scanner = table.getScanner(scan);
		
		for(Result result:scanner){		
			System.out.println(Bytes.toString(result.getRow()));
			byte[] value1 = result.getValue(Bytes.toBytes("students"),Bytes.toBytes("student_name"));
			byte[] value2 = result.getValue(Bytes.toBytes("students"),Bytes.toBytes("gender"));
			byte[] value3 = result.getValue(Bytes.toBytes("students"),Bytes.toBytes("dept"));
			byte[] value4 = result.getValue(Bytes.toBytes("students"),Bytes.toBytes("tel"));
			byte[] value5 = result.getValue(Bytes.toBytes("students"),Bytes.toBytes("admission_time"));
			byte[] value6 = result.getValue(Bytes.toBytes("dormitorys"),Bytes.toBytes("hostel_No."));
			System.out.println("student_name:"+Bytes.toString(value1));
			System.out.println("gender:"+Bytes.toString(value2));
			System.out.println("dep:"+Bytes.toString(value3));
			System.out.println("tel:"+Bytes.toString(value4));
			System.out.println("admission_time:"+Bytes.toString(value5));
			System.out.println("hostel_No.:"+Bytes.toString(value6));
			}
		System.out.println("===========get students info end===========");
	}
	
	public static void getStaffsinfo(String tableName, String columnFamily2, String column2, String val2)
			throws MasterNotRunningException, ZooKeeperConnectionException,
			IOException {
		HTable table =new HTable(getConfiguration(), tableName);
		Scan scan =new Scan();
		SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(columnFamily2),Bytes.toBytes(column2),CompareOp.EQUAL,
				new SubstringComparator(val2));
		filter.setFilterIfMissing(true);
		scan.setFilter(filter);
		ResultScanner scanner = table.getScanner(scan);
		
		for(Result result:scanner){		
			System.out.println(Bytes.toString(result.getRow()));
			byte[] value1 = result.getValue(Bytes.toBytes("staff members"),Bytes.toBytes("staff name"));
			byte[] value2 = result.getValue(Bytes.toBytes("staff members"),Bytes.toBytes("staff age"));
			byte[] value3 = result.getValue(Bytes.toBytes("staff members"),Bytes.toBytes("staff gender"));
			byte[] value4 = result.getValue(Bytes.toBytes("staff members"),Bytes.toBytes("staff tel"));
			System.out.println(Bytes.toString(value1));
			System.out.println(Bytes.toString(value2));
			System.out.println(Bytes.toString(value3));
			System.out.println(Bytes.toString(value4));
			System.out.println("---------------");
			}
		System.out.println("===========get staffsindo end===========");
	}
	
	public static void getAll(String tableName)throws MasterNotRunningException, ZooKeeperConnectionException,
	IOException{
		Configuration conf = HBaseConfiguration.create();
		HTable hTable = new HTable(conf,tableName);
		Scan scan =new Scan();
		//ResultScanner scanner1 = hTable.getScanner(scan);
		//for (Result res : scanner1) {
       //     System.out.println(res);
        //}
		try {
			   Scan scan1 = new Scan();
			   ResultScanner rss = hTable.getScanner(scan1);
			    
			   for(Result r:rss){
			    System.out.println("\n row: "+new String(r.getRow()));
			     
			    for(org.apache.hadoop.hbase.KeyValue kv:r.raw()){
			      
			     System.out.println("family=>"+new String(kv.getFamily(),"utf-8")
			           +"  value=>"+new String(kv.getValue(),"utf-8")
			     +"  qualifer=>"+new String(kv.getQualifier(),"utf-8")
			     +"  timestamp=>"+kv.getTimestamp());    
			    }
			   }
			   rss.close();
			  } catch (IOException e) {
			   // TODO Auto-generated catch block
			   e.printStackTrace();
			  }  
			  System.out.println("=============end show All Records=============");
			 }

	
	
	}


	


根据提供的宿舍号码来查询该宿舍下宿舍成员信息:

例如:查询115宿舍的成员信息:

package myhbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
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;
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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes;
public class ShowData{
	public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
		String tableName = "student_info";

		String columnFamily2 = "dormitorys";
		String column = "hostel_No.";
		String val = "115";
		find(tableName,columnFamily2, column,val);
	}

	public static Configuration getConfiguration() {
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
		conf.set("hbase.zookeeper.quorum", "localhost");
		return conf;
	}
	
	public static void find(String tableName, String columnFamily2, String column, String val)
			throws MasterNotRunningException, ZooKeeperConnectionException,
			IOException {
		HTable table =new HTable(getConfiguration(), tableName);
		Scan scan =new Scan();
		SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(columnFamily2),Bytes.toBytes(column),CompareOp.EQUAL,
				new SubstringComparator(val));
		filter.setFilterIfMissing(true);
		scan.setFilter(filter);
		ResultScanner scanner = table.getScanner(scan);
		
		for(Result result:scanner){		
			System.out.println(Bytes.toString(result.getRow()));
			byte[] value1 = result.getValue(Bytes.toBytes("students"),Bytes.toBytes("student_id"));
			byte[] value2 = result.getValue(Bytes.toBytes("students"),Bytes.toBytes("student_name"));
			System.out.println(Bytes.toString(value1));
			System.out.println(Bytes.toString(value2));
			System.out.println("---------------");
			}
		System.out.println("===========find end===========");
	}
	
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值