05hbase数据插入

本文详细介绍了一种使用Java API进行HBase数据管理语言(DML)操作的方法,包括数据的插入、删除、单行查询及范围查询。通过具体示例展示了如何批量插入数据、精确删除特定字段以及如何通过扫描器获取指定范围内的数据。

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

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

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScanner;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
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.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;

public class DMLfour {
	
	Connection conn = null;
	@Before
	public void init() throws Exception {
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "hadoop1:2181,hadoop2:2181,hadoop3:2181");
		conn = ConnectionFactory.createConnection(conf);		
	}
	



	/*
	 * 增
	 */
	@Test
	public void testPutBig() throws Exception {
		Table table = conn.getTable(TableName.valueOf("tt"));
		
		Put put = new Put(Bytes.toBytes("001"));
		put.addColumn(Bytes.toBytes("basic_info"), Bytes.toBytes("name"), Bytes.toBytes("Nul"));
		put.addColumn(Bytes.toBytes("high_info"), Bytes.toBytes("sex"), Bytes.toBytes("male"));
		
		Put put2 = new Put(Bytes.toBytes("002"));
		put2.addColumn(Bytes.toBytes("basic_info"), Bytes.toBytes("name"), Bytes.toBytes("sle"));
		put2.addColumn(Bytes.toBytes("basic_info"), Bytes.toBytes("age"), Bytes.toBytes("15"));
		
		ArrayList<Put> puts = new ArrayList<>();
		puts.add(put);
		puts.add(put2);
		
		table.put(puts);	
		
		table.close();
		conn.close();
	}
	
	
	
	
	/*
	 * 增加1000行数据
	 */
	@Test
	public void testPut() throws Exception {
		Table table = conn.getTable(TableName.valueOf("tt"));
		ArrayList<Put> puts = new ArrayList<>();
		
		for(int i = 0; i < 1000; i++) {
			Put put = new Put(Bytes.toBytes("" + i));
			put.addColumn(Bytes.toBytes("basic_info"), Bytes.toBytes("name"), Bytes.toBytes("Nul"+i));
			put.addColumn(Bytes.toBytes("basic_info"), Bytes.toBytes("age"), Bytes.toBytes((18+i)+""));
			put.addColumn(Bytes.toBytes("high_info"), Bytes.toBytes("sex"), Bytes.toBytes("male"));	
			puts.add(put);
		}
		
		table.put(puts);	
		
		table.close();
		conn.close();
	}
	
	






	/*
	 * 删
	 */
	@Test
	public void testDele() throws Exception {
		Table table = conn.getTable(TableName.valueOf("tt"));
		
		Delete delete1 = new Delete(Bytes.toBytes("001"));
		delete1.addColumn(Bytes.toBytes("basic_info"), Bytes.toBytes("name"));
		
		Delete delete2 = new Delete(Bytes.toBytes("002"));
		
		ArrayList<Delete> dels = new ArrayList<>();
		dels.add(delete1);
		dels.add(delete2);
		
		table.delete(dels);
		
		table.close();
		conn.close();
	}
	





	
	/*
	 * 查
	 */
	@Test
	public void testGet() throws Exception {
		Table table = conn.getTable(TableName.valueOf("tt"));
		
		Get get = new Get("002".getBytes());	
		Result result = table.get(get);
		
		//从结果中取用户指定的某个key的value值
		byte[] value = result.getValue("basic_info".getBytes(), "name".getBytes());
		System.out.println(new String(value));
		System.out.println("-------------------------");
		
		//遍历整行结果中的所有kv单元格
		CellScanner cellScanner = result.cellScanner();
		while(cellScanner.advance()) {
			Cell cell = cellScanner.current();
			byte[] rowArray = cell.getRowArray();       //本kv所属的行键的字节数组    				
			byte[] familyArray = cell.getFamilyArray();    //列族名的字节数组
			byte[] qualifierArray = cell.getQualifierArray();  //列名的字节数组
			byte[] valueArray = cell.getValueArray();     //value的字节数组
			
			System.out.println("行键" + new String(rowArray));
			System.out.println("列族名" + new String(familyArray));
			System.out.println("列名" + new String(qualifierArray));
			System.out.println("value" + new String(valueArray));
		}
		
		table.close();
		conn.close();
		
	}
	






	/*
	 * 按行键范围查询数据
	 */
	@Test
	public void testscan() throws Exception {
		Table table = conn.getTable(TableName.valueOf("tt"));
		
		//包含起始行键,不包含结束行键,但是如果真的想查询出末尾的那个行键,那么可以在末尾行键拼接一个不可见字节(\000)
		Scan scan = new Scan("001".getBytes(), "100\001".getBytes());
		
		ResultScanner scanner = table.getScanner(scan);
		
		Iterator<Result> iterator = scanner.iterator();
		while(iterator.hasNext()) {
			Result result = iterator.next();
			//遍历整行结果中的所有kv单元格
			CellScanner cellScanner = result.cellScanner();
			while(cellScanner.advance()) {
				Cell cell = cellScanner.current();
				byte[] rowArray = cell.getRowArray();       //本kv所属的行键的字节数组    				
				byte[] familyArray = cell.getFamilyArray();    //列族名的字节数组
				byte[] qualifierArray = cell.getQualifierArray();  //列名的字节数组
				byte[] valueArray = cell.getValueArray();     //value的字节数组
				
				System.out.println("行键" + new String(rowArray));
				System.out.println("列族名" + new String(familyArray));
				System.out.println("列名" + new String(qualifierArray));
				System.out.println("value" + new String(valueArray));
			}
			System.out.println("-------------------");
		}
		table.close();
		conn.close();
	}








	
	/*
	 * 测试末尾行键
	 */
	@Test
	public void test(){
		String a = "000";
		String b = "000\0";
		
		System.out.println(a);
		System.out.println(b);
		
		
		byte[] bytes = a.getBytes();
		byte[] bytes2 = b.getBytes();
		
		System.out.println("");	
	}
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hao难懂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值