springboot hbase

springboot hbase,第1张

springboot hbase 1.添加依赖

2.2.4

		
            org.apache.hbase
            hbase-shaded-client
            ${hbase.version}
        
        
            org.apache.commons
            commons-lang3
            3.4
        
        
            commons-io
            commons-io
            2.4
        
        
            commons-beanutils
            commons-beanutils
            1.9.4
        
2.配置信息
# database - hbase
datasource.hbase.zookeeper.quorum=192.169.9.225
datasource.hbase.zookeeper.port=22181
datasource.hbase.zookeeper.znode.parent=
datasource.hbase.table.myHbase=myHbase
3.hbaseConfig类
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.Table;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class HbaseConfig {

    @Value("${datasource.hbase.zookeeper.quorum}")
    private String zookeeper;

    @Value("${datasource.hbase.zookeeper.znode.parent}")
    private String parent;

    @Value("${datasource.hbase.zookeeper.port}")
    private String port;

    @Value("${datasource.hbase.table.myHbase}")
    private String tableName;

    public Connection getConnection() throws IOException {
        org.apache.hadoop.conf.Configuration config = HbaseConfiguration.create();
        config.set("hbase.zookeeper.quorum", zookeeper);
        config.set("hbase.zookeeper.property.clientPort", port);
        if (parent != null && !"".equals(parent)) {
            config.set("zookeeper.znode.parent", parent);
        }
        Connection connection = ConnectionFactory.createConnection(config);
        return connection;
    }

    @Bean(name = "hbaseTable")
    public Table getHbaseTable() throws IOException {
        Connection connection = getConnection();
        Table table = connection.getTable(TableName.valueOf(tableName));
        return table;
    }

}
4.使用
import com.quwan.dspriskadmin.dao.mysql.SdkUidMapper;
import lombok.val;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@Service
public class SdkUidService {

    @Autowired
    private SdkUidMapper sdkUidMapper;

    @Resource(name = "hbaseTable")
    private Table hbaseTable;

    public void insertSdkUidToHbase(){
        try {
            val sdkUIdList = sdkUidMapper.queryUidList();
            System.out.println(sdkUIdList);
            sdkUIdList.forEach(sdkUId -> {
                Put put = new Put(Bytes.toBytes(sdkUId.getUid().toString()));
                put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("uid"),
                        Bytes.toBytes(sdkUId.getUid().toString()));
                try {
                    hbaseTable.put(put);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            hbaseTable.close();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5676633.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存