/**
* Created by han on 2016/1/28.
*/
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; public class test { public static Configuration configuration;
static {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("hbase.zookeeper.quorum", "48.0.191.154:2181,48.0.191.154:2182,48.0.191.154:2183");
configuration.set("hbase.master", "48.0.191.154:60000");
System.setProperty("java.library.path","E:\\资料\\hadoop-2.6.2\\bin");
System.setProperty("hadoop.home.dir", "E:\\资料\\hadoop-2.6.2");
System.setProperty("PATH", "E:\\资料\\hadoop-2.6.2\\bin"); } public static void main(String[] args) {
// createTable("h");
// insertData("h");
// QueryAll("h");
// QueryByCondition1("h");
QueryByCondition2("h");
// QueryByCondition3("h");
// deleteRow("h","abcdef");
// deleteByCondition("h","abcdef");
// dropTable("h");
} /**
* 创建表
* @param tableName
*/
public static void createTable(String tableName) {
System.out.println("start create table ......");
try {
HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
if (hBaseAdmin.tableExists(tableName)) {// 如果存在要创建的表,那么先删除,再创建
hBaseAdmin.disableTable(tableName);
hBaseAdmin.deleteTable(tableName);
System.out.println(tableName + " is exist,detele....");
}
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
tableDescriptor.addFamily(new HColumnDescriptor("column1"));
tableDescriptor.addFamily(new HColumnDescriptor("column2"));
tableDescriptor.addFamily(new HColumnDescriptor("column3"));
tableDescriptor.addFamily(new HColumnDescriptor("cf"));
hBaseAdmin.createTable(tableDescriptor);
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end create table ......");
} /**
* 插入数据
* @param tableName
*/
public static void insertData(String tableName) {
System.out.println("start insert data ......");
HTablePool pool = new HTablePool(configuration, 1000);
HTableInterface table = pool.getTable(tableName);
Put put = new Put("abcdef".getBytes());// 一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值
put.add("column1".getBytes(), null, "aaa".getBytes());// 本行数据的第一列
put.add("column2".getBytes(), null, "bbb".getBytes());// 本行数据的第三列
put.add("column3".getBytes(), null, "ccc".getBytes());// 本行数据的第三列
Put put1 = new Put("han_r1".getBytes());
put1.addColumn("cf".getBytes(),"a".getBytes(),"han_cf_a".getBytes()); try {
table.put(put);
table.put(put1);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end insert data ......");
} /**
* 删除一张表
* @param tableName
*/
public static void dropTable(String tableName) {
try {
HBaseAdmin admin = new HBaseAdmin(configuration);
admin.disableTable(tableName);
admin.deleteTable(tableName);
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
/**
* 根据 rowkey删除一条记录
* @param tablename
* @param rowkey
*/
public static void deleteRow(String tablename, String rowkey) {
try {
HTable table = new HTable(configuration, tablename);
List list = new ArrayList();
Delete d1 = new Delete(rowkey.getBytes());
list.add(d1); table.delete(list);
System.out.println("删除行成功!"); } catch (IOException e) {
e.printStackTrace();
} } /**
* 组合条件删除
* @param tablename
* @param rowkey
*/
public static void deleteByCondition(String tablename, String rowkey) {
//目前还没有发现有效的API能够实现 根据非rowkey的条件删除 这个功能能,还有清空表全部数据的API操作 } /**
* 查询所有数据
* @param tableName
*/
public static void QueryAll(String tableName) {
HTablePool pool = new HTablePool(configuration, 1000);
HTableInterface table = pool.getTable(tableName);
try {
ResultScanner rs = table.getScanner(new Scan());
for (Result r : rs) {
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println("列:" + new String(keyValue.getFamily())
+
" :" + new String(keyValue.getQualifier())
+ "====值:" + new String(keyValue.getValue())
+ "===TIME===" + String.valueOf(keyValue.getTimestamp() ) );
}
}
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 单条件查询,根据rowkey查询唯一一条记录
* @param tableName
*/
public static void QueryByCondition1(String tableName) {
System.out.println(" QueryByCondition1");
HTablePool pool = new HTablePool(configuration, 1000);
HTableInterface table = pool.getTable(tableName);
try {
Get scan = new Get("abcdef".getBytes());// 根据rowkey查询
Result r = table.get(scan);
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println("列:" + new String(keyValue.getFamily())
+
" :" + new String(keyValue.getQualifier())
+ "====值:" + new String(keyValue.getValue())
+ "===TIME===" + String.valueOf(keyValue.getTimestamp() ) );
} } catch (IOException e) {
e.printStackTrace();
}
} /**
* 单条件按查询,查询多条记录
* @param tableName
*/
public static void QueryByCondition2(String tableName) {
System.out.println(" QueryByCondition2");
try {
HTablePool pool = new HTablePool(configuration, 1000);
HTableInterface table = pool.getTable(tableName);
FilterList filterList=new FilterList();
SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes
.toBytes("column1"), null, CompareOp.EQUAL, Bytes
.toBytes("aaa") ); // 当列column1的值为aaa时进行查询
filter.setFilterIfMissing(true);
filterList.addFilter(filter);
Scan s = new Scan();
s.setFilter(filterList);
ResultScanner rs = table.getScanner(s);
for (Result r : rs) {
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println("列:" + new String(keyValue.getFamily())
+
" :" + new String(keyValue.getQualifier())
+ "====值:" + new String(keyValue.getValue())
+ "===TIME===" + String.valueOf(keyValue.getTimestamp()) );
}
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
} } /**
* 组合条件查询
* @param tableName
*/
public static void QueryByCondition3(String tableName) {
System.out.println(" QueryByCondition3");
try {
HTablePool pool = new HTablePool(configuration, 1000);
HTableInterface table = pool.getTable(tableName); List<Filter> filters = new ArrayList<Filter>(); Filter filter1 = new SingleColumnValueFilter(Bytes
.toBytes("column1"), null, CompareOp.EQUAL, Bytes
.toBytes("aaa"));
filters.add(filter1); Filter filter2 = new SingleColumnValueFilter(Bytes
.toBytes("column2"), null, CompareOp.EQUAL, Bytes
.toBytes("bbb"));
filters.add(filter2); Filter filter3 = new SingleColumnValueFilter(Bytes
.toBytes("column3"), null, CompareOp.EQUAL, Bytes
.toBytes("ccc"));
filters.add(filter3); FilterList filterList1 = new FilterList(filters); Scan scan = new Scan();
scan.setFilter(filterList1);
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println("列:" + new String(keyValue.getFamily())
+
" :" + new String(keyValue.getQualifier())
+ "====值:" + new String(keyValue.getValue())
+ "===TIME===" + String.valueOf(keyValue.getTimestamp()) );
}
}
rs.close(); } catch (Exception e) {
e.printStackTrace();
} } }
根据 SingleColumnValueFilter 按照列和值查询,会把没有该列的行查询出来,加入
filter.setFilterIfMissing(true);
就可以了

hbase scan 的例子的更多相关文章

  1. HBase Scan流程分析

    HBase Scan流程分析 HBase的读流程目前看来比较复杂,主要由于: HBase的表数据分为多个层次,HRegion->HStore->[HFile,HFile,...,MemSt ...

  2. HBase Scan Timeout-OutOfOrderScannerNextException

    最近迁移数据时需要执行大Scan,HBase集群经常碰到以下日志: Exception in thread "main" org.apache.hadoop.hbase.DoNot ...

  3. <HBase><Scan>

    Overview The Scan operation for HBase. Scan API All operations are identical to Get with the excepti ...

  4. HBase Scan,Get用法

    Scan,get用法 1. get help帮助信息 从下列get用法信息可以看出 get 后面可以跟table表名,rowkey,以及column,value.但是如果想通过get直接获取一个表中的 ...

  5. hbase java 基本例子

    一下代码实用  0.99.0 以后的版本. package hadoop; import java.io.IOException; import java.util.ArrayList; import ...

  6. Hbase Scan的重要参数

    Scan是操作Hbase中非常常用的一个操作,虽然前面的Hbase API操作简单的介绍了Scan的操作,但不够详细,由于Scan非常常用,关于其详细的整理也是很有必要的. Scan HBase中的数 ...

  7. HBase scan 时 异常 ScannerTimeoutException 解决

    org.apache.Hadoop.hbase.client.ScannerTimeoutException: 60622ms passed since the last invocation, ti ...

  8. HBase scan setBatch和setCaching的区别

    HBase的查询实现只提供两种方式: 1.按指定RowKey获取唯一一条记录,get方法(org.apache.hadoop.hbase.client.Get) 2.按指定的条件获取一批记录,scan ...

  9. HBase scan setBatch和setCaching的区别【转】

    转自:http://blog.csdn.net/caoli98033/article/details/44650497 HBase的查询实现只提供两种方式: 1.按指定RowKey获取唯一一条记录,g ...

随机推荐

  1. log4cplus 在配置文件中设置文件路径,程序自动创建目录,且在日志文件前按日期创建相应的目录

    #include <string> #include <cstdio> #include <log4cplus/logger.h> #include <log ...

  2. java中Collection和Collections的区别

    1.Collection: 它是java集合类的一个通用接口,所有集合类都实现的它 2.Collections: 它是一个封装集合类常用工具方法的类,不能被示例化,只支持静态调用

  3. 微信小程序上传文件

    wx.chooseImage({ count: 1, // 默认9 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourc ...

  4. Android sdk tool android 命令参数

      命令参数说明: $ ./android --? Error: Flag '--?' is not a valid global flag. Did you mean to specify it a ...

  5. 异常:“System.Reflection.Metadata”已拥有为“System.Collections.Immutable”定义的依赖项

    参考动态执行T4模板:https://msdn.microsoft.com/zh-cn/library/bb126579.aspx 我项目是.NET Framework 4.5控制台应用程序写的. 执 ...

  6. centOS安装网卡驱动

    作为一个小白来说,安装驱动之类的真是无心下手的感觉,在学习了http://www.centoscn.com/image-text/config/2013/0816/1269.html这篇帖子的步骤之后 ...

  7. 通过seekBar改变图片的透明度

    作者:堕落的天使 对应的图片 activity_main.xml(代码) <RelativeLayout xmlns:android="http://schemas.android.c ...

  8. Django 浏览页面点击计数(通用视图)

    通常情况下在Views.py中直接写一个视图函数就可以了,由于每次点出详情时都会经视图函数处理, 所以可以在此视图函数中对浏览次数进行"+1" 操作. 对应的url:url(r'^ ...

  9. c#摄像头编程实例 (转)

    c#摄像头编程实例 摄像头编程 安装摄像头后,一般可以找到一个avicap32.dll文件 这是一个关于设想头的类 using  system;using  System.Runtime.Intero ...

  10. C++设计模式-Bridge桥接模式

    作用:将抽象部份与它的实现部份分离,使它们都可以独立地变化. 将抽象(Abstraction)与实现(Implementation)分离,使得二者可以独立地变化. 桥接模式号称设计模式中最难理解的模式 ...