public class HbaseTableTest {
private static Configuration conf;
static TableName tableName = TableName.valueOf("hbase_table");
static {
conf = HBaseConfiguration.create();
}
public static void createTable() {
Connection conn = null;
HBaseAdmin admin = null;
try {
conn = ConnectionFactory.createConnection(conf);
admin = (HBaseAdmin) conn.getAdmin();
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
HTableDescriptor descTable = new HTableDescriptor(tableName);
HColumnDescriptor family1 = new HColumnDescriptor("family1");
HColumnDescriptor family2 = new HColumnDescriptor("family2");
HColumnDescriptor family3 = new HColumnDescriptor("family3");
descTable.addFamily(family1);
descTable.addFamily(family2);
descTable.addFamily(family3);
admin.createTable(descTable);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeIO(conn);
IOUtils.closeIO(admin);
}
}
/**
* 插入单行数据
*/
public static void putSingleRow() {
Connection conn = null;
HTable table = null;
try {
conn = ConnectionFactory.createConnection(conf);
table = (HTable) conn.getTable(tableName);
Put put = new Put(Bytes.toBytes("20170610-1"));
put.addColumn(Bytes.toBytes("family1"), Bytes.toBytes("name"), Bytes.toBytes("zhangsan"));
put.addColumn(Bytes.toBytes("family1"), Bytes.toBytes("age"), Bytes.toBytes("12"));
put.addColumn(Bytes.toBytes("family2"), Bytes.toBytes("name"), Bytes.toBytes("lisi"));
put.addColumn(Bytes.toBytes("family3"), Bytes.toBytes("name"), Bytes.toBytes("wangwu"));
table.put(put);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeIO(conn);
IOUtils.closeIO(table);
}
}
/**
* 插入多行数据
*/
public static void putMoreRow() {
Connection conn = null;
HTable table = null;
try {
conn = ConnectionFactory.createConnection(conf);
table = (HTable) conn.getTable(tableName);
Put put1 = new Put(Bytes.toBytes("20170610-1"));
put1.addColumn(Bytes.toBytes("family1"), Bytes.toBytes("name"), Bytes.toBytes("zhangsan"));
put1.addColumn(Bytes.toBytes("family1"), Bytes.toBytes("age"), Bytes.toBytes("12"));
put1.addColumn(Bytes.toBytes("family2"), Bytes.toBytes("name"), Bytes.toBytes("lisi"));
put1.addColumn(Bytes.toBytes("family3"), Bytes.toBytes("name"), Bytes.toBytes("wangwu"));
Put put2 = new Put(Bytes.toBytes("20170610-2"));
put2.addColumn(Bytes.toBytes("family2"), Bytes.toBytes("age"), Bytes.toBytes("45"));
put2.addColumn(Bytes.toBytes("family3"), Bytes.toBytes("age"), Bytes.toBytes("52"));
List<Put> putList = new ArrayList<Put>();
putList.add(put1);
putList.add(put2);
table.put(putList);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeIO(conn);
IOUtils.closeIO(table);
}
}
/**
* 查询单行数据
*/
public static void querySingleRow() {
Connection conn = null;
HTable table = null;
try {
conn = ConnectionFactory.createConnection(conf);
table = (HTable) conn.getTable(tableName);
Get get = new Get(Bytes.toBytes("20170610-1"));
// get.addFamily(Bytes.toBytes("family1"));
Result result = table.get(get);
Cell[] rawCells = result.rawCells();
for (Cell cell : rawCells) {
System.out.println("row=" + new String(CellUtil.cloneRow(cell)) + " family="
+ new String(CellUtil.cloneFamily(cell)) + " Qualifier="
+ new String(CellUtil.cloneQualifier(cell)) + " Value="
+ new String(CellUtil.cloneValue(cell)));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeIO(conn);
IOUtils.closeIO(table);
}
}
/**
* 查询多行
*/
public static void queryMoreRow() {
Connection conn = null;
HTable table = null;
ResultScanner scanner = null;
try {
conn = ConnectionFactory.createConnection(conf);
table = (HTable) conn.getTable(tableName);
Scan scan = new Scan();
// scan.setStartRow(Bytes.toBytes("20170610-1"));
// scan.setStopRow(Bytes.toBytes("20170610-2"));
scan.setStopRow(Bytes.toBytes("20170610-z")); // z 表示扫描到最后
scanner = table.getScanner(scan);
for (Result result : scanner) {
Cell[] rawCells = result.rawCells();
for (Cell cell : rawCells) {
System.out.println("row=" + new String(CellUtil.cloneRow(cell)) + " family="
+ new String(CellUtil.cloneFamily(cell)) + " Qualifier="
+ new String(CellUtil.cloneQualifier(cell)) + " Value="
+ new String(CellUtil.cloneValue(cell))+" Timestamp="+cell.getTimestamp());
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeIO(scanner);
IOUtils.closeIO(conn);
IOUtils.closeIO(table);
}
}
/**
* 删除一行数据
*/
public static void deleteSingleRow() {
Connection conn = null;
HTable table = null;
try {
conn = ConnectionFactory.createConnection(conf);
table = (HTable) conn.getTable(tableName);
Delete delete = new Delete(Bytes.toBytes("20170610-2"));
table.delete(delete);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeIO(conn);
IOUtils.closeIO(table);
}
}
/**
* 删除一列数据
* 如果一个数据有多个版本,只会删除最新的
*/
public static void deleteQualifier() {
Connection conn = null;
HTable table = null;
try {
conn = ConnectionFactory.createConnection(conf);
table = (HTable) conn.getTable(tableName);
Delete delete = new Delete(Bytes.toBytes("20170610-1"));
delete.addColumn(Bytes.toBytes("family1"), Bytes.toBytes("age"));
table.delete(delete);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeIO(conn);
IOUtils.closeIO(table);
}
}
/**
* 删除列簇
*/
public static void deleteColumnFamily() {
Connection conn = null;
HBaseAdmin admin = null;
try {
conn = ConnectionFactory.createConnection(conf);
admin = (HBaseAdmin) conn.getAdmin();
admin.deleteColumn("hbase_table", "family3");
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeIO(conn);
IOUtils.closeIO(admin);
}
}
public static void deleteTable() {
Connection conn = null;
HBaseAdmin admin = null;
try {
conn = ConnectionFactory.createConnection(conf);
admin = (HBaseAdmin) conn.getAdmin();
admin.disableTable(tableName);
admin.deleteTable(tableName);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeIO(conn);
IOUtils.closeIO(admin);
}
}
public static void main(String[] args) {
// createTable();
// putSingleRow();
// putMoreRow();
// querySingleRow();
// deleteSingleRow();
//deleteQualifier();
// deleteColumnFamily();
// queryMoreRow();
deleteTable();
}
}
Hbase API 的简单使用
最新推荐文章于 2024-06-17 21:18:30 发布