1、准备工作
1.1 pom文件
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.1</version>
</dependency>
1.2 Bean对象
class Student {
private String id;
private String name;
private int age;
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
2. 创建连接对象
public static Connection conn;
static {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.191.128");
conf.set("hbase.zookeeper.property.clientPort", "2181");
try {
conn = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
3. 判断表是否存在
public static boolean isTableExist(String tableName) throws IOException {
HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();
return admin.tableExists(tableName);
}
4. 创建表
public static void createTable(String tableName, String... columnFamily) throws IOException {
HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();
if (isTableExist(tableName)) {
System.out.println("表" + tableName + "已存在!");
} else {
HTableDescriptor htableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
for (String cf : columnFamily) {
htableDescriptor.addFamily(new HColumnDescriptor(cf));
}
admin.createTable(htableDescriptor);
System.out.println("表" + tableName + "创建成功!");
}
}
5. 删除表
public static void dropTable(String tableName) throws IOException {
HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();
if (isTableExist(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println("表" + tableName + "已删除!");
} else {
System.out.println("表" + tableName + "不存在!");
}
}
6. 添加单行数据
public static void addRowData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException {
Table table = conn.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);
table.close();
System.out.println("插入数据成功!");
}
7. 添加多行数据
public static void addbatchData(String tableName, String columnFamily, JSONArray jsonArray)
throws IOException {
Table table = conn.getTable(TableName.valueOf(tableName));
List<Put> putList = new ArrayList<Put>();
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = JSON.parseObject(jsonArray.get(i).toString());
Put put = new Put(Bytes.toBytes(jsonObject.getString("id")));
String name = jsonObject.getString("name");
String age = jsonObject.getString("age");
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("name"), Bytes.toBytes(name));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("age"), Bytes.toBytes(age));
putList.add(put);
}
table.put(putList);
table.close();
}
8. 删除多行数据
public static void deleteMutilRow(String tableName, String...rows) throws IOException {
Table table = conn.getTable(TableName.valueOf(tableName));
List<Delete> deleteList = new ArrayList<Delete>();
for (String row:rows){
Delete delete = new Delete(Bytes.toBytes(row));
deleteList.add(delete);
}
table.delete(deleteList);
table.close();
}
9. 获取多行数据
public static void getAllRows(String tableName) throws IOException {
Table table = conn.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner resultScanner = table.getScanner(scan);
for(Result result:resultScanner){
Cell[] cells = result.rawCells();
for(Cell cell:cells){
System.out.println("行键:"+Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("列族:"+Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列:"+Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:"+Bytes.toString(CellUtil.cloneValue(cell))+"\n");
}
}
}
10. 获取某一行数据
public static void getRow(String tableName, String rowkey) throws IOException {
Table table = conn.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowkey));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println("行键:" + Bytes.toString(result.getRow()));
System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)) + "\n");
}
}
11. 获取某一行“列族:列”的数据
public static void getRowQualifier(String tableName, String rowkey
, String family, String qualifier) throws IOException {
Table table = conn.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowkey));
get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println("行键:" + Bytes.toString(result.getRow()));
System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}