文档章节

HBase学习-基本Java API

Endless2010
 Endless2010
发布于 2017/07/29 19:10
字数 603
阅读 10
收藏 1

创建Connection对象

//Connection对象相当于与HBase的一个连接
public static Connection getConnection() throws IOException {
		Configuration config = HBaseConfiguration.create();
		config.addResource(new Path(System.getenv("HBASE_HOME"),"conf/hbase-site.xml"));
		return ConnectionFactory.createConnection(config);
	}

创建表

	public static void createTable(Connection connection, String tableName,
			String[] columnNames) throws IOException {
		try (Admin admin = connection.getAdmin()) {
			HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tableName));
			for (String column : columnNames)
				table.addFamily(new HColumnDescriptor(column));
			admin.createTable(table);
			System.out.println("created table:"+tableName+",with columns:"+Arrays.asList(columnNames));
		}
	}

删除表

	public static void deleteTable(Connection connection, String tableName) throws IOException {
		try (Admin admin = connection.getAdmin()) {
			admin.disableTable(TableName.valueOf(tableName));
			admin.deleteTable(TableName.valueOf(tableName));
			System.out.println("deleted table:"+tableName);
		}
	}

插入put

 	public static void put(Connection connection, String tableName,String rowKey,
			String columnFamily,String column,String value) throws IOException {
		try (Table table = connection.getTable(TableName.valueOf(tableName))) {		
			Put row=new Put(rowKey.getBytes());
			row.addColumn(columnFamily.getBytes(), column.getBytes(), value.getBytes());
			table.put(row);
			System.out.println("put ["+rowKey+","+columnFamily+":"+column+"]="+value+" into table"+tableName);
		}
	}

查询get

public static Result get(Connection connection, String tableName,String rowKey,
			String columnFamily) throws IOException {
		 return get(connection, tableName, rowKey,columnFamily, null);
	}
	public static Result get(Connection connection, String tableName,String rowKey,
			String columnFamily,String column) throws IOException {
		try (Table table = connection.getTable(TableName.valueOf(tableName))) {		
			Get row=new Get(rowKey.getBytes());
			row.addFamily(columnFamily.getBytes());
			if(column!=null)
				row.addColumn(columnFamily.getBytes(), column.getBytes());
			Result result=table.get(row);
			for(Cell cell:result.listCells()){
				String col=new String(cell.getQualifier());
				String value=new String(cell.getValue());
				System.out.println("["+rowKey+","+columnFamily+":"+col+"]="+value);
			}
			return result;
		}
	}

扫描scan


public static void scan(Connection connection, String tableName,String startRow,String endRow,Filter filter) throws IOException {
		try (Table table = connection.getTable(TableName.valueOf(tableName))) {		
			Scan scan=new Scan();
			if(filter!=null)
				scan.setFilter(filter);
			if(startRow!=null)
				scan.setStartRow(startRow.getBytes());
			if(endRow!=null)
				scan.setStopRow(endRow.getBytes());
			ResultScanner scanner=table.getScanner(scan);
			for(Result result:scanner){
				String rowKey=new String(result.getRow());
				for(Cell cell:result.listCells()){
					String family=new String(cell.getFamily());
					String col=new String(cell.getQualifier());
					String value=new String(cell.getValue());
					System.out.println("["+rowKey+","+family+":"+col+"]="+value);
				}
			}
		}
	}
	public static void scan(Connection connection, String tableName) throws IOException {
		scan(connection,tableName,null,null,null);
	}
	public static void scan(Connection connection, String tableName,String startRow,String endRow) throws IOException {
		scan(connection,tableName,startRow,endRow,null);
	}
	public static void scan(Connection connection, String tableName,Filter filter) throws IOException {
		scan(connection,tableName,null,null,filter);
	}

incr


public static void incr(Connection connection, String tableName,String rowKey,
			String columnFamily,String column,long value) throws IOException {
		try (Table table = connection.getTable(TableName.valueOf(tableName))) {		
			Increment incr=new Increment(rowKey.getBytes());
			incr.addColumn(columnFamily.getBytes(), column.getBytes(), value);
			Result result=table.increment(incr);
			 for (KeyValue kv : result.raw()) {
			      System.out.println("KV: " + kv +" Value: " + Bytes.toLong(kv.getValue()));
			    }
		}
	}

测试

public static void main(String[] args) throws IOException {
		String tableName = "testTable";
		String columnFamily_info="info";
		String columnFamily_score="score";
		String column1_name="name",column1_age="age";
		String column2_Math="Math",column2_English="English";
		try(Connection conn = getConnection();){
			System.out.println("------------------- create table -------------------");
			createTable(conn,tableName,new String[]{columnFamily_info,columnFamily_score});
			
			System.out.println("------------------- put -------------------");
			put(conn,tableName,"row1",columnFamily_info,column1_name,"sam");
			put(conn,tableName,"row1",columnFamily_score,column2_Math,"70");
			put(conn,tableName,"row1",columnFamily_score,column2_English,"75");
			put(conn,tableName,"row2",columnFamily_score,column2_English,"80");
			put(conn,tableName,"row3",columnFamily_info,column1_age,"30");
			
			System.out.println("------------------- get -------------------");
			get(conn,tableName,"row1",columnFamily_score,column2_Math);
			get(conn,tableName,"row1",columnFamily_score);//get all columns in columnFamily2
			
			System.out.println("------------------- scan -------------------");
			scan(conn,tableName,"row1","row3"); //[row1,row3)
			System.out.println("------------------- scan all table -------------------");
			scan(conn,tableName); //[row1,row3)
			System.out.println("------------------- scan with filter -------------------");
			Filter filter=new ColumnPrefixFilter("Eng".getBytes());//过滤列族以Eng开头的
			scan(conn,tableName,filter); //[row1,row3)
		
			System.out.println("------------------- incr -------------------");
			incr(conn,tableName,"row3",columnFamily_score,column2_Math,80);
			incr(conn,tableName,"row3",columnFamily_score,column2_Math,-5);
			
			System.out.println("------------------- delete table -------------------");		
			deleteTable(conn,tableName);
		}

输入图片说明

© 著作权归作者所有

共有 人打赏支持
Endless2010
粉丝 1
博文 36
码字总数 23027
作品 0
南京
程序员
私信 提问
win环境下 hbase 数据库搭建流程

Hbase单机版安装使用教程 Hbase介绍请参考技术支持文档,这里只说明Hbase 的安装和使用 第一步:下载安装Hbase数据库 链接:链接:https://pan.baidu.com/s/1si6Nokvz8xkUsmefsRlntQ 密码:k...

lijiaweizuishuai
05/28
0
0
用 Java 对 hbase 进行CRUD增删改查操作

本文以HBase 0.90.2为例,介绍如何在Windows系统,Eclipse IDE集成环境下,使用Java语言,进行HBase客户端编程,包含建立表、删除表、插入记录、删除记录、各种方式下的查询操作等。 1. 准备...

大数据之路
2012/11/29
0
5
hadoop hive hbase 入门学习 (二)

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// hadoop 自学系列 hado......

curiousby
2015/03/08
0
0
HBase0.96.x开发使用(一)--安装

1.1) 解压安装包 [root@hadoop234 ~]# tar -xzvf hbase-0.96.0-hadoop1-bin.tar.gz 1.2) 配置hbase-site.xml [root@hadoop234 ~]# cd hbase-0.96.0-hadoop1/conf/ [root@hadoop234 conf]# v......

whatlly
2013/12/31
0
0
bulk-load装载hdfs数据到hbase小结

bulk-load的作用是用mapreduce的方式将hdfs上的文件装载到hbase中,对于海量数据装载入hbase非常有用,参考http://hbase.apache.org/docs/r0.89.20100621/bulk-loads.html: hbase提供了现成...

超人学院
2015/06/01
0
0

没有更多内容

加载失败,请刷新页面

加载更多

大数据教程(7.6)shell脚本定时采集日志数据到hdfs

上一篇博客博主分享了hadoop内置rpc的使用案例,本节博主将为小伙伴们分享一个在实际生产中使用的日志搜集案例。前面的文章我们有讲到过用户点击流日志分析的流程,本节就是要完成这个分析流...

em_aaron
7分钟前
0
0
wave和pcm互转

wav->pcm pcm->wav c#代码: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using Sys......

whoisliang
10分钟前
0
0
Win10:默认的图片打开应用,打开图片时速度明显很慢的解决办法

首先,我们随便地打开一张图片。然后,点击右上角的三个小点,最后点击弹出菜单最下面的“设置”。如下图: 在“设置”中找到下面的“人物”,把它关掉就好了。 原来,默认情况下,Win 10的图...

LivingInFHL
今天
3
0
js代码激发onchange事件,兼容谷歌火狐IE

var el = document.getElementsByName('role')[0]; el.value = '3'; var evt = document.createEvent("HTMLEvents"); evt.initEvent("change", false, true); el.dispatchEvent(evt);......

我退而结网
今天
4
0
mysql客户端报错:libmysqlclient_16 not defined in file libmysqlclient.so.16

报错情况: 安装完mydumper之后(上一篇文章),登陆Mysql客户端报错:version libmysqlclient_16 not defined in file libmysqlclient.so.16 with link time reference 同样:mysql的其他客...

machogyb
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部