使用环境
hadoop2.8.3
hbase2.1.0
使用软件idea2018.3.3
配置
maven3.2.5
jdk1.8
1.使用idea创建一个maven项目
file>new>project>选择maven,选择自己的jdk版本,勾选create from archetype>next>填写groupid(com."一般为公司名"),artifactid(自己看着写)>next>finish
2.编写pom文件
pom文件写入后会自动下载,注意对应的hbase,hadoop版本。如果没有自动下载右键找到maven,刷新一下。依然不成功说明maven有问题。
version里是版本,改成自己对应的版本
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.8.3</version>
</dependency>
</dependencies>
3.resources需要的文件
hbase-site.xml(文件来自hbase)
log4j.properties(文件来自hadoop,可有可无,没有会报三行含有log4j的错误)
从虚拟机里下载下来复制到resources文件夹里即可
4.创建链接HbaseConnection
在src>java下建包>再建一个类
类名HbaseConnection
注意:确保自己的win已经改过hosts文件,需要添加自己虚拟机的主机名和ip。使用win的控制台"ping主机名"能ping通,避免造成不必要的麻烦。
方法一
1.以下是连接虚拟机里的Hadoop,base的方法
此方法必须第3步的hbase-site.xml
public static Connection connection = null;
static{
try {
connection = ConnectionFactory.createConnection();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void closeConnetcion() throws IOException {
if (connection != null){
connection.close();
}
}
2.在写一个main来调用以上方法
public static void main(String[] args) throws IOException {
//调用方法
System.out.println(HbaseConnection.connection);
//关闭连接
HbaseConnection.closeConnetcion();
}
3.需要的import
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;
方法二
conf.set("hbase.zookeeper.quorum","node1:2181");里需要填入自己的虚拟机配置
此配置在hbase-site.xml文件里,内容如下
<property> <name>hbase.zookeeper.quorum</name> <value>node1:2181</value> </property>
public static void main(String[] args) throws IOException {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum","node1:2181");
Connection connection = ConnectionFactory.createConnection(conf);
CompletableFuture<AsyncConnection> asyncConnection = ConnectionFactory.createAsyncConnection(conf);
System.out.println(connection);
connection.close();
5.创建一个新的类HbaseDDL
修改表格
1.获取连接
public static Connection connection = HbaseConnection.connection;
2.创建命名空间
public static void createNamespace(String namespace) throws IOException {
Admin admin = connection.getAdmin();
NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(namespace);
// builder.addConfiguration("user", "cat");
admin.createNamespace(builder.build());
admin.close();
try {
admin.createNamespace(builder.build());
System.out.println("name" + namespace + "成功");
} catch (Exception e) {
System.out.println("命令空间已经存在");
e.printStackTrace();
} finally {
admin.close();
}
}
3.判断表是否存在
public static boolean isTableExits(String namespace, String tableName) throws IOException {
//获取admin
Admin admin = connection.getAdmin();
boolean b = false;
try {
b = admin.tableExists(TableName.valueOf(namespace, tableName));
} catch (IOException e) {
e.printStackTrace();
}
admin.close();
return b;
}
4.创建表,以及多个列族。此方法调用量3的判断表格是否存在。
public static void createTable(String namespace ,String tableName , String... columnFamilies ) throws IOException {
if (columnFamilies.length == 0){
System.out.println("创建表格至少有一个列族");
return;
}
if (isTableExits(namespace,tableName)){
System.out.println("表格已经存在");
}
Admin admin = connection.getAdmin();
//创建表格描述的建造者
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(namespace,tableName));
//
for (String columnFamily : columnFamilies) {
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamily));
columnFamilyDescriptorBuilder.setMaxVersions(3);
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
}
try {
admin.createTable(tableDescriptorBuilder.build());
} catch (IOException e) {
e.printStackTrace();
}
admin.close();
}
5.修改列族和版本
public static void modifytable(String namespace ,String tableName , String columnFamiliy , int version) throws IOException {
//判断表格是否存在
if (!isTableExits(namespace,tableName)){
System.out.println("表格不存在");
return;
}
//获取admin
Admin admin = connection.getAdmin();
//获取之前的表格描述
TableDescriptor descriptor = admin.getDescriptor(TableName.valueOf(namespace, tableName));
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(descriptor);
ColumnFamilyDescriptor columnFamily = descriptor.getColumnFamily(Bytes.toBytes(columnFamiliy));
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(columnFamily);
//修改对应的版本
columnFamilyDescriptorBuilder.setMaxVersions(version);
tableDescriptorBuilder.modifyColumnFamily(columnFamilyDescriptorBuilder.build());
try {
admin.modifyTable(tableDescriptorBuilder.build());
} catch (IOException e) {
e.printStackTrace();
}
admin.close();
}
6.删除表
public static boolean deleteTable(String namepace , String tableName) throws IOException {
if (!isTableExits(namepace,tableName)){
System.out.println("表格不存在,无法删除");
return false;
}
Admin admin = connection.getAdmin();
try {
//删除前先标记为不可用
admin.disableTable(TableName.valueOf(namepace, tableName));
admin.deleteTable(TableName.valueOf(namepace, tableName));
} catch (IOException e) {
e.printStackTrace();
}
admin.close();
return true;
}
7.写一个main方法调用以上方法
以下为一个例子,其他调用直接写入此main方法即可
public static void main(String[] args) throws IOException {
// 创建命名空间
createNamespace("cat");
//关闭连接
HbaseConnection.closeConnetcion();
}
8.需要import
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
6.创建一个新的类HbaseDML
修改数据
1.同样是获取连接
public static Connection connection = HbaseConnection.connection;
2.插入数据
public static void putCell(String namespace,String tableName,String rowKey,String columFamily,String columnName,String value) throws IOException {
//获取table
Table table = connection.getTable(TableName.valueOf(namespace, tableName));
//调用
Put put = new Put(Bytes.toBytes(rowKey));
//给put对象添加属性
put.addColumn(Bytes.toBytes(columFamily),Bytes.toBytes(columnName),Bytes.toBytes(value));
try {
table.put(put);
} catch (IOException e) {
e.printStackTrace();
}
table.close();
}
3.读取数据,精确查找
public static void getCell(String namespace,String tableName,String rowKey,String columFamily,String columnName) throws IOException {
Table table = connection.getTable(TableName.valueOf(namespace, tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(columFamily),Bytes.toBytes(columnName));
get.readAllVersions();
try {
Result 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();
}
table.close();
}
4.扫描,不带过滤
public static void scanRows(String namespace,String tableName,String startRow,String stopRow) throws IOException {
Table table = connection.getTable(TableName.valueOf(namespace, tableName));
Scan scan = new Scan();
scan.withStartRow(Bytes.toBytes(startRow));
scan.withStopRow(Bytes.toBytes(stopRow));
try {
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.print(new String(CellUtil.cloneRow(cell)) + "-" + new String(CellUtil.cloneFamily(cell)) + "-" +
new String(CellUtil.cloneQualifier(cell)) + "-" + new String(CellUtil.cloneValue(cell)) + "\t");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
table.close();
}
5.扫描,带过滤器
注意:方法中包含两个过滤器,可以在
filterList.addFilter(columnValueFilter);
这行代码中更改所使用的过滤器,
当前使用过滤器为columnValueFilter
public static void filterRows(String namespace,String tableName,String startRow,String stopRow, String coumnFamily,String coumnName,String value) throws IOException {
Table table = connection.getTable(TableName.valueOf(namespace, tableName));
Scan scan = new Scan();
scan.withStartRow(Bytes.toBytes(startRow));
scan.withStopRow(Bytes.toBytes(stopRow));
FilterList filterList = new FilterList();
//结果只保留当前列,过滤器
ColumnValueFilter columnValueFilter = new ColumnValueFilter(
//列族名
Bytes.toBytes(coumnFamily),
//列名
Bytes.toBytes(coumnName),
//比较关系
CompareOperator.EQUAL,
//值
Bytes.toBytes(value)
);
//结果只保留整行数据
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
Bytes.toBytes(coumnFamily),
//列名
Bytes.toBytes(coumnName),
//比较关系
CompareOperator.EQUAL,
//值
Bytes.toBytes(value)
);
filterList.addFilter(columnValueFilter);
//添加过滤器
scan.setFilter(filterList);
try {
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.print(new String(CellUtil.cloneRow(cell)) + "-" + new String(CellUtil.cloneFamily(cell)) + "-" +
new String(CellUtil.cloneQualifier(cell)) + "-" + new String(CellUtil.cloneValue(cell)) + "\t");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
table.close();
}
6.删除数据
public static void deleteColumn(String namespace,String tableName,String rawKey, String coumnFamily,String coumnName) throws IOException {
Table table = connection.getTable(TableName.valueOf(namespace, tableName));
//创建delete对象
Delete delete = new Delete(Bytes.toBytes(rawKey));
//删除一个版本
// delete.addColumn(Bytes.toBytes(coumnFamily),Bytes.toBytes(coumnName));
//删除所有版本
delete.addColumns(Bytes.toBytes(coumnFamily),Bytes.toBytes(coumnName));
try {
table.delete(delete);
} catch (IOException e) {
e.printStackTrace();
}
table.close();
}
6.使用main方法调用以上方法
以下为其中一个例子
public static void main(String[] args) throws IOException {
//插入数据
putCell("cat","llll","10010","info","name","zhangsan");
//关闭连接
HbaseConnection.closeConnetcion();
}
7.需要的import
有两行是多余的,变成灰色自行删除即可。
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.ColumnCountGetFilter;
import org.apache.hadoop.hbase.filter.ColumnValueFilter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;