版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38256924/article/details/79848232
相信大家在开发大数据平台的时候都涉及到数据的导入导出,而hbase作为hadoop上的一个数据库,如何连接和操作则成为了不得不学的一个东西,那么今天这篇文章就教大家如何操作hbase数据库。
1.新建工程
1.新建一个普通的java工程:
2.在工程文件目录下创建一个lib文件夹,导入相对应的jar文件,并通过build-path导入
具体需要的jar如下:
如需要下载可在下面链接:
hbase-1.1.2-bin.tar.gz
3.在工程文件目录下创建一个conf文件夹(需通过buildpath注入)并导入hbase的配置文件,这个配置文件需在hbase的conf目录下寻找:
做到这里基本的工程创建就完成了,现在需要下一步了哦
2.配置hosts文件
在你java工程所在电脑配置你的hadoop集群信息,所以需要找到并修改hosts文件:
(因为hadoop的分布式hbase可能存储在一个slaver里,所有要先确定那个是hbase的安装机,或者全部导入,我就是哈哈)
hosts的目录:
C:\Windows\System32\drivers\etc
写入集群信息:
需要注意的是,我这里已经默认你配置好集群,配置好后其他集群机器的hosts文件应该也含有这些内容
3.编写java代码,这里有各种方法,欢迎使用交流
代码
package hbase;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.exceptions.DeserializationException;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos.DeleteColumnFamilyState;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.htrace.fasterxml.jackson.databind.Module.SetupContext;
import org.junit.Before;
import org.junit.Test;
import com.sun.java_cup.internal.runtime.Symbol;
public class HBaseDemo {
public static Connection connection;
public static Admin admin;
public static void main(String[] args) {
try {
System.out.println("开始执行本程序");
System.out.println("HBaseDemo.main()->admin1:" + admin);
setUp();
createTable("helloWorld");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void setUp() throws Exception {
Configuration conf =HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "master2");
conf.set("hbase.zookeeper.property.clientPort", "2181");
connection=ConnectionFactory.createConnection(conf);
admin=connection.getAdmin();
System.out.println("HBaseDemo.setUp()->admin:" + admin);
}
/**
* 创建表的方法,输入参数为表名
* @param tabelNameString
* @throws IOException
*/
public static void createTable(String tableNameString) throws IOException {
System.out.println("-------------------创建表开始了哦------------------------");
TableName tableName=TableName.valueOf(tableNameString);
System.out.println("HBaseDemo.createTable()->tabelName:" + tableName);
if(admin.tableExists(tableName)) {
System.out.println("表已经存在!");
}
else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
System.out.println("HBaseDemo.createTable()->hTableDescriptor:" + hTableDescriptor);
HColumnDescriptor family = new HColumnDescriptor("base");
System.out.println("HBaseDemo.createTable()->family:" + family);
hTableDescriptor.addFamily(family);
admin.createTable(hTableDescriptor);
System.out.println("HBaseDemo.createTable()->admin3:" + admin);
}
System.out.println("-----------------创建表结束 ---------------");
}
/**
* 查询表中的数据
*/
public static void queryTable(String tableNameString) throws IOException {
System.out.println("--------------------查询整表的数据--------");
Table table = connection.getTable(TableName.valueOf(tableNameString));
ResultScanner scanner = table.getScanner(new Scan());
for (Result result : scanner) {
byte[] row = result.getRow();
System.out.println("row key is:"+new String(row));
List<Cell> listCells = result.listCells();
for (Cell cell : listCells) {
byte[] familyArray = cell.getFamilyArray();
byte[] qualifierArray = cell.getQualifierArray();
byte[] valueArray = cell.getValueArray();
System.out.println("row value is:"+ new String(familyArray) +
new String(qualifierArray) + new String(valueArray));
}
}
System.out.println("---------------查询整表数据结束----------");
}
/**
* 输入行键的名字
* @param rowName
* @throws IOException
*/
public static void queryTableByRowKey(String tableNameString,String rowNameString) throws IOException {
System.out.println("---------------按行建查询表中数据--------");
Table table=connection.getTable(TableName.valueOf(tableNameString));
Get get = new Get(rowNameString.getBytes());
Result result = table.get(get);
byte[] row = result.getRow();
System.out.println("row key is:" +new String(row));
List<Cell> listCells = result.listCells();
for (Cell cell : listCells) {
byte[] familyArray = cell.getFamilyArray();
byte[] qualifierArray = cell.getQualifierArray();
byte[] valueArray = cell.getValueArray();
System.out.println("row value is:"+ new String(familyArray) +
new String(qualifierArray) + new String(valueArray));
}
System.out.println("---------------查行键数据结束----------");
}
public static void queryTableCondition(String tableNameString) throws IOException {
System.out.println("------------------按条件查询---------------");
Table table = connection.getTable(TableName.valueOf(tableNameString));
Filter filter = new SingleColumnValueFilter(Bytes.toBytes("base"),
Bytes.toBytes("name"), CompareOp.EQUAL, Bytes.toBytes("bookName"));
Scan scan = new Scan();
scan.setFilter(filter);
ResultScanner scanner =table.getScanner(scan);
for (Result result : scanner) {
byte[] row = result.getRow();
System.out.println("row key is:" + new String(row));
List<Cell> listCells = result.listCells();
for (Cell cell : listCells) {
byte[] familyArray = cell.getFamilyArray();
byte[] qualifierArray = cell.getQualifierArray();
byte[] valueArray = cell.getValueArray();
System.out.println("row value is:" + new String(familyArray)
+ new String(qualifierArray) + new String(valueArray));
}
}
System.out.println("------------------------按条件查询结束--------------------");
}
/**
* 这是清空表的函数,用以使表变得无效
* @param tableNameString
* @throws IOException
*/
public static void truncateTable(String tableNameString) throws IOException {
System.out.println("-------------------------清空表开始------------------");
TableName tableName = TableName.valueOf(tableNameString);
admin.disableTable(tableName);
admin.truncateTable(tableName, true);
System.out.println("-------------------------清空表结束-----------------");
}
/**
* 删除指定的表,输入值为表名
* @param tableNameString
* @throws IOException
*/
public static void deleteTable(String tableNameString) throws IOException {
System.out.println("-----------------------删除表---------------");
admin.disableTable(TableName.valueOf(tableNameString));
admin.deleteTable(TableName.valueOf(tableNameString));
System.out.println("-------------------------删除表-----------------------");
}
/**
* rowkey 是第二层,一行有很多的数据
*
* @throws IOException
*/
public static void deleteByRowKey(String tableNameString, String rowKey) throws IOException {
System.out.println("删除行开始");
Table table =connection.getTable(TableName.valueOf(tableNameString));
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
System.out.println("删除行结束");
}
/**
* 新建一个列簇,第一个是表名,第二个是列簇名
* @param tableNameString
* @param columnFamily
* @throws IOException
*/
public static void addColumnFamily(String tableNameString, String columnFamily) throws IOException {
System.out.println("新建列簇开始");
TableName tableName = TableName.valueOf(tableNameString);
HColumnDescriptor columnDescriptor = new HColumnDescriptor(columnFamily);
admin.addColumn(tableName, columnDescriptor);
System.out.println("新建列簇结束");
}
/**
* 删除列簇的函数,第一个是表名,第二个是列簇名
* @param tableNameString
* @param columnFamily
* @throws IOException
*/
public static void DeleteColumnFamily(String tableNameString, String columnFamily) throws IOException {
System.out.println("删除列簇开始");
TableName tableName = TableName.valueOf(tableNameString);
admin.deleteColumn(tableName, columnFamily.getBytes());
System.out.println("删除列簇成功");
}
/**
* 是插入的一个函数,插入的是一个put的list。具体创建方法
* List<Put> putList = new ArrayList<put>();
* Put put;
* for(int i = 0; i < 10; i++){
* put = new Put(Bytes.toBytes("row" + i));
* put.addColumn(Bytes.toBytes("Base")//列簇,Bytes.toBytes("name")//列名,Bytes.toBytes("bookName")//值);
* putList.ad(put);
* }
*
* @param tableNameString
* @param putList
* @throws IOException
*/
public static void insert(String tableNameString, List<Put> putList) throws IOException {
System.out.println("开始执行插入操作");
Table table = connection.getTable(TableName.valueOf(tableNameString));
table.put(putList);
System.out.println("插入成功");
}
}
4.成功页面截图