java: 写入数据到HBase

一、添加依赖

复制代码
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.6.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.4.2</version>
        </dependency>

二、调用API写HBase示例

复制代码
package cn.edu.tju;

import org.apache.commons.io.IOUtils;
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;

public class TestHbaseWrite {
    public static void main(String[] args) throws Exception{
        Configuration config = HBaseConfiguration.create();// create configuration
        //zookeeper 地址
        config.set("hbase.zookeeper.quorum","xxx.xxx.xxx.xxx");//
        //zookeeper端口
        config.set("hbase.zookeeper.property.clientPort", "2181");//
        //表名,必须提前在hbase中创建
        String tableName ="c1";
        //row key
        String rowKey = "myRowKeyTest";
        //family,必须是hbase中有的family
        String familyName = "fam3";
        //column
        String columnName = "age";
        //value
        String columnValue = "18";
        Connection connection = ConnectionFactory.createConnection(config);
        Table table = null;
        try {
            table = connection.getTable(TableName.valueOf(tableName));
            Put put = new Put(Bytes.toBytes(rowKey));
            put.addColumn(familyName.getBytes(), Bytes.toBytes(columnName),Bytes.toBytes(columnValue));
            table.put(put);
        }
        catch(Exception ex) {
            System.out.println(ex.getMessage());
        }
        finally {
            IOUtils.closeQuietly(table);
        }
    }
}
相关推荐
Felix_XXXXL16 分钟前
IDEA + Spring Boot 的三种热加载方案
java·后端
我命由我1234517 分钟前
IDEA - IDEA 快速回到页面首尾、页面快速滑动、快速定位到指定行
java·运维·ide·后端·java-ee·intellij-idea·intellij idea
Mickyจุ๊บ22 分钟前
IDEA 插件推荐
java·ide·intellij-idea
千里镜宵烛24 分钟前
深入 Lua 环境机制:全局变量的 “容器” 与 “隔离术”
开发语言·junit·lua
命运之光25 分钟前
【快速解决】idea运行javafx错误: 缺少 JavaFX 运行时组件, 需要使用该组件来运行此应用程序
java·ide·intellij-idea
学到头秃的suhian6 小时前
Maven
java·maven
QX_hao6 小时前
【Go】--反射(reflect)的使用
开发语言·后端·golang
小坏讲微服务6 小时前
Docker-compose 搭建Maven私服部署
java·spring boot·后端·docker·微服务·容器·maven
chxii6 小时前
Maven 详解(下)
java·maven