在pom文件中加载依赖
<dependency><!-- HBase 客户端依赖包 -->
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.4.6</version>
</dependency>
<dependency><!-- HBase 公共依赖包 -->
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>1.4.6</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.4.6</version>
</dependency>
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.util.ArrayList;
import java.util.List;
public class HbaseUtils {
private static Connection conn;
private static Configuration configuration;
private static Admin admin;
public static void main(String[] args) throws Exception {
}
@Before
public void setup() throws Exception {
configuration = new Configuration();
configuration.set("hbase.zookeeper.quorum", "master:2181");
conn = ConnectionFactory.createConnection(configuration);
admin = conn.getAdmin();
}
@After
public void End_up() throws Exception {
if (conn != null) conn.close();
}
@Test
public void create() throws IOException {
String[] cols = {"phone", "addr", "age"};
createTable("helloworld", cols);
}
void createTable(String tableName, String[] columns) throws IOException {
TableName tb = TableName.valueOf(tableName);
if (admin.tableExists(tb)) {
System.out.println("已经存在");
} else {
System.out.println("不存在");
HTableDescriptor desc = new HTableDescriptor(tb);
for (String column : columns) {
desc.addFamily(new HColumnDescriptor(column));
}
admin.createTable(desc);
System.out.println("create " + tableName + " success");
}
}
@Test
public void delete() throws IOException {
deleteTable("tab1");
}
void deleteTable(String tableName) throws IOException {
TableName tb = TableName.valueOf(tableName);
if (admin.tableExists(tb)) {
System.out.println("已经存在");
admin.disableTable(tb);
admin.deleteTable(tb);
}
System.out.println("delete " + tableName + " success");
}
@Test
public void showTable() throws IOException {
TableName[] tables = admin.listTableNames();
for (TableName tab : tables) {
System.out.println("表名:" + tab.getNameAsString());
HTableDescriptor desc = admin.getTableDescriptor(tab);
for (HColumnDescriptor column : desc.getColumnFamilies()) {
System.out.println(column.getNameAsString() + "===" + column.toString());
}
}
}
@Test
public void getByRowKey(String tablename, String row) throws IOException {
Table table = conn.getTable(TableName.valueOf(tablename));
Get get = new Get(row.getBytes());
Result rt = table.get(get);
System.out.println(rt.toString());
for (Cell cell : rt.rawCells()) {
System.out.println(
Bytes.toString(CellUtil.cloneRow(cell)) + "\t" +
Bytes.toString(CellUtil.cloneFamily(cell)) + "\t" +
Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t" +
Bytes.toString(CellUtil.cloneValue(cell))
);
}
}
@Test
public void put(String tablename, String row, String cloums, String[] cloum, String[] value) throws IOException {
Table tab = conn.getTable(TableName.valueOf(tablename));
Put put1 = new Put(row.getBytes());
put1.addColumn(cloums.getBytes(), cloum[0].getBytes(), Bytes.toBytes(value[0]));
put1.addColumn(cloums.getBytes(), cloum[1].getBytes(), Bytes.toBytes(value[1]));
List list = new ArrayList();
list.add(put1);
tab.put(list);
System.out.println("上传到hbase表成功");
}
@Test
public void deletByRowKey(String tablename, String row) throws IOException {
Table table = conn.getTable(TableName.valueOf(tablename));
Delete delete = new Delete(Bytes.toBytes(row));
table.delete(delete);
System.out.println("删除" + tablename + "的" + row + "成功!");
table.close();
}
@Test
public void scanTable(String tablename) throws IOException {
Table table = conn.getTable(TableName.valueOf(tablename));
Scan scan = new Scan();
ResultScanner rtScan = table.getScanner(scan);
for (Result next = rtScan.next(); next != null; next = rtScan.next()) {
System.out.println(next.toString());
}
}
}
Hbase测试类
public class HbaseDemo {
public static void main(String[] args) throws Exception {
HbaseUtils demo = new HbaseUtils();
demo.setup();
System.out.println("\n\n\n=======================================delete table==================");
String[] cols = {"info"};
demo.deleteTable("tab1");
System.out.println("\n\n\n=======================================create table==================");
demo.createTable("tab1",cols);
System.out.println("\n\n\n=======================================show tables==================");
demo.showTable();
System.out.println("\n\n\n=======================================put==================");
String cloums = "info";
String[] cloum = {"age","name"};
String[] values = {"24","lyt"};
demo.put("tab1","1001",cloums,cloum,values);
demo.put("tab1","1002",cloums,cloum,values);
System.out.println("\n\n\n=======================================getByRowKey==================");
demo.getByRowKey("tab1","1001");
System.out.println("\n\n\n=======================================deletByRowKey==================");
demo.deletByRowKey("tab1","1001");
System.out.println("\n\n\n=======================================scan tables===============================");
demo.scanTable("tab1");
demo.End_up();
}
}
文章参考链接
链接: java操作hbase1.4_JAVA API操作hbase1.4.2