一、建立连接,创建表
public class Demo01Test {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","master:2181,node1:2181,node2:2181");
Connection conn = ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
HTableDescriptor testAPI = new HTableDescriptor(TableName.valueOf("TestJavaAPI"));
HColumnDescriptor cf1 = new HColumnDescriptor("cf1");
cf1.setMaxVersions(5);
cf1.setTimeToLive(5);
testAPI.addFamily(cf1);
admin.createTable(testAPI);
admin.close();
conn.close();
}
二、一些常用的方法
public class Demo02API {
Admin admin=null;
Connection conn = null;
@Before
public void init(){
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","master:2181,node1:2181,node2:2181");
try {
conn = ConnectionFactory.createConnection(conf);
admin = conn.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void createTable() throws Exception{
HTableDescriptor test2 = new HTableDescriptor(TableName.valueOf("test2"));
HColumnDescriptor cf1 = new HColumnDescriptor("cf1");
cf1.setMaxVersions(3);
test2.addFamily(cf1);
admin.createTable(test2);
}
@Test
public void listTable()throws Exception{
TableName[] tableNames = admin.listTableNames();
for (TableName tableName : tableNames) {
System.out.println(tableName);
}
}
@Test
public void dropTable() throws Exception{
TableName table = TableName.valueOf("test001");
admin.disableTable(table);
admin.deleteTable(table);
}
@Test
public void modifyTable() throws Exception{
TableName table2 = TableName.valueOf("test2");
HTableDescriptor hTableDescriptor = admin.getTableDescriptor(table2);
hTableDescriptor.addFamily(new HColumnDescriptor("cf2"));
admin.modifyTable(table2,hTableDescriptor);
}
@Test
public void put() throws Exception{
Table test2 = conn.getTable(TableName.valueOf("test2"));
Put put = new Put("003".getBytes());
put.addColumn("cf1".getBytes(),"name".getBytes(),"王五".getBytes());
put.addColumn("cf1".getBytes(),"age".getBytes(), Bytes.toBytes(22));
put.addColumn("cf1".getBytes(),"gender".getBytes(),"man".getBytes());
put.addColumn("cf2".getBytes(),"clazz".getBytes(),"文科三班".getBytes());
test2.put(put);
}
@Test
public void get()throws Exception{
Table test2 = conn.getTable(TableName.valueOf("test2"));
Get get = new Get("001".getBytes());
Result rs = test2.get(get);
byte[] cf1 = "cf1".getBytes();
byte[] cf2 = "cf2".getBytes();
String name = Bytes.toString(rs.getValue(cf1, "name".getBytes()));
int age = Bytes.toInt(rs.getValue(cf1, "age".getBytes()));
String gender = Bytes.toString(rs.getValue(cf1, "gender".getBytes()));
String clazz = Bytes.toString(rs.getValue(cf2, "clazz".getBytes()));
System.out.println(name+","+age+","+gender+","+clazz);
}
@Test
public void scan()throws Exception{
Table test2 = conn.getTable(TableName.valueOf("test2"));
Scan scan = new Scan();
scan.withStartRow("001".getBytes());
scan.withStopRow("004".getBytes());
ResultScanner scanner = test2.getScanner(scan);
for (Result rs : scanner) {
String rowkey = Bytes.toString(rs.getRow());
if("001".equals(rowkey) || "002".equals(rowkey)){
byte[] cf1 = "cf1".getBytes();
byte[] cf2 = "cf2".getBytes();
String name = Bytes.toString(rs.getValue(cf1, "name".getBytes()));
int age = Bytes.toInt(rs.getValue(cf1, "age".getBytes()));
String gender = Bytes.toString(rs.getValue(cf1, "gender".getBytes()));
String clazz = Bytes.toString(rs.getValue(cf2, "clazz".getBytes()));
System.out.println(name+","+age+","+gender+","+clazz);
}
else if("003".equals(rowkey)){
byte[] cf1 = "cf1".getBytes();
byte[] cf2 = "cf2".getBytes();
String name = Bytes.toString(rs.getValue(cf1, "name".getBytes()));
String clazz = Bytes.toString(rs.getValue(cf2, "clazz".getBytes()));
System.out.println(name+","+clazz);
}
}
}
@Test
public void cellUtil()throws Exception{
Table test2 = conn.getTable(TableName.valueOf("test2"));
Scan scan = new Scan();
scan.withStartRow("001".getBytes());
scan.withStopRow("004".getBytes());
ResultScanner scanner = test2.getScanner(scan);
for (Result rs : scanner) {
List<Cell> columnCells = rs.listCells();
for (Cell cell : columnCells) {
String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
String cf = Bytes.toString(CellUtil.cloneFamily(cell));
String qf = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println("rowkey: "+rowkey+" columnFamily: "+cf+" Qualifier: "+qf+" value: "+value);
}
}
}
@Test
public void putAll()throws Exception{
TableName students = TableName.valueOf("students");
if(!admin.tableExists(students)){
HTableDescriptor hTableDescriptor = new HTableDescriptor(students);
HColumnDescriptor info = new HColumnDescriptor("info");
hTableDescriptor.addFamily(info);
admin.createTable(hTableDescriptor);
}
Table table = conn.getTable(students);
BufferedReader bf = new BufferedReader(new FileReader("data/students.txt"));
String line = null;
while ((line = bf.readLine()) != null){
String[] s = line.split(",");
String id = s[0];
String name = s[1];
String age = s[2];
String gender = s[3];
String clazz = s[4];
Put put = new Put(id.getBytes());
byte[] info = "info".getBytes();
put.addColumn(info,"name".getBytes(),name.getBytes());
put.addColumn(info,"age".getBytes(),age.getBytes());
put.addColumn(info,"gender".getBytes(),gender.getBytes());
put.addColumn(info,"clazz".getBytes(),clazz.getBytes());
table.put(put);
}
}
@Test
public void getAll() throws Exception{
Table students = conn.getTable(TableName.valueOf("students"));
Scan scan = new Scan();
ResultScanner scanner = students.getScanner(scan);
for (Result rs : scanner) {
String rowkey = Bytes.toString(rs.getRow());
System.out.print(rowkey + " ");
List<Cell> cells = rs.listCells();
for (Cell cell : cells) {
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.print(value + " ");
}
System.out.println(" ");
}
}
@Test
public void loadData()throws Exception{
TableName students = TableName.valueOf("students");
Table table = conn.getTable(students);
String rowkey = "1500100485";
System.out.print(rowkey + " ");
Get get = new Get(rowkey.getBytes());
Result rs = table.get(get);
for (Cell cell : rs.listCells()) {
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.print(value + " ");
}
System.out.println(" ");
}
@Test
public void delet()throws Exception{
Table test2 = conn.getTable(TableName.valueOf("test2"));
Delete delete = new Delete("001".getBytes());
test2.delete(delete);
}
@After
public void close(){
try {
conn.close();
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}