getBBytes和Bytes.toBytes的区别

探讨Bytes.toBytes方法底层实现,揭示其如何将数据转换为UTF-8编码的字节数组,解析字符串编码过程中的关键技术细节。

Bytes.toBytes底层使用了getBytes
Bytes,toBytes是在转化成字节数组的时候,把它编码成utf-8的格式

你提供的代码仍然存在 **关键性错误**,尤其是在 HBase 数据模型的理解上。我们来一步步分析问题、给出正确实现,并解释原理。 --- ## ✅ 修正后的 `insertInfo()` 方法(Java + HBase) ```java import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class Task { private Connection connection; private TableName tableName; public Task(Connection connection, TableName tableName) { this.connection = connection; this.tableName = tableName; } public void insertInfo() throws IOException { /********* Begin *********/ Table table = connection.getTable(tableName); // 获取Table对象 try { // 插入第一行数据:Lily Put put1 = new Put(Bytes.toBytes("101")); // 行键为 "101" put1.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("Lily")); put1.addColumn(Bytes.toBytes("info"), Bytes.toBytes("sex"), Bytes.toBytes("Female")); put1.addColumn(Bytes.toBytes("info"), Bytes.toBytes("birthday"), Bytes.toBytes("2004-01-24")); // 插入第二行数据:Tom Put put2 = new Put(Bytes.toBytes("102")); // 行键为 "102" put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("Tom")); put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("sex"), Bytes.toBytes("male")); put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("birthday"), Bytes.toBytes("2005-01-23")); // 批量提交两个 Put 操作 table.put(java.util.Arrays.asList(put1, put2)); System.out.println("两条记录插入成功!"); } finally { if (table != null) { table.close(); // 释放资源 } } /********* End *********/ } } ``` --- ## ❌ 原始代码的问题详解 | 错误 | 说明 | |------|------| | `Bytes.toBytes("101", "102")` | ❌ `Bytes.toBytes(String)` 只接受一个字符串参数,不能传多个。这会导致编译失败。 | | `byte[] row = ...` 赋值给单个字节数组却想表示两行 | ❌ 行键是唯一标识一条记录的,每条记录必须使用独立的 `Put` 实例。 | | `byte[] columnFamily = Bytes.toBytes("name","sex","birthday")` | ❌ 语法错误!这不是创建列簇的方式。列簇是像 `"info"` 这样的统一命名空间,而 `"name"` 是列限定符(qualifier)。 | | 同一个 `Put` 对象中重复添加相同列(如两次 `info:name`) | ⚠️ 虽然不会报错,但会覆盖或产生多个时间戳版本(取决于配置),通常这是逻辑错误。 | | 使用了 `throws Exception` 而非更精确的 `IOException` | 📝 最佳实践应抛出具体异常类型。 | --- ## 🔍 核心概念澄清:HBase 数据模型 HBase 中的数据结构类似于二维表,但本质是四维的: ``` (rowkey, columnfamily:qualifier, timestamp) -> value ``` ### 示例数据: | RowKey | ColumnFamily:Qualifier | Value | |--------|-------------------------|------------| | 101 | info:name | Lily | | 101 | info:sex | Female | | 101 | info:birthday | 2004-01-24 | | 102 | info:name | Tom | | 102 | info:sex | male | | 102 | info:birthday | 2005-01-23 | > ✅ 每个 `rowkey` 对应一个人的信息,每个属性是一个 `(columnfamily:qualifier, value)` 对。 --- ## 💡 如何批量插入多行? 你可以将多个 `Put` 放入 `List<Put>` 并一次性提交,提高性能: ```java List<Put> puts = new ArrayList<>(); puts.add(put1); puts.add(put2); table.put(puts); ``` --- ## ✅ 前提条件:确保 HBase 表已存在 在运行 Java 程序前,请先通过 HBase Shell 创建表: ```bash create 'student', 'info' ``` 否则会抛出 `TableNotFoundException`。 --- ##
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值