1.配置文件和连接信息
// 两个变量的声明:
private static Connection connection = null;
private static Admin admin = null;
static{
try {
// 1.获取配置文件信息
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","vincen,vincen1,vincen2");
// 2.创建连接对象
connection = ConnectionFactory.createConnection(configuration);
// 3.创建admin对象
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
2.单独关闭资源
// 4.关闭资源
public static void close(){
if(admin != null){
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.获取数据(get)
// 6.获取数据(get)
public static void getData(String tableName,String rowKey,String cf,String cn) throws IOException {
// 6.1获取表对象
Table table = connection.getTable(TableName.valueOf(tableName));
// 6.2创建get对象
Get get = new Get(Bytes.toBytes(rowKey));
// 6.2.1指定获取的列族
get.addFamily(Bytes.toBytes(cf));
// 6.2.2指定列族和列
get.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn));
// 6.2.3设置获取数据的版本数
get.setMaxVersions(5);
// 6.3获取数据
Result result = table.get(get);
// 6.4解析 result 并打印
for (Cell cell : result.rawCells()){
// 6.5打印数据
System.out.println("CF:" + Bytes.toString(CellUtil.cloneFamily(cell)) +
", CN:" + Bytes.toString(CellUtil.cloneQualifier(cell)) +
",Value:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
// 6.6关闭表连接
table.close();
}
4.测试和关闭
public static void main(String[] args) throws IOException {
// 7.获取单行数据
getData("0408:stu1","1001","info2","name");
// 5.关闭资源的调用
close();
}
整体详情:
package test;
import com.sun.xml.internal.fastinfoset.stax.events.StAXEventAllocatorBase;
import javafx.scene.control.Tab;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class getData {
// 两个变量的声明:
private static Connection connection = null;
private static Admin admin = null;
static{
try {
// 1.获取配置文件信息
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","vincen,vincen1,vincen2");
// 2.创建连接对象
connection = ConnectionFactory.createConnection(configuration);
// 3.创建admin对象
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
// 6.获取数据(get)
public static void getData(String tableName,String rowKey,String cf,String cn) throws IOException {
// 6.1获取表对象
Table table = connection.getTable(TableName.valueOf(tableName));
// 6.2创建get对象
Get get = new Get(Bytes.toBytes(rowKey));
// 6.2.1指定获取的列族
get.addFamily(Bytes.toBytes(cf));
// 6.2.2指定列族和列
get.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn));
// 6.2.3设置获取数据的版本数
get.setMaxVersions(5);
// 6.3获取数据
Result result = table.get(get);
// 6.4解析 result 并打印
for (Cell cell : result.rawCells()){
// 6.5打印数据
System.out.println("CF:" + Bytes.toString(CellUtil.cloneFamily(cell)) +
", CN:" + Bytes.toString(CellUtil.cloneQualifier(cell)) +
",Value:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
// 6.6关闭表连接
table.close();
}
// 4.关闭资源
public static void close(){
if(admin != null){
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
// 7.获取单行数据
getData("0408:stu1","1001","info2","name");
// 5.关闭资源的调用
close();
}
}
HBase API 测试结果: