Hbase的Eclipse连接测试
来源:互联网 发布:c语言编写程序 编辑:程序博客网 时间:2024/04/30 02:15
1首先学习用maven创建hadoop项目以及链接Eclipse测试。这部分内容可以参考
我的上篇博客http://blog.csdn.net/tryhl/article/details/44032687
2Hbase的Eclipse连接测试
1)首先在源pom.xml文件中加入以下依赖
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>0.98.6-hadoop2</version> </dependency>
注:使用自己的maven服务器可能下载不了,如果下载不了的话,在maven配置文件中把自己的仓库注释掉,使用默认的。如果还是不能下载(最好翻墙以下)
整个的pom文件依赖,如下图
2)把集群中的hbase-site.xml复制到项目的ClassPath路径下
如下是我的项目结构
新创建一个source folder包,方法如下:
点击项目名---->右键 new---> source folder---->输入 src/main/resources/hbase
然后把hbase-site.xml复制到下面如上图。
最后把下面的属性加入到hbase-site.xml中
<property> <name>fs.hdfs.impl</name> <value>org.apache.hadoop.hdfs.DistributedFileSystem</value> </property>
3)测试代码
package com.lh.test;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.TableName;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.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.filter.Filter;import org.apache.hadoop.hbase.util.Bytes;public class HBaseTest {static Configuration conf = HBaseConfiguration.create();/** * create a table :table_name(columnFamily) * * @param tablename * @param columnFamily * @throws Exception */public static void createTable(String tablename, String columnFamily)throws Exception {HBaseAdmin admin = new HBaseAdmin(conf);if (admin.tableExists(tablename)) {System.out.println("Table exists!");System.exit(0);} else {HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tablename));tableDesc.addFamily(new HColumnDescriptor(columnFamily));admin.createTable(tableDesc);System.out.println("create table success!");}admin.close();}/** * delete table ,caution!!!!!! ,dangerous!!!!!! * * @param tablename * @return * @throws IOException */public static boolean deleteTable(String tablename) throws IOException {HBaseAdmin admin = new HBaseAdmin(conf);if (admin.tableExists(tablename)) {try {// 删除之前 需要先disable表admin.disableTable(tablename);admin.deleteTable(tablename);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();admin.close();return false;}}admin.close();return true;}/** * put a cell data into a row identified by rowKey,columnFamily,identifier * * @param HTable * , create by : HTable table = new HTable(conf, "tablename") * @param rowKey * @param columnFamily * @param identifier * @param data * @throws Exception */public static void putCell(HTable table, String rowKey,String columnFamily, String identifier, String data)throws Exception {Put p1 = new Put(Bytes.toBytes(rowKey));p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(identifier),Bytes.toBytes(data));table.put(p1);System.out.println("put '" + rowKey + "', '" + columnFamily + ":"+ identifier + "', '" + data + "'");}/** * get a row identified by rowkey * * @param HTable * , create by : HTable table = new HTable(conf, "tablename") * @param rowKey * @throws Exception */public static Result getCell(HTable table, String rowKey) throws Exception {Get get = new Get(Bytes.toBytes(rowKey));Result result = table.get(get);System.out.println("Get: " + result);return result;}/** * delete a row identified by rowkey * * @param HTable * , create by : HTable table = new HTable(conf, "tablename") * @param rowKey * @throws Exception */public static void deleteCell(HTable table, String rowKey) throws Exception {Delete delete = new Delete(Bytes.toBytes(rowKey));table.delete(delete);System.out.println("Delete row: " + rowKey);}/** * return all row from a table * * @param HTable * , create by : HTable table = new HTable(conf, "tablename") * @throws Exception */public static ResultScanner scanAll(HTable table) throws Exception {Scan s = new Scan();ResultScanner rs = table.getScanner(s);return rs;}/** * return a range of rows specified by startrow and endrow * * @param HTable * , create by : HTable table = new HTable(conf, "tablename") * @param startrow * @param endrow * @throws Exception */public static ResultScanner scanRange(HTable table, String startrow,String endrow) throws Exception {Scan s = new Scan(Bytes.toBytes(startrow), Bytes.toBytes(endrow));ResultScanner rs = table.getScanner(s);return rs;}/** * return a range of rows filtered by specified condition * * @param HTable * , create by : HTable table = new HTable(conf, "tablename") * @param startrow * @param filter * @throws Exception */public static ResultScanner scanFilter(HTable table, String startrow,Filter filter) throws Exception {Scan s = new Scan(Bytes.toBytes(startrow), filter);ResultScanner rs = table.getScanner(s);return rs;}public static void main(String[] args) throws Exception {// TODO Auto-generated method stub// 创建一个名为 booktable的表 author的列族// HBaseTest.createTable("bookTable", "author");// HTable table = new HTable(conf, "bookTable");// 插入数据// HBaseTest.putCell(table, "100001", "author", "name", "liuhuan");// HBaseTest.putCell(table,"100002", "author", "age", "23");// 删除行// HBaseTest.deleteCell(table, "100002");// 得到行数据// HBaseTest.getCell(table, "100001");// keyrow范围扫描// ResultScanner rs = HBaseTest.scanRange(table, "100001", "100003");// 全表扫描// ResultScanner rs = HBaseTest.scanAll(table);// for (Result r : rs) {// System.out.println("Scan: " + r);// }// table.close();// 删除表// HBaseTest.deleteTable("bookTable");}}
把上面的代码复制到相应的包下,执行测试即可
此文章用于学习和交流转载请注明
也可加入群316297243交流学习
0 0
- Hbase的Eclipse连接测试
- hbase连接测试时的常见错误
- eclipse远程连接hbase遇到的问题
- eclipse连接hbase
- PowerMock测试 mock hbase连接
- eclipse远程连接hbase错误
- phoenix hbase 安装 eclipse 测试
- windows环境的eclipse创建hbase连接时报错处理
- Eclipse连接hbase并实现简单表的建立
- 求助?eclipse连接hbase出现如下异常
- eclipse下使用javaapi连接hbase记录
- eclipse停使用javaapi连接hbase记录
- eclipse 连接并操作单机版Hbase
- eclipse远程hbase 无法连接 org.apache.hadoop.hbase.MasterNotRunningException
- 【HBase】HBase的eclipse开发环境搭建
- win下Eclipse远程连接Hbase的配置及程序示例(create、insert、get、delete)
- eclipse无法连接到真机测试
- HBase的简单性能测试
- C++检测空字符串
- Linux_Screen 之共享屏幕
- HTML5 知识总结
- STC12C56XX的时钟周期
- 多线程开发
- Hbase的Eclipse连接测试
- C#中OnGUI的使用
- Linux下ELF可执行文件装载与运行
- 继承、实现、依赖、关联、聚合、组合的联系与区别
- Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义
- hadoop put内部调用,hdfs写文件流程
- java日志《三》Log4j日志远程发送
- [150310]Linux学习笔记
- Tom