hbase客户端api--建表

package os.hbase.index;

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.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class HbaseDaoDemo {

    public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {

        //Configuration conf = new Configuration();

        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","os-1:2181,os-2:2181,os-3:2181");

        //DDL操作用的客户端对象
        HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);

        TableName cart = TableName.valueOf("cart");

        //构建表的描述对象
        HTableDescriptor desc = new HTableDescriptor(cart);

        //在表描述对象中封装所必须指定的信息
        HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("product");
        HColumnDescriptor goods = new HColumnDescriptor("goods");
        //保存的版本数量
        hColumnDescriptor.setMaxVersions(3);
        goods.setMaxVersions(2);
        //指定列族
        desc.addFamily(hColumnDescriptor);
        desc.addFamily(goods);

        //通过hBaseAdmin客户端将表描述对象所描述的表在hbase集群中创建出来
        hBaseAdmin.createTable(desc);

        hBaseAdmin.close();
    }
}
  • 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、付费专栏及课程。

余额充值