import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Random;
public class HBaseShell {
private Connection connection;
private Admin admin;
@Before
public void before() throws IOException {
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","CentOS");
connection= ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}
//创建
@Test
public void testcreatenamespace() throws IOException {
NamespaceDescriptor namespace=NamespaceDescriptor.create("dd")
.addConfiguration("author","zhangsan")
.build();
admin.createNamespace(namespace);
}
//删除
@Test
public void dropnamespace() throws IOException {
admin.deleteNamespace("dd");
}
//创建表
@Test
public void createtable() throws IOException {
TableName tableName = TableName.valueOf("dd:t_user");
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
//创建列簇
HColumnDescriptor cf1 = new HColumnDescriptor("cf1");
//为列簇cf1指定保存的最大版本数
cf1.setMaxVersions(10);
HColumnDescriptor cf2 = new HColumnDescriptor("cf2");
//为列簇cf2指定保存的最大版本数
cf2.setMaxVersions(10);
//设置最大存活时间
cf2.setTimeToLive(300);//5分钟
tableDescriptor.addFamily(cf1);
tableDescriptor.addFamily(cf2);
admin.createTable(tableDescriptor);
}
//为表t_user插入值
@Test
public void testput() throws IOException {
//获取表
TableName tableName = TableName.valueOf("dd:t_user");
Table table = connection.getTable(tableName);
//插入值
Put put = new Put("100".getBytes());
put.addColumn("cf1".getBytes(),"name".getBytes(),"张三丰".getBytes());
put.addColumn("cf1".getBytes(),"age".getBytes(), Bytes.toBytes(256));
put.addColumn("cf1".getBytes(),"sex".getBytes(), Bytes.toBytes(true));
table.put(put);
}
//为表t_user批量插入
@Test
public void tsetbatchput() throws IOException {
TableName tableName = TableName.valueOf("dd:t_user");
String[] str ={"www.cc.com","www.bb.com"};
//创建缓冲区
BufferedMutator bufferedMutator = connection.getBufferedMutator(tableName);
for (int i =1;i<=1000;i++) {
String s=str[new Random().nextInt(2)];
String rowkey =s;
if(i<10){
rowkey+="000"+i;
}else if(i<100){
rowkey+="0"+i;
}else if(i<=1000){
rowkey+=""+i;
}
Put put = new Put(rowkey.getBytes());
put.addColumn("cf1".getBytes(),"name".getBytes(),("user"+i).getBytes());
put.addColumn("cf1".getBytes(),"age".getBytes(),Bytes.toBytes(i));
put.addColumn("cf1".getBytes(),"sex".getBytes(),i%2==0?Bytes.toBytes("man"):Bytes.toBytes("women"));
put.addColumn("cf1".getBytes(),"company".getBytes(),s.getBytes());
//放入缓冲区
bufferedMutator.mutate(put);
}
//提交到数据库
bufferedMutator.flush();
bufferedMutator.close();
}
//修改单条记录
@Test
public void testupdate() throws IOException {
TableName tableName = TableName.valueOf("dd:t_user");
Table table = connection.getTable(tableName);
Put put = new Put("100".getBytes());
put.addColumn("cf1".getBytes(),"name".getBytes(),"wanfwu".getBytes());
put.addColumn("cf1".getBytes(),"age".getBytes(),Bytes.toBytes(25));
put.addColumn("cf1".getBytes(),"sex".getBytes(),Bytes.toBytes("man"));
put.addColumn("cf1".getBytes(),"company".getBytes(),"abc".getBytes());
table.put(put);
}
//删除表
@Test
public void testdeletetable() throws IOException {
TableName tableName = TableName.valueOf("dd:t_user");
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
//展示表
@Test
public void testshowtables() throws IOException {
TableName[] tableNames = admin.listTableNames();
for(TableName tableName:tableNames){
System.out.println(tableName.getNameAsString());
}
}
//截断表
@Test
public void testtable() throws IOException {
TableName tableName = TableName.valueOf("dd:t_user");
admin.disableTable(tableName);
admin.truncateTable(tableName,true);
admin.close();
}
//判断表是否存在
@Test
public void testable1() throws IOException {
TableName tableName = TableName.valueOf("dd:t_user");
boolean b = admin.tableExists(tableName);
System.out.println(b);
}
//插入数据
@Test
public void testinserttable() throws IOException {
TableName tableName = TableName.valueOf("dd:t_user");
HTable hTable = (HTable) connection.getTable(tableName);
Put put = new Put(Bytes.toBytes("user:123"));
put.addColumn(Bytes.toBytes("cf1"),
Bytes.toBytes("name"),
Bytes.toBytes("lisi"));
put.addColumn(Bytes.toBytes("cf1"),
Bytes.toBytes("salary"),
Bytes.toBytes(1234));
hTable.put(put);
hTable.flushCommits();
hTable.close();
}
//删除数据
@Test
public void testdeletetables() throws IOException {
TableName tableName = TableName.valueOf("dd:t_user");
HTable hTable =(HTable)connection.getTable(tableName);
Delete delete = new Delete("user:123".getBytes());
hTable.delete(delete);
hTable.close();
}
//查询数据
@Test
public void testshowtable() throws IOException {
TableName tableName = TableName.valueOf("dd:t_user");
HTable hTable = (HTable)connection.getTable(tableName);
Get get = new Get("user:123".getBytes());
get.setMaxVersions(10);//设置要查的版本
Result result = hTable.get(get);
CellScanner cellScanner = result.cellScanner();
while (cellScanner.advance()){
Cell cell = cellScanner.current();
String qualifier= Bytes.toString(CellUtil.cloneQualifier(cell));
String famliy= Bytes.toString(CellUtil.cloneFamily(cell));
String value=Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(famliy+":"+qualifier+" "+value);
}
}
//查询数据
@Test
public void testshowtable1() throws IOException {
TableName tableName = TableName.valueOf("dd:t_user");
HTable hTable =(HTable)connection.getTable(tableName);
Get get = new Get("user:123".getBytes());
get.setMaxVersions();
Result result = hTable.get(get);
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}
hTable.close();
}
//查询数据
@Test
public void testshowtable2() throws IOException {
TableName tableName = TableName.valueOf("dd:t_user");
HTable hTable = (HTable)connection.getTable(tableName);
Get get = new Get("user:123".getBytes());
Result result = hTable.get(get);
List<Cell> columnCells = result.getColumnCells(Bytes.toBytes("cf1"), Bytes.toBytes("name"));
for (Cell columnCell : columnCells) {
System.out.println(Bytes.toString(CellUtil.cloneQualifier(columnCell)));
System.out.println(Bytes.toString(CellUtil.cloneValue(columnCell)));
}
hTable.close();
}
//数据查询
@Test
public void testshowtable3() throws IOException {
TableName tableName = TableName.valueOf("dd:t_user");
HTable hTable = (HTable)connection.getTable(tableName);
Scan scan = new Scan();
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
List<Cell> columnCells = result.getColumnCells(Bytes.toBytes("cf1"), Bytes.toBytes("name"));
for (Cell columnCell : columnCells) {
System.out.println(Bytes.toString(CellUtil.cloneQualifier(columnCell)));
System.out.println(Bytes.toString(CellUtil.cloneValue(columnCell)));
}
}
hTable.close();
}
@After
public void after() throws IOException {
admin.close();
connection.close();
}
}
HBase--部分API
最新推荐文章于 2024-06-17 21:18:30 发布