package com.sxt.hbase.Daemon;
import java.io.IOException;
import java.io.InterruptedIOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
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.RetriesExhaustedWithDetailsException;
import org.junit.Before;
import org.junit.Test;
public class Hbase {
HBaseAdmin admin = null;
private String tn="phone";
HTable table=null;
@Before
public void init() throws Exception{
Configuration conf=new Configuration();
conf.set("hbase.zookeeper.quorum", "node01,node02,node03");
table =new HTable(conf, tn.getBytes());
}
@Test
public void createTable() throws Exception{
HTableDescriptor desc=new HTableDescriptor(TableName.valueOf(tn));
HColumnDescriptor family=new HColumnDescriptor("cf".getBytes());
desc.addFamily(family);
if(admin.tableExists(tn)){
admin.disableTable(tn);
admin.deleteTable(tn);
}
admin.createTable(desc);
}
@Test
public void insertDB() throws IOException{
String rowkey="1111";
Put put=new Put(rowkey.getBytes());
put.add("cf".getBytes(), "name".getBytes(), "zhangsan".getBytes());
put.add("cf".getBytes(), "age".getBytes(), "12".getBytes());
put.add("cf".getBytes(), "sex".getBytes(), "man".getBytes());
table.put(put);
}
@Test
public void get() throws Exception{
String rowkey="1111";
Get get =new Get(rowkey.getBytes());
get.addColumn("cf".getBytes(), "name".getBytes());
get.addColumn("cf".getBytes(), "age".getBytes());
get.addColumn("cf".getBytes(), "sex".getBytes());
Result rs=table.get(get);
Cell cell=rs.getColumnLatestCell("cf".getBytes(), "name".getBytes());
Cell cell2=rs.getColumnLatestCell("cf".getBytes(), "age".getBytes());
Cell cell3=rs.getColumnLatestCell("cf".getBytes(), "sex".getBytes());
System.out.println(new String(CellUtil.cloneValue(cell)));
System.out.println(new String(CellUtil.cloneValue(cell2)));
System.out.println(new String(CellUtil.cloneValue(cell3)));
}
@Test
public void close() throws Exception{
if(admin!=null){
admin.close();
}
}
相关文章: