这里注意一定要用JDK 1.8 否则会出现不兼容,报错的问题。
java api 代码4.0.0 org.example HbaeDemo 1.0-SNAPSHOT org.apache.hbase hbase-client 1.3.1 org.apache.hbase hbase-server 1.3.1 org.apache.hbase hbase-common 1.3.1 commons-logging commons-logging 1.2 log4j log4j 1.2.17
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 java.io.IOException;
public class HbaseDemo {
private static Admin admin = null;
private static Connection connection = null;
//创建链接
static {
Configuration configuration = HbaseConfiguration.create();
//配置zookeeper的地址
configuration.set("hbase.zookeeper.quorum", "这里填写的是zookeeper集群的地址");
try {
//通过工厂类获取管理员
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
private void close(Connection conn, Admin admin) {
if (conn != null) {
try {
conn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (admin != null) {
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void createTable(String tableName, String... cfs) throws IOException {
if (ExistTable(tableName)) {
System.out.println("表名已经存在!");
return;
}
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
for (String cf : cfs) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println("表名创建成功!");
}
public static boolean ExistTable(String tableName) throws IOException {
boolean exists = admin.tableExists(TableName.valueOf(tableName));
return exists;
}
public static void deleteTable(String tableName) throws IOException {
if (!ExistTable(tableName)) {
System.out.println("表不存在!");
return;
}
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
System.out.println("表已经被删除!");
}
public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
table.put(put);
table.close();
System.out.println("添加成功!");
}
public static void deleteData(String tableName, String rowKey, String cf, String cn) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));
table.delete(delete);
table.close();
System.out.println("删除成功!");
}
public static void scanTable(String tableName) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner results = table.getScanner(scan);
for (Result result : results) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("RK" + Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("CF" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("CN" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("VALUE" + Bytes.toString(CellUtil.clonevalue(cell)));
}
}
}
public static void getData(String tableName, String rowKey) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get);
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println(Bytes.toString(CellUtil.clonevalue(cell)));
}
}
public static void main(String[] args) {
try {
scanTable("ecdefault:EmailLog");
} catch (IOException e) {
e.printStackTrace();
}
}
}



