目录
引入的依赖
创建连接
命名空间
表
引入的依赖
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.4.11</version>
<exclusions>
<exclusion>
<artifactId>javax.el</artifactId>
<groupId>org.glassfish</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.1-b06</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>2.4.11</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>2.4.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.10.0</version>
</dependency>
</dependencies>
创建连接
单线程使用连接
conf.set的配置信息具体跟本地hosts文件中的配置有关
public class HBaseConnection {
public static void main(String[] args) throws IOException {
//1.创建连接配置对象
Configuration conf = new Configuration();
//2.添加配置参数 zookeeper地址
conf.set("hbase.zookeeper.quorum","node1,node2,node3");
//3.创建连接,默认使用同步连接
Connection connection = ConnectionFactory.createConnection(conf);
//也可以使用异步连接
//CompletableFuture<AsyncConnection> asyncConnection = ConnectionFactory.createAsyncConnection();
//4.使用连接
System.out.println(connection);
//5.关闭连接
connection.close();
}
}
在创建连接时,由于该连接是重量级的,创建时最好创建一个,所以当多个线程使用连接时,需要只创建一个连接,采用单例的模式
多线程使用连接
public class HBaseConnection2 {
public static Connection connection = null;
static {
//1.创建连接配置对象
Configuration conf = new Configuration();
//2.添加配置参数 zookeeper地址
conf.set("hbase.zookeeper.quorum","node1,node2,node3");
//3.创建连接,默认使用同步连接
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void closeConnection(){
if(connection != null) {
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//4.使用连接
System.out.println(HBaseConnection2.connection);
HBaseConnection2.closeConnection();
}
}
命名空间
public class HBaseDDL {
public static Connection connection = HBaseConnection2.connection;
public static void createNameSpace(String namespace) throws IOException {
//1.获取admin,admin是轻量级,不是线程安全的,不推荐池化或缓存这个连接
//连接出现问题不属于createNameSpace的问题,应该抛出
Admin admin = connection.getAdmin();
//2.调用方法,创建命名空间
//2.1 创建命名空间描述,利用了建造者模式,拓展丰富了构造器
NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(namespace);
//2.2 给命名空间添加需求,就是命名空间的描述
builder.addConfiguration("user","zz");
//2.3 使用builder构造出对应的添加完参数的对象 完成创建
//创建命名空间出现的问题,都属于本方法自身的问题,不应该抛出
try {
admin.createNamespace(builder.build());
} catch (IOException e) {
System.out.println("命名空间已经存在");
e.printStackTrace();
}
//3关闭admin
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
HBaseDDL.createNameSpace("company");
HBaseConnection2.closeConnection();
}
}
表
判断表是否存在
//判断表格是否存在
public static boolean isTableExists(String namespace,String tableName) throws IOException {
//1.获取admin 连接的异常不属于该方法所以抛出
Admin admin = connection.getAdmin();
//2.使用方法判断表格是否存在,该异常是属于该方法,所以try起来
boolean b = false;
try {
b = admin.tableExists(TableName.valueOf(namespace,tableName));
} catch (IOException e) {
e.printStackTrace();
}
//3.关闭admin
admin.close();
//4.返回结果
return b;
}
public static void main(String[] args) throws IOException {
System.out.println(isTableExists("bigdata","student"));
}
创建表
//创建表格
public static void createTable(String namespace,String tableName,String... columnFamilys) throws IOException {
//判断是否至少有一个列族
if(columnFamilys.length == 0){
System.out.println("创建表格至少有一个列族");
return;
}
//判断表格是否存在
if(isTableExists(namespace,tableName)){
System.out.println("表格已经存在");
return;
}
//1.获取admin
Admin admin = connection.getAdmin();
//2.调用方法创建表格
//2.1创建表格描述
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(namespace,tableName));
//2.2添加参数
for(String columnFamily : columnFamilys){
//2.3创建列族描述的建造者
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder
= ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamily));
//2.4对应当前列族添加参数
columnFamilyDescriptorBuilder.setMaxVersions(5);
//2.5创建添加完参数的列族描述
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
}
//2.6创建表格
try {
admin.createTable(tableDescriptorBuilder.build());
} catch (IOException e) {
System.out.println("表格已经存在");
e.printStackTrace();
}
//3.关闭admin
admin.close();
}
public static void main(String[] args) throws IOException {
createTable("company","worker","info","msg");
}
修改表格
public static void modifyTable(String namespace,String tableName,String columnFamily,int version) throws IOException {
//判断表格是否存在
if(!isTableExists(namespace,tableName)){
System.out.println("表格不存在,无法修改");
return;
}
//1.获取admin
Admin admin = connection.getAdmin();
try {
//2.调用方法修改表格
//2.1获取之前的表格描述 :先获取描述,再获取建造者,再根据建造者获取表格对象
TableDescriptor tableDescriptor = admin.getDescriptor(TableName.valueOf(namespace, tableName));
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableDescriptor);
//2.2获取旧的列族描述
ColumnFamilyDescriptor columnFamilyDescriptor = tableDescriptor
.getColumnFamily(Bytes.toBytes(columnFamily));
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder
.newBuilder(columnFamilyDescriptor);
//修改对应信息
columnFamilyDescriptorBuilder.setMaxVersions(version);
//将修改后的列族赋给旧的表格
tableDescriptorBuilder.modifyColumnFamily(columnFamilyDescriptorBuilder.build());
//3.修改版本数
admin.modifyTable(tableDescriptorBuilder.build());
} catch (IOException e) {
e.printStackTrace();
}
//4.关闭admin
admin.close();
}
public static void main(String[] args) throws IOException {
modifyTable("company","worker","info",6);
}
删除表格
public static boolean deleteTable(String namespace,String tableName) throws IOException {
//1.判断表格是否存在
if(!isTableExists(namespace,tableName)){
System.out.println("表格不存在,无法删除");
return false;
}
//2.获取admin
Admin admin = connection.getAdmin();
//3.调用相关方法
try {
//删除表格之前,先标记表格为不可用
TableName tableName1 = TableName.valueOf(namespace, tableName);
admin.disableTable(tableName1);
admin.deleteTable(tableName1);
} catch (IOException e) {
e.printStackTrace();
}
//4.关闭admin
admin.close();
return true;
}
数据
写入数据
public class HBaseDML {
public static Connection connection = HBaseConnection2.connection;
public static void putCell(String namespace,String tableName,
String rowKey,String columnFamily,
String columnName,String value) throws IOException {
//1.获取Table
Table table = connection.getTable(TableName.valueOf(namespace, tableName));
//2.调用相关方法插入数据
//2.1创建put对象
Put put = new Put(Bytes.toBytes(rowKey));
//2.2给put对象添加数据
put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(columnName),Bytes.toBytes(value));
//2.3将对象写入方法
try {
table.put(put);
} catch (IOException e) {
e.printStackTrace();
}
//3.关闭table
table.close();
}
public static void main(String[] args) throws IOException {
putCell("bigdata","student","1005","info","age","100");
HBaseConnection2.closeConnection();
}
读取数据
get
用get读取数据只能读取一行
public static void getCells(String namespace,String tableName,
String rowKey,String columnFamily,
String columnName) throws IOException {
//1.获取table
Table table = connection.getTable(TableName.valueOf(namespace,tableName));
//2.创建get对象
Get get = new Get(Bytes.toBytes(rowKey));
//3.如果直接调用get方法读取数据 此时读取一整行数据
//如果读取某几列需要增加参数
get.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(columnName));
//设置读取的版本
get.readAllVersions();
Result result = null;
try {
//4.获取并处理数据
result = table.get(get);
Cell[] cells = result.rawCells();
for(Cell cell:cells){
String value = new String(CellUtil.cloneValue(cell));
System.out.println(value);
}
} catch (IOException e) {
e.printStackTrace();
}
//5.关闭table
table.close();
}
public static void main(String[] args) throws IOException {
getCells("bigdata","student","1005","info","age");
HBaseConnection2.closeConnection();
}
scan
public static void scanRows(String namespace,String tableName,String startRow,String stopRow) throws IOException {
//1.获取table
Table table = connection.getTable(TableName.valueOf(namespace,tableName));
//2.创建scan对象
Scan scan = new Scan();
//如果不写这一句就扫描整张表
scan.withStartRow(Bytes.toBytes(startRow)).withStopRow(Bytes.toBytes(stopRow));
//3.读取多行数据,Result记录一行数据,ResultScanner记录多行数据,Result是cell的数组,ResultScanner是Result的数据
ResultScanner resultScanner = null;
try {
resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
Cell[] cells = result.rawCells();
for(Cell cell:cells){
System.out.println(new String(CellUtil.cloneRow(cell))+"-"
+new String(CellUtil.cloneFamily(cell))+"-"
+new String(CellUtil.cloneQualifier(cell))+"-"
+new String(CellUtil.cloneValue(cell)));
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
//4.关闭table
table.close();
}
public static void main(String[] args) throws IOException {
scanRows("bigdata","student","1001","1004");
HBaseConnection2.closeConnection();
}
scan之单列过滤扫描
输出多行数据的某一列,并且这些行中可以按值去筛选,选中从startRow到stopRow的所有行的columnName的列的内容,再根据value进行筛选
public static void filterScan(String namespace,String tableName,
String startRow,String stopRow,
String columnFamily,String columnName,
String value) throws IOException {
//1.获取table
Table table = connection.getTable(TableName.valueOf(namespace,tableName));
//2.创建scan对象
Scan scan = new Scan();
//如果不写这一句就扫描整张表
scan.withStartRow(Bytes.toBytes(startRow)).withStopRow(Bytes.toBytes(stopRow));
//可以添加多个过滤器
FilterList filterList = new FilterList();
//创建过滤器
//(1)结果只保留当前列的数据
ColumnValueFilter columnValueFilter = new ColumnValueFilter(
//列族名称
Bytes.toBytes(columnFamily),
//列名
Bytes.toBytes(columnName),
//比较关系
CompareOperator.EQUAL,
//值
Bytes.toBytes(value)
);
filterList.addFilter(columnValueFilter);
scan.setFilter(filterList);
//3.读取多行数据,Result记录一行数据,ResultScanner记录多行数据,Result是cell的数组,ResultScanner是Result的数据
ResultScanner resultScanner = null;
try {
resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
Cell[] cells = result.rawCells();
for(Cell cell:cells){
System.out.println(new String(CellUtil.cloneRow(cell))+"-"
+new String(CellUtil.cloneFamily(cell))+"-"
+new String(CellUtil.cloneQualifier(cell))+"-"
+new String(CellUtil.cloneValue(cell)));
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
//4.关闭table
table.close();
}
public static void main(String[] args) throws IOException {
filterScan("bigdata","student",
"1001","1005",
"info","name",
"wangwu");
HBaseConnection2.closeConnection();
}
scanf之整行过滤扫描
跟上面过滤的区别是:假设某行数据没有我们指定的columnFamily和columnName,那么该行数据也会作为结果保留下来
public static void filterScan(String namespace,String tableName,
String startRow,String stopRow,
String columnFamily,String columnName,
String value) throws IOException {
//1.获取table
Table table = connection.getTable(TableName.valueOf(namespace,tableName));
//2.创建scan对象
Scan scan = new Scan();
//如果不写这一句就扫描整张表
scan.withStartRow(Bytes.toBytes(startRow)).withStopRow(Bytes.toBytes(stopRow));
//可以添加多个过滤器
FilterList filterList = new FilterList();
//创建过滤器
//(1)结果只保留当前列的数据
ColumnValueFilter columnValueFilter = new ColumnValueFilter(
//列族名称
Bytes.toBytes(columnFamily),
//列名
Bytes.toBytes(columnName),
//比较关系
CompareOperator.EQUAL,
//值
Bytes.toBytes(value)
);
//(2)结果保留整行数据
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
//列族名称
Bytes.toBytes(columnFamily),
//列名
Bytes.toBytes(columnName),
//比较关系
CompareOperator.EQUAL,
//值
Bytes.toBytes(value)
);
filterList.addFilter(singleColumnValueFilter);
scan.setFilter(filterList);
//3.读取多行数据,Result记录一行数据,ResultScanner记录多行数据,Result是cell的数组,ResultScanner是Result的数据
ResultScanner resultScanner = null;
try {
resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
Cell[] cells = result.rawCells();
for(Cell cell:cells){
System.out.println(new String(CellUtil.cloneRow(cell))+"-"
+new String(CellUtil.cloneFamily(cell))+"-"
+new String(CellUtil.cloneQualifier(cell))+"-"
+new String(CellUtil.cloneValue(cell)));
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
//4.关闭table
table.close();
}
public static void main(String[] args) throws IOException {
filterScan("bigdata","student",
"1001","1005",
"info","name",
"wangwu");
HBaseConnection2.closeConnection();
}
删除数据
删除某一列数据
public static void deleteColumn(String namespace,String tableName,
String rowKey,String columnFamily,
String columnName) throws IOException {
//1.获取table
Table table = connection.getTable(TableName.valueOf(namespace,tableName));
//2.获取delete对象
Delete delete = new Delete(Bytes.toBytes(rowKey));
//添加列信息,删除一个版本
//delete.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(columnName));
//删除所有版本
delete.addColumns(Bytes.toBytes(columnFamily),Bytes.toBytes(columnName));
try {
table.delete(delete);
} catch (IOException e) {
e.printStackTrace();
}
//3.关闭table
table.close();
}
public static void main(String[] args) throws IOException {
deleteColumn("bigdata","student","1002","info","name");
HBaseConnection2.closeConnection();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)