Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Hbase basic development-insert table

Shulou Source: shulou.com Published: 2022-06-03 06:39:01 11月23日 Update

Create a maven project

Contents of pom.xml file: 3 dependent packages 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

There are two ways to connect to hbase:

Configuration method (not necessarily according to the figure, the program will default to read the configuration file under classpath, which can be obtained through System.out.println (ClassLoader.getSystemResource ("). ToString ()

1 under the main folder, set up the resources folder, create the hbase file under the resources folder, and distribute the hbase-site.xml configuration file under the hbase folder (mainly using the zookeeper configuration information inside)

The main contents of the configuration file are as follows:

Hbase-site.xml

Hbase.zookeeper.quorum

192.168.50.1071

Hbase.zookeeper.property.clientPort

2181

Code:

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 ()

}

}

Incorrect configuration error (lost connection for 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

Set zookeeper in the code:

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 ()

}

}

Important: be sure to configure / etc/hosts on windows. Add a 192.168.50.107 rhel-where rhel is the hostname of the hbase machine. The reason is unknown.

Otherwise, an error is reported:

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

Tags: File configuration folder code content important host information reason can be passed through method machine program project previous plus one development Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL NVidia OPPO Reno Huawei Shulou Technology