HBase 笔记客户端API基础知识 -- 3





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用 HBase 客户端 API 连接到 HBase 服务器、创建 HBase 表、为表指定列族、从 HDFS 上读取 CSV 文件并将行数据插入到 HBase 表中的代码示例: ``` import org.apache.hadoop.conf.Configuration; 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.Put; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class HBaseClient { public static void main(String[] args) throws Exception { // 配置 HBase 连接 Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "zookeeper1,zookeeper2,zookeeper3"); conf.set("hbase.zookeeper.property.clientPort", "2181"); // 连接 HBase Connection connection = ConnectionFactory.createConnection(conf); // 创建表 TableName tableName = TableName.valueOf("my_table"); HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); if (!admin.tableExists(tableName)) { HTableDescriptor tableDesc = new HTableDescriptor(tableName); HColumnDescriptor columnDesc = new HColumnDescriptor("data"); tableDesc.addFamily(columnDesc); admin.createTable(tableDesc); } // 获取表 Table table = connection.getTable(tableName); // 从 HDFS 读取 CSV 文件 FileSystem fs = FileSystem.get(conf); Path path = new Path("hdfs://namenode:8020/path/to/file.csv"); BufferedReader reader = new BufferedReader(new InputStreamReader(fs.open(path))); // 逐行处理 CSV 文件 String line; while ((line = reader.readLine()) != null) { String[] parts = line.split(","); // 生成行键 byte[] rowKey = generateRowKey(parts[0], parts[1]); // 生成 Put 对象 Put put = new Put(rowKey); put.addColumn(Bytes.toBytes("data

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值