在Eclipse中运行JAVA代码远程操作HBase的示例

分类: 大数据 2014-03-04 13:47 3762人阅读 评论(2) 收藏 举报

下面是一个在Windows的Eclipse中通过JAVA操作一个Linux上运行的hbase的示例。
Hbase的配置需要注意下面一些要点:
1,服务器端用主机名配置hadoop和hbase,不要用IP
比如如下:
<property>  
 <name>hbase.zookeeper.quorum</name>  
 <value>hadoopsrv</value>  
</property>
2,hbase运行的机器上的机器名不能叫localhost
改/etc/sysconfig/network中的HOSTNAME
比如:
HOSTNAME=hadoopsrv
3,修改eclipse运行的windows客户端的C:\Windows\System32\drivers\etc\hosts文件.
对应到hbase运行服务器的ip,比如:
192.168.2.6 hadoopsrv
JAVA代码如下

        package org.apache.hadoop.hbase;
        import java.io.IOException;
        import java.util.ArrayList;
        import java.util.List;
        import org.apache.hadoop.conf.Configuration;
        import org.apache.hadoop.hbase.client.Delete;
        import org.apache.hadoop.hbase.client.Get;
        import org.apache.hadoop.hbase.client.HBaseAdmin;
        import org.apache.hadoop.hbase.client.HTable;
        import org.apache.hadoop.hbase.client.HTablePool;
        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;
        public class HbaseTest {
        private HBaseAdmin admin = null;
        // 定义配置对象HBaseConfiguration
        private HBaseConfiguration cfg = null;
        public HbaseTest() throws Exception {
        Configuration HBASE_CONFIG = new Configuration();
        HBASE_CONFIG.set("hbase.zookeeper.quorum", "192.168.2.6");
        HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");
        cfg = new HBaseConfiguration(HBASE_CONFIG);
        admin = new HBaseAdmin(cfg);
        }

        // 创建一张表,指定表名,列族
        public void createTable(String tableName, String columnFarily)
        throws Exception {
        if (admin.tableExists(tableName)) {
        System.out.println(tableName + "存在!");
        System.exit(0);
        } else {
        HTableDescriptor tableDesc = new HTableDescriptor(tableName);
        tableDesc.addFamily(new HColumnDescriptor(columnFarily));
        admin.createTable(tableDesc);
        System.out.println("创建表成功!");
        }
        }
        // Hbase获取所有的表信息
        public List getAllTables() {
        List<String> tables = null;
        if (admin != null) {
        try {
        HTableDescriptor[] allTable = admin.listTables();
        if (allTable.length > 0)
        tables = new ArrayList<String>();
        for (HTableDescriptor hTableDescriptor : allTable) {
        tables.add(hTableDescriptor.getNameAsString());
        System.out.println(hTableDescriptor.getNameAsString());
        }
        } catch (IOException e) {
        e.printStackTrace();
        }
        }
        return tables;
        }
        // Hbase中往某个表中添加一条记录
        public boolean addOneRecord(String table, String key, String family,
        String col, byte[] dataIn) {
        HTablePool tp = new HTablePool(cfg, 1000);
        HTable tb = (HTable) tp.getTable(table);
        Put put = new Put(key.getBytes());
        put.add(family.getBytes(), col.getBytes(), dataIn);
        try {
        tb.put(put);
        System.out.println("插入数据条" + key + "成功!!!");
        return true;
        } catch (IOException e) {
        System.out.println("插入数据条" + key + "失败!!!");
        return false;
        }
        }
        // Hbase表中记录信息的查询
        public void getValueFromKey(String table, String key) {
        HTablePool tp = new HTablePool(cfg, 1000);
        HTable tb = (HTable) tp.getTable(table);
        Get get = new Get(key.getBytes());
        try {
        Result rs = tb.get(get);
        if (rs.raw().length == 0) {
        System.out.println("不存在关键字为" + key + "的行!!");
        } else {
        for (KeyValue kv : rs.raw()) {
        System.out.println(new String(kv.getKey()) + " "
        + new String(kv.getValue()));
        }
        }
        } catch (IOException e) {
        e.printStackTrace();
        }
        }
        // 显示所有数据,通过HTable Scan类获取已有表的信息
        public void getAllData(String tableName) throws Exception {
        HTable table = new HTable(cfg, tableName);
        Scan scan = new Scan();
        ResultScanner rs = table.getScanner(scan);
        for (Result r : rs) {
        for (KeyValue kv : r.raw()) {
        System.out.println(new String(kv.getKey())
        + new String(kv.getValue()));
        }
        }
        }
        // Hbase表中记录信息的删除
        public boolean deleteRecord(String table, String key) {
        HTablePool tp = new HTablePool(cfg, 1000);
        HTable tb = (HTable) tp.getTable(table);
        Delete de = new Delete(key.getBytes());
        try {
        tb.delete(de);
        return true;
        } catch (IOException e) {
        System.out.println("删除记录" + key + "异常!!!");
        return false;
        }
        }
        // Hbase中表的删除
        public boolean deleteTable(String table) {
        try {
        if (admin.tableExists(table)) {
        admin.disableTable(table);
        admin.deleteTable(table);
        System.out.println("删除表" + table + "!!!");
        }
        return true;
        } catch (IOException e) {
        System.out.println("删除表" + table + "异常!!!");
        return false;
        }
        }
        // 测试函数
        public static void main(String[] args) {
        try {
        HbaseTest hbase = new HbaseTest();
        // hbase.createTable("student", "fam1");
        // hbase.getAllTables();
        // hbase.addOneRecord("student","id1","fam1","name","Jack".getBytes());
        // hbase.addOneRecord("student","id1","fam1","address","HZ".getBytes());
        // hbase.getValueFromKey("student","id1");
        // hbase.getAllData("student");
        //hbase.deleteRecord("student", "id1");
        hbase.deleteTable("student");
        } catch (Exception e) {
        e.printStackTrace();
        }
        }
        }

在Eclipse中运行JAVA代码远程操作HBase的示例的更多相关文章

  1. eclipse中添加Java代码注释模板

    eclipse中添加Java代码注释模板 1.Window->Preference->Java->Code Style->Code Template,进入注释编辑界面 2.文件 ...

  2. eclipse中运行java程序

    1 package ttt; public class Testttt { public static void main() { Person p =new Person(); p.name=&qu ...

  3. 在eclipse中运行perl代码,需要配置的插件以及方法

    Eclipse配置正则表达式 网址:http://www.cnblogs.com/itech/archive/2010/02/23/1671676.html perl的环境配置以及在Eclipse中安 ...

  4. Eclipse中设置Java代码格式化

    一.自定义 Java 代码格式化 [Java-Code-Formatting.xml 下载],下载完毕以后,打开 Eclipse 找到如下图界面,点击 Import 导入即可.

  5. Eclipse中实现JAVA代码的自动提示功能

    1.打开Eclipse,在.出现时进行代码提示换成任意字母+.出现时的代码提示了(.abcdefghijklmnopqrstuvwxyz):

  6. [转载]在sublime中运行Java代码

    1.设置java的PATH环境变量 2.创建批处理或Shell脚本文件 runJava.bat: 将该文件复制到JDK的bin目录下. @echo off cd %~dp1 echo Compilin ...

  7. 关于在Eclipse中运行java程序报出:The project:XXXX which is referenced by the classpath10

    1.work_space名称与project是否一样,如果是一样的可能会导致错误. 2.project所在的文件夹中的.mymetadata文件中定义的project-module名称是否与proje ...

  8. 智能客服 利用python运行java代码

    因为需要在linux中用python来进行分析,顾需要利用python来运行java中语音转文字和文字转语音代码 在python中运行java代码需要利用jpype

  9. eclipse中,把java函数代码折叠/展开

    首先,在eclipse 中开启设置代码折叠功能 1. windows->perferences->General->Editors->Structured Text Edito ...

随机推荐

  1. JavaScript中判断null、undefined与NaN的方法

    1.判断undefined: ? 1 2 3 4 var tmp = undefined; if (typeof(tmp) == "undefined"){ alert(" ...

  2. Linux sudo用法与配置

    Linux环境:CentOS 6.7 结构说明 可以通过编辑文件/etc/sudoers来配置,通常使用visudo命令来进行修改,因为如果你修改的格式不符合它会进行提示.接下来就通过一个格式来了解它 ...

  3. HDU4522 湫湫系列故事——过年回家

    传送门:点我 中文题面. 思路:拿spfa对卧铺和硬铺分别跑spfa,然后找两个的最短路.体感堆优化的dij也可以,不过spfa跑跑就过去了.有个细节是最后得用long long 存数据,其他的没啥. ...

  4. 165. Compare Version Numbers (String)

    Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 & ...

  5. 205. Isomorphic Strings (Map)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  6. java 異常抛出 throw 與 return

    package 異常;    public class TestException {      public TestException() {      }        boolean test ...

  7. 23【notepad++】修改背景颜色

    notepad++是一款功能丰富的编辑器,运行在windows平台上的编辑工具. 但它默认设置是白色背景,黑色文字,长时间看很刺眼.那么怎么设定成为暗色背景,亮色文字呢? 点击,设置->语言格式 ...

  8. 【js语法】array

    array操作说明 链接:http://www.w3school.com.cn/jsref/jsref_obj_array.asp 函数说明: concat():把两个array连接起来 join() ...

  9. 检测Android手机的IP地址

    package com.jason.demo.androidip; import android.content.Context; import android.net.DhcpInfo; impor ...

  10. idea工程中web.xml报错Servlet should have a mapping

    搭建ssm工程过程中web.xml报错:Servlet should have a mapping ....但是mapping已经配置好了...如下图: 搜索无果,后来发现是工程的web.xml位置配 ...