java客户端对hbase进行建表、增删改数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InterruptedIOException;
public class HbaseDemo {
private HBaseAdmin admin = null;
private HTable table = null;
private String tableName = "tb_user";
@Before
public void init() throws IOException {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.1.201,192.168.1.202,192.168.1.203");
admin = new HBaseAdmin(conf);
table = new HTable(conf, TableName.valueOf(tableName));
}
@After
public void destroy() throws IOException {
if (null != table) {
table.close();
}
if (null != admin) {
admin.close();
}
}
@Test
public void createTable() throws IOException {
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("cf".getBytes());
hTableDescriptor.addFamily(hColumnDescriptor);
admin.createTable(hTableDescriptor);
}
@Test
public void testInsert() throws InterruptedIOException, RetriesExhaustedWithDetailsException {
Put put = new Put("1001".getBytes());
put.add("cf".getBytes(), "name".getBytes(), "zhangsan".getBytes());
put.add("cf".getBytes(), "age".getBytes(), Bytes.toBytes(26));
put.add("cf".getBytes(), "address".getBytes(), "shanghai".getBytes());
table.put(put);
put = new Put("1002".getBytes());
put.add("cf".getBytes(), "name".getBytes(), "lisi".getBytes());
put.add("cf".getBytes(), "age".getBytes(), Bytes.toBytes(25));
put.add("cf".getBytes(), "likes".getBytes(), "reading".getBytes());
table.put(put);
}
@Test
public void testScan() throws IOException {
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
while (scanner.next() != null) {
printMsg(scanner.next());
}
scanner.close();
}
@Test
public void testGet() throws IOException {
Get get = new Get("1001".getBytes());
Result result = table.get(get);
printMsg(result);
}
public void printMsg(Result result) {
Cell namecell = result.getColumnLatestCell("cf".getBytes(), "name".getBytes());
Cell agecell = result.getColumnLatestCell("cf".getBytes(), "age".getBytes());
Cell addcell = result.getColumnLatestCell("cf".getBytes(), "address".getBytes());
Cell likescell = result.getColumnLatestCell("cf".getBytes(), "likes".getBytes());
System.out.println(Bytes.toString(CellUtil.cloneValue(namecell)));
System.out.println(Bytes.toString(likescell == null ? new byte[]{} : CellUtil.cloneValue(likescell)));
System.out.println(Bytes.toString(addcell == null ? new byte[]{} : CellUtil.cloneValue(addcell)));
System.out.println(Bytes.toInt(CellUtil.cloneValue(agecell)));
}
@Test
public void testAddColumnFamily() throws IOException {
HColumnDescriptor columnDescriptor = new HColumnDescriptor("cf2".getBytes());
columnDescriptor.setMaxVersions(3);
admin.addColumn(tableName,columnDescriptor);
}
@Test
public void testDelete() throws IOException {
Delete delete = new Delete("1002".getBytes());
table.delete(delete);
}
}