hbase基本開發(fā)-插入表

創(chuàng)建maven項(xiàng)目

創(chuàng)新互聯(lián)是一家專業(yè)提供永勝企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、HTML5、小程序制作等業(yè)務(wù)。10年已為永勝眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。

pom.xml文件內(nèi)容: 3個(gè)依賴包 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的兩種方法:

配置法(不一定要按圖配置,程序會(huì)默認(rèn)去讀classpath下的配置文件,可以通過System.out.println(ClassLoader.getSystemResource("").toString());獲?。?/p>

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

hbase 基本開發(fā)-插入表

配置文件主要內(nèi)容如下:

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

}

}

配置不正確報(bào)錯(cuò)(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

代碼里設(shè)定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機(jī)子的主機(jī)名。原因未知。

否則報(bào)錯(cuò): 

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

分享題目:hbase基本開發(fā)-插入表
網(wǎng)站URL:http://muchs.cn/article6/ihesog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、標(biāo)簽優(yōu)化、品牌網(wǎng)站建設(shè)、小程序開發(fā)網(wǎng)站收錄、網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)