我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

hbase基本开发-插入表

创建maven项目

创新互联是一家专业提供永胜企业网站建设,专注与成都做网站、成都网站制作、成都外贸网站建设HTML5、小程序制作等业务。10年已为永胜众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。

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

  4.0.0

  mjj.hbase

  test-10

  0.0.1-SNAPSHOT

 

       

            org.apache.hbase

            hbase-client

            2.0.0

       

       

            org.apache.hadoop

            hadoop-hdfs

            2.8.0

       

       

            jdk.tools

            jdk.tools

            1.8

            system

            C:\Program Files\Java\jdk1.8.0_151\lib\tools.jar

       

   

连接hbase的两种方法:

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

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

hbase 基本开发-插入表

配置文件主要内容如下:

hbase-site.xml

 hbase.zookeeper.quorum

  192.168.50.1071

 

 

  hbase.zookeeper.property.clientPort  

  2181

 

代码:

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


本文名称:hbase基本开发-插入表
当前地址:http://shouzuofang.com/article/ihesog.html

其他资讯