Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Hbase basic development-insert table

2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report