java连接虚拟机hadoop_本地eclipse java api连接远程虚拟机HBase

本文介绍了如何在本地Java环境中利用Eclipse连接远程虚拟机上的Hadoop集群,并通过HBase Java API进行操作。主要内容包括设置HBase环境、解决Windows环境下winutils.exe问题、配置HADOOP_HOME环境变量、POM.xml依赖配置,以及提供了一段创建、查询、删除HBase表的Java代码示例。
摘要由CSDN通过智能技术生成

1.本地与远程连通

无论是域名或者ip都可以,另外需保证HBase在虚拟机集群上正常运行。

2.本地要有一个跟远程相同的hadoop环境

当然不相同,只要兼容也可以,现采用hadoop-2.5.0-cdh5.3.6,此版本如果在Windows系统中。在java api调用时候会报错Could not locate executable D:\Hu_software\hadoop-2.5.0-cdh5.3.6\bin\winutils.exe in the Hadoop bina

此时可以从别的拷贝一个winutils.exe到此bin来。可从winutls.exe下载,提取吗wt9x。经测试用hadoop-2.7.7也可以测试跑通,下载解压即可。

3.代码运行前准备

首先需要HADOOP_HOME的环境变量,可以配置到系统环境变量里面,也可以如下:

Eclipse->Debug/Run Configurations -> Environment (tab) -> and added

variable: HADOOP_HOME

Value: C:\hadoop-2.5.0-cdh5.3.6

最好配以log4j.properties到src目录下面,地址。

4.启动执行

(1) pom.xml

org.apache.hbase

hbase-server

0.98.6-hadoop2

org.apache.hbase

hbase-client

0.98.6-hadoop2

(2)java 代码 HBaseDemo.java

package com.hoult.hbase;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.Cell;

import org.apache.hadoop.hbase.CellUtil;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.HColumnDescriptor;

import org.apache.hadoop.hbase.HTableDescriptor;

import org.apache.hadoop.hbase.MasterNotRunningException;

import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.hbase.ZooKeeperConnectionException;

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

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

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

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

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

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

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

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

public class HBaseDemo {

//创建Hadoop以及HBased管理配置对象

public static Configuration conf;

static{

//使用HBaseConfiguration的单例方法实例化

conf = HBaseConfiguration.create();

conf.set("hbase.rootdir","hdfs://hadoop-senior01.hoult.com:9000/hbase");//可以吧hadoop改成你的域名或Ip

conf.set("hbase.zookeeper.quorum","hadoop-senior01.hoult.com,hadoop-senior02.hoult.com,hadoop-senior03.hoult.com"); //hbase 服务地址

conf.set("hbase.zookeeper.property.clientPort","2181"); //端口号默认2128不用改

}

/**

* 判断表是否已存在

* @param args

* @throws IOException

* @throws ZooKeeperConnectionException

* @throws MasterNotRunningException

*/

public static boolean isTableExist(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{

//在HBase中管理、访问表需要先创建HBaseAdmin对象

HBaseAdmin admin = new HBaseAdmin(conf);

return admin.tableExists(tableName);

}

/**

* 创建表

* @param args

* @throws IOException

* @throws ZooKeeperConnectionException

* @throws MasterNotRunningException

*/

public static void createTable(String tableName, String... columnFamily) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{

HBaseAdmin admin = new HBaseAdmin(conf);

//判断表是否存在

if(isTableExist(tableName)){

System.out.println("表" + tableName + "已存在");

//System.exit(0);

}else{

//创建表属性对象,表名需要转字节

HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));

//创建多个列族

for(String cf : columnFamily){

descriptor.addFamily(new HColumnDescriptor(cf));

}

//根据对表的配置,创建表

admin.createTable(descriptor);

System.out.println("表" + tableName + "创建成功!");

}

}

/**

* 删除表

* @param args

* @throws IOException

* @throws ZooKeeperConnectionException

* @throws MasterNotRunningException

*/

public static void dropTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{

HBaseAdmin admin = new HBaseAdmin(conf);

if(isTableExist(tableName)){

admin.disableTable(tableName);

admin.deleteTable(tableName);

System.out.println("表" + tableName + "删除成功!");

}else{

System.out.println("表" + tableName + "不存在!");

}

}

/**

* 向表中插入数据

* @param tableName

* @param rowKey

* @param columnFamily

* @param column

* @param value

* @throws IOException

*/

public static void addRowData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException{

//创建HTable对象

HTable hTable = new HTable(conf, tableName);

//向表中插入数据

Put put = new Put(Bytes.toBytes(rowKey));

//向Put对象中组装数据

put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));

hTable.put(put);

hTable.close();

System.out.println("插入数据成功");

}

/**

* 删除多行数据

* @param tableName

* @param rows

* @throws IOException

*/

public static void deleteMultiRow(String tableName, String... rows) throws IOException{

HTable hTable = new HTable(conf, tableName);

List deleteList = new ArrayList();

for(String row : rows){

Delete delete = new Delete(Bytes.toBytes(row));

deleteList.add(delete);

}

hTable.delete(deleteList);

hTable.close();

}

/**

* 得到所有的数据

* @param tableName

* @throws IOException

*/

public static void getAllRows(String tableName) throws IOException{

HTable hTable = new HTable(conf, tableName);

//得到用于扫描region的对象

Scan scan = new Scan();

//使用HTable得到resultcanner实现类的对象

ResultScanner resultScanner = hTable.getScanner(scan);

for(Result result : resultScanner){

Cell[] cells = result.rawCells();

for(Cell cell : cells){

//得到rowkey

System.out.print(Bytes.toString(CellUtil.cloneRow(cell)) + "\t");

//得到列族

System.out.print(Bytes.toString(CellUtil.cloneFamily(cell))+ "\t");

System.out.print(Bytes.toString(CellUtil.cloneQualifier(cell))+ "\t");

System.out.println(Bytes.toString(CellUtil.cloneValue(cell))+ "\t");

}

}

}

public static void main(String[] args) {

try {

//System.out.println(isTableExist("student"));

//createTable("person", "basic_info", "job", "heathy");

//dropTable("person");

addRowData("person", "1002", "basic_info", "name", "Hoult");

addRowData("person", "1003", "basic_info", "sex", "Male");

addRowData("person", "1002", "basic_info", "age", "22");

addRowData("person", "1004", "job", "dept_no", "123");

//

//deleteMultiRow("person", "person");

//getAllRows("person");

} catch (Exception e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值