HBase中Java操作数据库增删改查——删除数据/删除数据表

本文介绍如何在HBase中使用Java进行数据库操作,包括删除特定行键(001)的数据以及删除整个数据表student_info。数据表student_info包含students, dormitorys, staff_members三个列族。" 122439897,11535265,C语言实现十六进制到八进制转换,"['C语言', '进制转换']

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

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

数据表表名:student_info

列族1:students

列族2:dormitorys

列族3:staff_members

例如:删除数据表中行键为001的数据:

package myhbase;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.Column;
import org.apache.hadoop.hbase.util.Bytes;

import com.sun.xml.bind.v2.schemagen.xmlschema.List;

public class DeleteData {
	public static void main(String[] args) throws IOException {
		
		//deletedata("student_info");
		deleterowkey("student_info","001");
	}
	
	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 deletedata(String tableName) throws IOException{
		Configuration conf = HBaseConfiguration.create();
		HTable table = new HTable(conf, "student_info");
		
		Delete delete = new Delete(Bytes.toBytes("001"));
		delete.deleteColumn(Bytes.toBytes("students"), Bytes.toBytes("student_name"));
		table.delete(delete);
		table.close();
		System.out.println("data deleted!");
	}
	
	public static void deleterowkey(String tableName,String rowkey){
		try{
			Configuration conf = HBaseConfiguration.create();
			HTable table = new HTable(conf, "student_info");
			ArrayList list = new ArrayList();
			Delete d1 = new Delete(rowkey.getBytes());
			list.add(d1);
			table.delete(list);
			System.out.println("rowkey deleted!");
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
}

删除数据表student_info:

package myhbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;


public class DeleteTable {
	public static void main(String[] args) throws IOException{
		deletetable("student_info");
	}

	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 deletetable(String tableName) throws IOException{
		Configuration conf = HBaseConfiguration.create();
		HBaseAdmin admin = new HBaseAdmin(conf);
		if(admin.tableExists(tableName)){
			admin.disableTable(tableName);
			admin.deleteTable(tableName);
			System.out.println("========Table deleted=======");	
		}else{
			System.out.println("The table was not found!");
		}

	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值