1 shell command
1.1Create
Create 表名, 列族名1, 列族名2
Create ‘test1’,’aa’,’bb’
1.2 put
put表名,行健, 列族:列名,值
例子
put 'test1','row1','aa:a','1'
put 'test1','row1','aa:a1','3'
put 'test1','row2','aa:a','3'
1.3 get
get 表名,行健
get 'test1','row1'
get 表名,行健,列
get 'test1','row1',’aa:a’
get 表名,行健{过滤条件}
get 'test1','row1',{COLUMN=>'aa:a'}
1.4 scan
Scan表名
scan 'test1'
Scan 表名,{条件}
scan 'test1',{COLUMNS=>['aa:a']}
条件COLUMNS STARTROW STARTROW CACHE_BLOCKS LIMIT RAW FILTER(自定义,缺省)
1.5Disable表名
更改表状态到离线
1.6enable
更改表状态到在线
1.7Drop表名
删除表,注:删除表以前需要disable
1.8list
列出所有的表
2 java api
2.1 必须条件
Hbase-site.xml 设置hbase的位置
Core-site.xml hadoop配置文件
Hbase-***.jar
2.2 配置
Configuration conf=HBaseConfiguration.create();
HBaseAdmin admin=new HBaseAdmin(conf);
2.3 表
HTableDescriptor htd=new HTableDescriptor(表名);
2.4建表
admin.createTable(desc)
2.5 列出所有表
HTableDescriptor[]tables=admin.listTables();
2.6 增加列族
htd.addFamily(hcd);
2.7 插入值
新增,修改都是用put
HTable table=new HTable(conf, tablename);
byte[] row1=Bytes.toBytes("row1");
Put p1=new Put(row1);
byte[] databytes=Bytes.toBytes("aa");//列族
p1.add(databytes,Bytes.toBytes("1"),Bytes.toBytes("value1"));//增加列
table.put(p1);
2.8取行值get
Get g=newGet(Bytes.toBytes("row1"));
Resultresult=table.get(g);
System.out.println( result.getValue("aa".getBytes(),"1".getBytes()));//aa 列族.1 列名
2.9scan
2.9.1查询
HTabletable=new HTable(conf,"test1");
byte[] row1=Bytes.toBytes("row1");
Scanscan=new Scan();
scanner=table.getScanner(scan);
System.out.println(scanner.next());
2.9.2 Scan条件:
addColumn增加列
例如:scan.addColumn(Bytes.toBytes("aa"),Bytes.toBytes("a"));
addFamily增加列族
scan.addColumn(Bytes.toBytes("aa"),Bytes.toBytes("a"));
例如:scan.addFamily( Bytes.toBytes("aa") );
setBatch; 一次批量读的数目, 行有效,假如一个行,列数目没有超过batch,一次读到行结尾
setFilter 增加过滤器
2.9.3过滤器
常用filter
9.4.1. Structural
Structural Filterscontain other Filters.
9.4.1.1. FilterList
FilterList representsa list of Filters with a relationship of FilterList.Operator.MUST_PASS_ALL
or FilterList.Operator.MUST_PASS_ONE
between the Filters.The following example shows an 'or' between two Filters (checking for either'my value' or 'my other value' on the same attribute).
FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ONE);
SingleColumnValueFilter filter1 = new SingleColumnValueFilter(
cf,
column,
CompareOp.EQUAL,
Bytes.toBytes("my value")
);
list.add(filter1);
SingleColumnValueFilter filter2 = new SingleColumnValueFilter(
cf,
column,
CompareOp.EQUAL,
Bytes.toBytes("my other value")
);
list.add(filter2);
scan.setFilter(list);
9.4.2. Column Value
9.4.2.1. SingleColumnValueFilter
SingleColumnValueFilter canbe used to test column values for equivalence (CompareOp.EQUAL
),inequality (CompareOp.NOT_EQUAL
), or ranges(e.g., CompareOp.GREATER
). The folowing isexample of testing equivalence a column to a String value "myvalue"...
SingleColumnValueFilter filter = new SingleColumnValueFilter(
cf,
column,
CompareOp.EQUAL,
Bytes.toBytes("my value")
);
scan.setFilter(filter);
9.4.3. Column ValueComparators
There are severalComparator classes in the Filter package that deserve special mention. TheseComparators are used in concert with other Filters, such as Section 9.4.2.1,“SingleColumnValueFilter”.
9.4.3.1. RegexStringComparator
RegexStringComparator supportsregular expressions for value comparisons.
RegexStringComparator comp = new RegexStringComparator("my."); // any value that starts with 'my'
SingleColumnValueFilter filter = new SingleColumnValueFilter(
cf,
column,
CompareOp.EQUAL,
comp
);
scan.setFilter(filter);
See the Oracle JavaDocfor supported RegEx patterns in Java.
9.4.3.2. SubstringComparator
SubstringComparator canbe used to determine if a given substring exists in a value. The comparison iscase-insensitive.
SubstringComparator comp = new SubstringComparator("y val"); // looking for 'my value'
SingleColumnValueFilter filter = new SingleColumnValueFilter(
cf,
column,
CompareOp.EQUAL,
comp
);
scan.setFilter(filter);
9.4.3.3. BinaryPrefixComparator
9.4.3.4. BinaryComparator
See BinaryComparator.
9.4.4. KeyValue Metadata
As HBase stores datainternally as KeyValue pairs, KeyValue Metadata Filters evaluate the existenceof keys (i.e., ColumnFamily:Column qualifiers) for a row, as opposed to valuesthe previous section.
9.4.4.1. FamilyFilter
FamilyFilter canbe used to filter on the ColumnFamily. It is generally a better idea to selectColumnFamilies in the Scan than to do it with a Filter.
9.4.4.2. QualifierFilter
QualifierFilter canbe used to filter based on Column (aka Qualifier) name.
9.4.4.3. ColumnPrefixFilter
ColumnPrefixFilter canbe used to filter based on the lead portion of Column (aka Qualifier) names.
A ColumnPrefixFilterseeks ahead to the first column matching the prefix in each row and for eachinvolved column family. It can be used to efficiently get a subset of thecolumns in very wide rows.
Note: The same columnqualifier can be used in different column families. This filter returns allmatching columns.
Example: Find allcolumns in a row and family that start with "abc"
HTableInterface t = ...;
byte[] row = ...;
byte[] family = ...;
byte[] prefix = Bytes.toBytes("abc");
Scan scan = new Scan(row, row); // (optional) limit to one row
scan.addFamily(family); // (optional) limit to one family
Filter f = new ColumnPrefixFilter(prefix);
scan.setFilter(f);
scan.setBatch(10); // set this if there could be many columns returned
ResultScanner rs = t.getScanner(scan);
for (Result r = rs.next(); r != null; r = rs.next()) {
for (KeyValue kv : r.raw()) {
// each kv represents a column
}
}
rs.close();
9.4.4.4. MultipleColumnPrefixFilter
MultipleColumnPrefixFilter behaveslike ColumnPrefixFilter but allows specifying multiple prefixes.
LikeColumnPrefixFilter, MultipleColumnPrefixFilter efficiently seeks ahead to thefirst column matching the lowest prefix and also seeks past ranges of columnsbetween prefixes. It can be used to efficiently get discontinuous sets ofcolumns from very wide rows.
Example: Find allcolumns in a row and family that start with "abc" or "xyz"
HTableInterface t = ...;
byte[] row = ...;
byte[] family = ...;
byte[][] prefixes = new byte[][] {Bytes.toBytes("abc"), Bytes.toBytes("xyz")};
Scan scan = new Scan(row, row); // (optional) limit to one row
scan.addFamily(family); // (optional) limit to one family
Filter f = new MultipleColumnPrefixFilter(prefixes);
scan.setFilter(f);
scan.setBatch(10); // set this if there could be many columns returned
ResultScanner rs = t.getScanner(scan);
for (Result r = rs.next(); r != null; r = rs.next()) {
for (KeyValue kv : r.raw()) {
// each kv represents a column
}
}
rs.close();
9.4.4.5. ColumnRangeFilter
A ColumnRangeFilter allowsefficient intra row scanning.
A ColumnRangeFilter canseek ahead to the first matching column for each involved column family. It canbe used to efficiently get a 'slice' of the columns of a very wide row. i.e.you have a million columns in a row but you only want to look at columnsbbbb-bbdd.
Note: The same columnqualifier can be used in different column families. This filter returns allmatching columns.
Example: Find allcolumns in a row and family between "bbbb" (inclusive) and"bbdd" (inclusive)
HTableInterface t = ...;
byte[] row = ...;
byte[] family = ...;
byte[] startColumn = Bytes.toBytes("bbbb");
byte[] endColumn = Bytes.toBytes("bbdd");
Scan scan = new Scan(row, row); // (optional) limit to one row
scan.addFamily(family); // (optional) limit to one family
Filter f = new ColumnRangeFilter(startColumn, true, endColumn, true);
scan.setFilter(f);
scan.setBatch(10); // set this if there could be many columns returned
ResultScanner rs = t.getScanner(scan);
for (Result r = rs.next(); r != null; r = rs.next()) {
for (KeyValue kv : r.raw()) {
// each kv represents a column
}
}
rs.close();
Note: Introduced inHBase 0.92