zoukankan      html  css  js  c++  java
  • 通过Java操作HBase

      本文主要是利用IDEA 2017做一个小的Demo,能访问CentOS的HBase。

    一,hosts设置

    1,Win10的C:WindowsSystem32driversetc目录下,在hosts的最后加一行代码。(其中,LZW是CentOS的主机名)

    192.168.30.128     LZW

    2,通过如下命令编辑CentOS的hosts文件,同样是加上上面的IP

    vi /etc/hosts
    

    二,代码编写  

    1,IDEA 2017新建Maven项目,填入信息,直到完成

      

      

    2,pom.xml文件新增如下配置,并应用更改

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

    3,新建com.demo.hbase包,并新增HBaseDemo代码文件

    package com.demo.hbase;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.client.HTable;
    import org.apache.hadoop.hbase.client.Put;
    import org.apache.hadoop.hbase.util.Bytes;
    
    import java.io.IOException;
    
    public class HBaseDemo {
        public static void main(String[] args) throws IOException {
            Configuration conf= HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum","192.168.30.129");
    
            HTable table=new HTable(conf,"testtable");
    
            Put put=new Put(Bytes.toBytes("row1"));
            put.add(Bytes.toBytes("colfam1"),Bytes.toBytes("qual1"),Bytes.toBytes("val1"));
            put.add(Bytes.toBytes("colfam1"),Bytes.toBytes("qual2"),Bytes.toBytes("val2"));
    
            table.put(put);
        }
    }
    

    4,运行代码,然后在HBase shell查看结果。成功添加

    scan 'testtable'
    

      

  • 相关阅读:
    Linux 配置jdk vim和 Linux 基本操作
    Java02_数据类型
    java01_简介_开发环境
    基于Vue + webpack + Vue-cli 实现分环境打包项目
    理解TCP/IP三次握手与四次挥手的正确姿势
    Vue 项目骨架屏注入与实践
    我的第一个Quartz代码
    hdu5882 Balanced Game
    hdu5883 The Best Path(欧拉路)
    Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )
  • 原文地址:https://www.cnblogs.com/nanchuan/p/7615486.html
Copyright © 2011-2022 走看看