在Eclipse中运行JAVA代码远程操作HBase的示例
2014-03-04 13:47
495 查看
下面是一个在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代码如下
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的示例
- 《Thinking in Java》RMI远程方法示例代码运行是可能遇到的问题及解决方法
- win7系统eclipse远程连接虚拟机中的centos6.5,对hbase进行操作
- Java远程共享目录的操作代码
- 用eclipse远程调试java代码
- java操作Oracle中的BLOB和CLOB代码示例
- eclipse远程操作hbase
- java中操作xml几种方式的比较和代码示例(DOM,SAX,JDOM,DOM4J)
- java中文件的读与写操作代码示例
- java操作文件示例代码
- java中操作xml几种方式的比较和代码示例(DOM,SAX,JDOM,DOM4J)
- eclipse中远程操作hbase例子,只有map过程没有reduce,map中读取hbase表的所有列
- 使用Eclipse运行Java代码调用JDBC读写MySQL中文变成问号的终极解决办法
- Java客户端操作HBase代码
- java代码操作cmd二次运行新java程序
- JAVA远程操作Zookeeper示例
- java 操作blob、clob大字段代码示例
- java代码通过ssh免密操作远程服务器shell
- JAVA代码实现远程服务器的文件操作
- 本地eclipse运行hadoop程序,操作远程服务器(供理解和入门)