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

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

数据表表名: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===========");
	}
	
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮您回答这个问题。下面是使用 Java 操作 HBase 数据库增删改查示例代码: 1. 导入必要的包 ``` 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.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; ``` 2. 创建 HBase 配置对象 ``` Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum", "localhost"); // HBase 的 ZooKeeper 地址 configuration.set("hbase.zookeeper.property.clientPort", "2181"); // HBase 的 ZooKeeper 端口号 ``` 3. 创建 HBase 连接对象和表对象 ``` Connection connection = ConnectionFactory.createConnection(configuration); Table table = connection.getTable(TableName.valueOf("test_table")); // test_table 是表名 ``` 4. 插入数据 ``` Put put = new Put(Bytes.toBytes("row1")); // row1 是行键名 put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1"), Bytes.toBytes("value1")); // cf 是列族名,col1 是列名,value1 是值 table.put(put); ``` 5. 查询数据 ``` Get get = new Get(Bytes.toBytes("row1")); // row1 是行键名 Result result = table.get(get); for (Cell cell : result.listCells()) { String cf = Bytes.toString(CellUtil.cloneFamily(cell)); // 获取列族名 String col = Bytes.toString(CellUtil.cloneQualifier(cell)); // 获取列名 String value = Bytes.toString(CellUtil.cloneValue(cell)); // 获取值 System.out.println(cf + ":" + col + "=" + value); } ``` 6. 修改数据 ``` Put put = new Put(Bytes.toBytes("row1")); // row1 是行键名 put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1"), Bytes.toBytes("new_value")); // cf 是列族名,col1 是列名,new_value 是新的值 table.put(put); ``` 7. 删除数据 ``` Delete delete = new Delete(Bytes.toBytes("row1")); // row1 是行键名 delete.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1")); // cf 是列族名,col1 是列名 table.delete(delete); ``` 以上就是使用 Java 操作 HBase 数据库增删改查示例代码,希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值