hbase 基本开发-插入表

发布时间:2020-06-17 17:46:10 作者:JUN_LJ
来源:网络 阅读:916

创建maven项目

pom.xml文件内容: 3个依赖包 hbase-client hadoop-hdfs jdk.tools


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>mjj.hbase</groupId>

  <artifactId>test-10</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <dependencies>

        <dependency>

            <groupId>org.apache.hbase</groupId>

            <artifactId>hbase-client</artifactId>

            <version>2.0.0</version>

        </dependency>

        <dependency>

            <groupId>org.apache.hadoop</groupId>

            <artifactId>hadoop-hdfs</artifactId>

            <version>2.8.0</version>

        </dependency>

        <dependency>

            <groupId>jdk.tools</groupId>

            <artifactId>jdk.tools</artifactId>

            <version>1.8</version>

            <scope>system</scope>

            <systemPath>C:\Program Files\Java\jdk1.8.0_151\lib\tools.jar</systemPath>

        </dependency>

    </dependencies>

</project>


连接hbase的两种方法:

配置法(不一定要按图配置,程序会默认去读classpath下的配置文件,可以通过System.out.println(ClassLoader.getSystemResource("").toString());获取)

1 在main文件夹下,建立resources文件夹,在resources文件夹下建立hbase文件,hbase文件夹下放hbase-site.xml配置文件(主要用到里面的zookeeper配置信息)

hbase 基本开发-插入表

配置文件主要内容如下:

hbase-site.xml

<property>

 <name>hbase.zookeeper.quorum</name>

  <value>192.168.50.1071</value>

 </property>

 <property>

  <name>hbase.zookeeper.property.clientPort</name>  

  <value>2181</value>

 </property>


代码:

package myHbase;


import java.io.IOException;


import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.Cell;

import org.apache.hadoop.hbase.Cell.Type;

import org.apache.hadoop.hbase.CellBuilder;

import org.apache.hadoop.hbase.CellBuilderFactory;

import org.apache.hadoop.hbase.CellBuilderType;

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.HBaseAdmin;

import org.apache.hadoop.hbase.client.Put;

import org.apache.hadoop.hbase.client.Table;

import org.apache.hadoop.hbase.util.Bytes;


public class SimpleTest {


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

// TODO Auto-generated method stub

Configuration hBaseConfig = HBaseConfiguration.create();

HBaseAdmin.available(hBaseConfig);

Connection connection = ConnectionFactory.createConnection(hBaseConfig);


TableName table1 = TableName.valueOf("test");

Table table = connection.getTable(table1);


CellBuilder cb = CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY);

cb.setRow(Bytes.toBytes("row3"));

cb.setFamily(Bytes.toBytes("cf"));

cb.setQualifier("qualifier1".getBytes());

cb.setValue(Bytes.toBytes("mjj2"));

cb.setType(Type.Put);

Cell cell = cb.build();


Put p = new Put(Bytes.toBytes("row3"));

p.add(cell);

table.put(p);


connection.close();

}


}


配置不正确报错(zookeeper的连接丢失): 

Exception in thread "main" org.apache.hadoop.hbase.MasterNotRunningException: org.apache.hadoop.hbase.MasterNotRunningException: java.io.IOException: org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /hbase/master


代码里设定zookeeper:

package myHbase;


import java.io.IOException;


import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.Cell;

import org.apache.hadoop.hbase.Cell.Type;

import org.apache.hadoop.hbase.CellBuilder;

import org.apache.hadoop.hbase.CellBuilderFactory;

import org.apache.hadoop.hbase.CellBuilderType;

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.HBaseAdmin;

import org.apache.hadoop.hbase.client.Put;

import org.apache.hadoop.hbase.client.Table;

import org.apache.hadoop.hbase.util.Bytes;


public class SimpleTest {


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

// TODO Auto-generated method stub

Configuration hBaseConfig = HBaseConfiguration.create();

hBaseConfig.set("hbase.zookeeper.quorum", "192.168.50.107");

hBaseConfig.set("hbase.zookeeper.property.clientPort", "2181");

HBaseAdmin.available(hBaseConfig);

Connection connection = ConnectionFactory.createConnection(hBaseConfig);


TableName table1 = TableName.valueOf("test");

Table table = connection.getTable(table1);


CellBuilder cb = CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY);

cb.setRow(Bytes.toBytes("row3"));

cb.setFamily(Bytes.toBytes("cf"));

cb.setQualifier("qualifier1".getBytes());

cb.setValue(Bytes.toBytes("mjj2"));

cb.setType(Type.Put);

Cell cell = cb.build();


Put p = new Put(Bytes.toBytes("row3"));

p.add(cell);

table.put(p);


connection.close();

}


}


重要: windows上一定要配置/etc/hosts. 添加一条 192.168.50.107 rhel  -------其中rhel 为 hbase机子的主机名。原因未知。

否则报错: 

Exception in thread "main" org.apache.hadoop.hbase.MasterNotRunningException: org.apache.hadoop.hbase.MasterNotRunningException: java.net.UnknownHostException: can not resolve rhel,16000,1530027948780



推荐阅读:
  1. HBase框架学习之路
  2. Hbase shell操作总结

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

hbase 开发 基本开发

上一篇:jenkins 邮件发送错误

下一篇:如何解决java中的string乱码

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》