hbase 的 shell 操作和 java 操作

hbase shell 操作

ddl 操作

列出所有表
list

创建表
create 't1','f1'

增加列族
disable 't1'
alter 't1', {NAME => 'f2', VERSIONS => 5}
enable 't1'

删除列族
alter 't1', 'delete' => 'f2'

描述表
describe 't1'

删除表
disable 't1'
drop 't1'



dml 操作

create 'users','info','address'

插入
put 'users','xiaoming','info:age','24'
put 'users','xiaoming','info:company','alibaba'
put 'users','xiaoming','address:contry','japan'
put 'users','zhangsan','info:age','25'
put 'users','zhangsan','address:contry','china'

查询
scan 'users'
scan 'users',{COLUMN=>'info'}
count 'users'

get 'users','xiaoming'
get 'users','xiaoming','info'
get 'users','xiaoming','info:age'
get 'users','xiaoming',{COLUMN => 'info:age',VERSIONS => 4}
get 'users','xiaoming',{COLUMN=>'info:age',TIMESTAMP=>1364874937056}

更新
put 'users','xiaoming','info:age' ,'29'
get 'users','xiaoming','info:age'
put 'users','xiaoming','info:age' ,'30'
get 'users','xiaoming','info:age'

删除
delete 'users','xiaoming','info:age',1406091705230
deleteall 'users','xiaoming'

删除所有记录
truncate 'users'


---------------------------------------------------------------------
hbase的java操作

package hbase;

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.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.ColumnPrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseTest {

private static String TABLE_NAME = "t1";
private static String ROW = "zhangsan";
private static String FAMILY = "fc1";
private static String COLUMN = "weight";


/**
* @param args
* @throws 
* @throws Exception
*/
public static void main(String[] args) throws Exception {

Configuration config = getConfig();

//HBaseAdmin admin = new HBaseAdmin(config);
//ddl操作
//createTable(admin);
//addColumn(admin);
//delTable(admin);
//admin.close();

//dml操作
//HTable htable = new HTable(config,TABLE_NAME);
//put(htable);
//delColumns(htable);
//delRow(htable);
//scan(htable);
//get(htable);
//htable.close();

}

private static void get(HTable htable) throws IOException {
Get get = new Get(Bytes.toBytes(ROW));
Result result = htable.get(get);
byte[] value = result.getValue(Bytes.toBytes(FAMILY),Bytes.toBytes(COLUMN));
System.out.println(new String(value));
}

private static void scan(HTable htable) throws IOException {
Scan sc = new Scan();
sc.setFilter(new ColumnPrefixFilter(Bytes.toBytes("weight")));
ResultScanner results = htable.getScanner(sc);
//System.out.println(results);
if(results != null){
for(Result result:results){
byte[] value = result.getValue(Bytes.toBytes(FAMILY),Bytes.toBytes(COLUMN));
System.out.println(new String(value));
}
}
}

private static void delRow(HTable htable) throws IOException {
Delete row = new Delete(Bytes.toBytes(ROW));
htable.delete(row);
}

private static void delColumns(HTable htable) throws IOException {
Delete row = new Delete(Bytes.toBytes(ROW));
row.deleteColumns(Bytes.toBytes(FAMILY), Bytes.toBytes(COLUMN));
htable.delete(row);
}

private static void put(HTable htable) throws IOException {
Put put = new Put(Bytes.toBytes(ROW));
put.add(Bytes.toBytes(FAMILY), Bytes.toBytes(COLUMN), Bytes.toBytes("15"));
htable.put(put);
}

private static void delTable(HBaseAdmin admin) throws IOException {
if(admin.tableExists(TABLE_NAME)){
admin.disableTable(TABLE_NAME);
admin.deleteTable(TABLE_NAME);
}
}

private static void addColumn(HBaseAdmin admin) throws IOException {
HColumnDescriptor column2 = new HColumnDescriptor("fc2");
column2.setMaxVersions(5);
admin.disableTable(TABLE_NAME);
admin.addColumn(TABLE_NAME, column2);
}

private static void createTable(HBaseAdmin admin) throws IOException {
HTableDescriptor table = new HTableDescriptor(TABLE_NAME);
table.addFamily(new HColumnDescriptor(FAMILY));
if(!admin.tableExists(TABLE_NAME)){
admin.createTable(table);
}
}

private static Configuration getConfig() {
Configuration config = HBaseConfiguration.create();
config.set("hbase.rootdir", "hdfs://hadoop:9000/hbase");
//使用eclipse时必须添加这个,否则无法定位
config.set("hbase.zookeeper.quorum", "hadoop");
return config;
}

}


猜你喜欢

转载自jsh0401.iteye.com/blog/2095780