一、环境搭建
集群规划:
Master:192.168.76.11
Slave1:192.168.76.12
Slave2:192.168.76.13
操作系统:RedHat Linux7
对三台虚拟机进行,修改主机名,配置域名映射,关闭防火墙,配置免密登录操作,上传安装包,创建安装目录操作。本项目选择配置Hadoop完全分布式、Zookeeper集群模式和Hbase完全分布式。
- Hadoop集群部署(仅在master执行)
安装jdk
解压jdk-1.8的压缩包到安装目录
配置全局环境变量(vim /etc/profile)
#jdk
export JAVA_HOME=/lx/bigdata/jdk
export PATH=$PATH:$JAVA_HOME/bin
将jdk安装目录分发给另外两台虚拟机,然后刷新环境变量
安装hadoop
解压hadoop-2.7.1压缩包到安装目录
去到安装目录下的/etc/Hadoop,修改core-site.xml、hadoop-env.sh、hdfs- site.xml、mapred-site.xml、yarn-site.xml、yarn-env.sh、slaves
core-site.xml
hadoop-env.sh
export JAVA_HOME=/usr/jdk
hdfs-site.xml
mapred-site.xml
默认系统中没有这个配置文件,但是有它的模板我们复制一份
cp mapred-site.xml.template mapred-site.xml
yarn-site.xml
yarn-env.sh
export JAVA_HOME=/usr/jdk
slaves
删除原有内容添加
slave1
slave2
然后vim /etc/profile 写入
#hadoop
export HADOOP_HOME=/lx/bigdata/hadoop
export PATH=$PATH:$HADOOP_HOME/bin:$HADOOP_HOME/sbin
最后将修改的文件分发给slave、slave2,在三台虚拟机上刷新环境变量
格式化namenode
hdfs namenode -format
Zookeper的安装(仅在master操作)
解压zookeeper-3.4到安装目录
修改/etc/profile,写入
#zookeeper
export ZOOKEEPER_HOME=/lx/bigdata/zookeeper
export PATH=$PATH:$ZOOKEEPER_HOME/bin
进入安装目录下的conf目录
修改zoo.cgf,系统默认也是没有的,所以复制一份
cp zoo_sample.cfg zoo.cfg
zoo.cfg
修改dataDir参数,改为自己使用的目录(默认可以不用修改),在文件末尾加入
server.1=192.168.76.11:2888:3888
server.2=192.168.76.12:2888:3888
server.3=192.168.76.13:2888:3888
然后创建目录(就是dataDir设置的目录)
最后将修改的文件分发给slave、slave2,在三台虚拟机上刷新环境变量
在三台虚拟机上分别进入刚刚创建的目录,执行
Master
echo 1 > myid
Slave1
echo 2 > myid
Slave3
echo 3 > myid
Hbase安装(仅在master操作)
将hbase-1.4.13解压到安装目录
进入安装目录下的conf目录,修改配置文件
hbase-site.xml
hbase-env.sh
注释下面两行内容
export Hbase_MASTER_OPTS="$Hbase_MASTER_OPTS -XX:PermSize=128m -XX:MaxPermSize=128m"
export Hbase_REGIONSERVER_OPTS="$Hbase_REGIONSERVER_OPTS -XX:PermSize=128m -XX:MaxPermSize=128m"
写入以下内容
export Hbase_CLASSPATH=/lx/bigdata/hadoop/etc/hadoop
export JAVA_HOME=/lx/bigdata/jdk
export Hbase_MANAGES_ZK=false
系统默认使用内置zookeeper,改为false意思为使用外置zookeeper
修改/etc/profile,写入
#hbase
export Hbase_HOME=/lx/bigdata/hbase
export PATH=$PATH:$Hbase_HOME/bin
最后将修改的文件分发给slave、slave2,在三台虚拟机上刷新环境变量
最后开启集群
Start-all.sh(只需在master执行)
zkServer.sh start(三台都要执行)
start-hbase.sh(只需在master执行)
查看进程(jps)
Master:
Slave1:
Slave2:
二、代码部分
导入jar包(jar包为hbase安装目录下lib目录的所有jar包)
hbaseutils
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 hbaseutils {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void init() throws IOException //执行数据库操作之前,把与创建configuration, Connection, Admin相关的代码统一放到静态的初始化方法init()中
{
configuration = HbaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "master");
connection = ConnectionFactory.createConnection(configuration); //实质得到是Connection接口的间接实现类HConnectionImplementation的一个实例
admin = connection.getAdmin();//实质得到是Admin接口的直接接实现类HbaseAdmin的一个实例
System.out.println("Hbase Init Success");
}
public static void close() throws IOException
{
connection.close();
admin.close();
}
//help
public static void helpinfo() {
System.out.println("command"+"ttt"+"sample code");
System.out.println("list:"+"ttt"+"list");
System.out.println("exists:"+"ttt"+"exists t1");
System.out.println("disable:"+"tt"+"disable t1");
System.out.println("desc:"+"ttt"+"desc t1");
System.out.println("create:"+"ttt"+"create t1 f1");
System.out.println("alter:"+"ttt"+"alter t1 f1");
System.out.println("delete:"+"ttt"+"delete t1");
System.out.println("put:"+"ttt"+"put t1 r1 f1 q1 v1");
System.out.println("getr:"+"ttt"+"getr t1 r1");
System.out.println("getc:"+"ttt"+"getc t1 r1 f1 q1");
System.out.println("scant:"+"ttt"+"scant t1");
System.out.println("scanf:"+"ttt"+"scanf t1 f1");
System.out.println("deleter:"+"tt"+"deleter t1 r1");
System.out.println("deletec:"+"tt"+"deletec t1 r1 f1 q1");
System.out.println("listNameSpaces:"+"tt"+"listNameSpaces");
System.out.println("listTables:"+"tt"+"listTables namespace");
System.out.println("createNameSpace:"+"t"+"createNameSpace namespace");
System.out.println("help:"+"ttt"+"help");
}
//list
public static void listTables() throws IOException//实现列出所有的表名
{
//通过TableName类获取表名
TableName[] tableNames = admin.listTableNames();
for(TableName tableName : tableNames)
{
System.out.println(new String(tableName.getName()));//把字节数组byte[]转换成String
}
}
//exists
public static boolean isTableExist(String strTableName) throws IOException//判断表是否存在
{
//处理不合法的情况,直接return
if(!isTableNameFormatCorrect(strTableName))
{
return false;
}
TableName tableName = TableName.valueOf(strTableName);
return admin.tableExists(tableName);
}
public static boolean isTableNameFormatCorrect(String strTableName)
{
// System.out.println(strTableName.indexOf(":"));
// System.out.println(strTableName.lastIndexOf(":"));
// System.out.println(strTableName.length());
//输入参数的合法性检查,提高方法代码的健壮性
//包含不只1个冒号||第一个字符是冒号||最后一个字符是冒号
if(strTableName.indexOf(":") != strTableName.lastIndexOf(":") || //第一个字符是冒号
strTableName.indexOf(":")==0 || //最后一个字符是冒号
strTableName.indexOf(":")==strTableName.length()-1)//包含不只1个冒号|
{
System.out.println("Input parameter format incorrect");
return false;
}
return true;
}
//desc
public static void descTable(String strTableName) throws IOException//描述指定表的属性信息
{
//处理不合法的情况,直接返回false
if (!isTableNameFormatCorrect(strTableName)) {
return;
}
if(!isTableExist(strTableName)) //表不允许被删除返回false
{
System.out.println("Table dose not exists");
return ;
}
TableName tableName = TableName.valueOf(strTableName);//获取表名对象
if (admin.isTableEnabled(tableName)) //判断表是否可用
{
System.out.println("Table " + strTableName + " is ENABLED");
} else {
System.out.println("Table " + strTableName + " is DISABLED");
}
HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);
// System.out.println(strTableName);
HColumnDescriptor[] hcds = tableDescriptor.getColumnFamilies();
System.out.println("COLUMN FAMILIES DEscriptION");
for (HColumnDescriptor hcd : hcds) {
System.out.println("{NAME => " + hcd.getNameAsString() + ", " +
"BLOOMFILTER => " + hcd.getBloomFilterType() + ", " +
"VERSIONS => " + hcd.getMaxVersions() + ", " +
"IN_MEMORY => " + hcd.isInMemory() + ", " +
"KEEP_DELETED_CELLS => " + hcd.getKeepDeletedCells() + ", " +
"DATA_BLOCK_ENCODING => " + hcd.getDataBlockEncoding().name() + ", " +
"TTL => " + hcd.getTimeToLive() + ", " +
"COMPRESSION => " + hcd.getCompression().getName() + ", " +
"MIN_VERSIONS => " + hcd.getMinVersions() + ", " +
"BLOCKCACHE => " + hcd.isBlockCacheEnabled() + ", " +
"BLOCKSIZE => " + hcd.getBlocksize() + ", " +
"REPLICATION_SCOPE => " + hcd.getDFSReplication() + "}"
);//获取列族名称
}
}
//create
public static void createTable(String strTableName, String strColumnFamilyName) throws IOException
{
if(!isTableNameFormatCorrect(strTableName)) //判断表是否允许创建
{
return;
}
if(isTableExist(strTableName)) //如果表存在则直接返回
{
System.out.println("Table " + strTableName + " does exist");
return;
}
TableName tableName = TableName.valueOf(strTableName);
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
HColumnDescriptor columnDescriptor = new HColumnDescriptor(strColumnFamilyName);
tableDescriptor.addFamily(columnDescriptor);
admin.createTable(tableDescriptor);
}
//alter
public static void alterTable(String strTableName, String newColumnFamily) throws IOException
{
if(!isTableExist(strTableName)) //表不存在则返回false
{
System.out.println("Table dose not exists");
return;
}
TableName tableName = TableName.valueOf(strTableName);//获取表名对象
HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);//获取表的描述器
if(isColumnFamilyExists(tableDescriptor, newColumnFamily)) //如果表的列族已存在,则直接返回
{
return;
}
HColumnDescriptor columnDescriptor = new HColumnDescriptor(newColumnFamily);//创建一个新的列族描述器
tableDescriptor.addFamily(columnDescriptor);//添加新列族描述器到表描述器
admin.modifyTable(tableName, tableDescriptor); //提交修改表的操作
}
public static boolean isColumnFamilyExists(HTableDescriptor tableDescriptor, String strFamiliyName) throws IOException//实现列出所有的表名
{
HColumnDescriptor[] hColumnDescriptors = tableDescriptor.getColumnFamilies();
for(HColumnDescriptor hColumnDescriptor: hColumnDescriptors)
{
if(hColumnDescriptor.getNameAsString().equals(strFamiliyName))
{
// System.out.println("Column Family already exists");
return true;
}
}
return false;
}
//delete
public static void dropTable(String strTableName) throws IOException
{
if(!isTableAllowDrop(strTableName)) //返回false,表不允许删除
{
System.out.println("Table dose not allow to be dropped");
return;
}
TableName tableName = TableName.valueOf(strTableName);
admin.deleteTable(tableName);
System.out.println("Table deleted successfully");
}
public static boolean isTableAllowDrop(String strTableName) throws IOException//判断表是否存在
{
if(!isTableExist(strTableName)) //表不允许被删除返回false
{
System.out.println("Table dose not exists");
return false;
}
TableName tableName = TableName.valueOf(strTableName);
if(admin.isTableEnabled(tableName)) //判断表是否被禁用,如果是enabled,返回true
{
admin.disableTable(tableName);//删除前先将表禁用 ,使其off-line
}
return true;
}
public static void scanTable(String strTableName) throws IOException
{
if(!isTableExist(strTableName)) //如果表不存在则直接返回
{
System.out.println("Table dose not exists");
return;
}
TableName tableName = TableName.valueOf(strTableName); //获取表名对象
Scan scan = new Scan(); //创建扫描器对象
Table table = connection.getTable(tableName);//获取操作表的Table对象
ResultScanner resultScanner = table.getScanner(scan);//获得用于检索数据的ResultScanner对象
printScanResult(resultScanner);//输出扫描结果
}
public static void printScanResult(ResultScanner resultScanner) throws IOException
{
for (Result result : resultScanner)
{
// System.out.println(result);
Cell[] cells = result.rawCells();//返回单元格Cell的数组
printCellValue(cells);//输出单元格Cell数组的内容
}
}
public static void printCellValue(Cell[] cells) throws IOException//实现列出所有的表名
{
for (Cell cell : cells)
{
System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell)) +
" | column family:" + Bytes.toString(CellUtil.cloneFamily(cell)) +
" | column:" + Bytes.toString(CellUtil.cloneQualifier(cell)) +
" | timestamp:" + cell.getTimestamp() +
" | cell value:" + Bytes.toString(CellUtil.clonevalue(cell)));
}
}
//put
public static void putACellValue(String strTableName, String rowKey, String columnFamily, String column, String value) throws IOException
{
if(!isTableExist(strTableName)) //如果表不存在则直接返回
{
System.out.println("Table dose not exists");
return;
}
TableName tableName = TableName.valueOf(strTableName);//获取表名对象
if(!isColumnFamilyExists(admin.getTableDescriptor(tableName), columnFamily)) //如果列族不存在则直接返回
{
System.out.println("Column Family does not exists");
return;
}
Table table = connection.getTable(tableName);//获取操作表的Table对象
Put put = new Put(Bytes.toBytes(rowKey));//创建一个Put对象,指定行键rowKey
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));//增加一列,指定列族、列修饰符、单元格值
table.put(put); //执行put方法,插入一列
}
//getr
public static void getCellValue(String strTableName, String rowKey) throws IOException
{
if(!isTableExist(strTableName)) //如果表不存在则直接返回
{
System.out.println("Table dose not exists");
return;
}
TableName tableName = TableName.valueOf(strTableName); //获取表名对象
Table table = connection.getTable(tableName);//获取操作表的Table对象
Get get = new Get(Bytes.toBytes(rowKey));//创建一个Get对象,指定行键rowKey
Result result = table.get(get);//执行get方法,获得指定列的指,返回结果
Cell[] cells = result.rawCells();//获取单元格对象的数组
printCellValue(cells);//输出单元格数组的内容
}
//getc
public static void getCellValue(String strTableName, String rowKey, String columnFamily, String column) throws IOException
{
if(!isTableExist(strTableName)) //如果表不存在则直接返回
{
System.out.println("Table dose not exists");
return;
}
TableName tableName = TableName.valueOf(strTableName); //获取表名对象
if(!isColumnFamilyExists(admin.getTableDescriptor(tableName), columnFamily)) //如果列族不存在则直接返回
{
System.out.println("Column Family does not exists");
return;
}
Table table = connection.getTable(tableName);//获取操作表的Table对象
Get get = new Get(Bytes.toBytes(rowKey));//创建一个Get对象,指定行键rowKey
get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));//加入列族名和列名作为查询条件
Result result = table.get(get);//执行get方法,获得指定列的指,返回结果
Cell[] cells = result.rawCells();//获取单元格对象的数组
printCellValue(cells);//输出单元格数组的内容
}
//scant
// public static void scanTable(String strTableName) throws IOException
// {
// if(!isTableExist(strTableName)) //如果表不存在则直接返回
// {
// System.out.println("Table dose not exists");
// return;
// }
//
// TableName tableName = TableName.valueOf(strTableName); //获取表名对象
// Scan scan = new Scan(); //创建扫描器对象
// Table table = HbaseJavaAPIUtils.connection.getTable(tableName);//获取操作表的Table对象
// ResultScanner resultScanner = table.getScanner(scan);//获得用于检索数据的ResultScanner对象
//
// printScanResult(resultScanner);//输出扫描结果
// }
//scanf
public static void scanTable(String strTableName, String columnFamily) throws IOException
{
if(!isTableExist(strTableName)) //如果表不存在则直接返回
{
System.out.println("Table dose not exists");
return;
}
TableName tableName = TableName.valueOf(strTableName); //获取表名对象
if(!isColumnFamilyExists(admin.getTableDescriptor(tableName), columnFamily)) //如果列族不存在则直接返回
{
System.out.println("Column Family does not exists");
return;
}
Scan scan = new Scan(); //创建扫描器对象
scan.addFamily(Bytes.toBytes(columnFamily));//把要扫描的一个列族增加到扫描器中
Table table = connection.getTable(tableName);//获取操作表的Table对象
ResultScanner resultScanner = table.getScanner(scan);//获得用于检索数据的ResultScanner对象
printScanResult(resultScanner);//输出扫描结果
}
//deleter
public static void deleteCellValue(String strTableName, String rowKey) throws IOException
{
if(!isTableExist(strTableName)) //如果表不存在则直接返回
{
System.out.println("Table dose not exists");
return;
}
TableName tableName = TableName.valueOf(strTableName); //获取表名对象
Table table = connection.getTable(tableName);//获取操作表的Table对象
Delete delete = new Delete(Bytes.toBytes(rowKey));//创建一个Delete对象,指定要删除列的行键rowKey
table.delete(delete);//执行delete方法,删除该列
}
//deletec
public static void deleteCellValue(String strTableName, String rowKey, String columnFamily, String column) throws IOException
{
if(!isTableExist(strTableName)) //如果表不存在则直接返回
{
System.out.println("Table dose not exists");
return;
}
TableName tableName = TableName.valueOf(strTableName); //获取表名对象
if(!isColumnFamilyExists(admin.getTableDescriptor(tableName), columnFamily)) //如果列族不存在则直接返回
{
System.out.println("Column Family does not exists");
return;
}
Table table =connection.getTable(tableName);//获取操作表的Table对象
Delete delete = new Delete(Bytes.toBytes(rowKey));//创建一个Delete对象,指定要删除列的行键rowKey
delete.addColumns(Bytes.toBytes(columnFamily), Bytes.toBytes(column));//在Delete对象中增加要删除的列,指定列族和列名
table.delete(delete);//执行delete方法,删除该列
}
//listNameSpaces
public static void listNameSpaces() throws IOException//实现列出所有的表名
{
NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
for(NamespaceDescriptor namespaceDescriptor: namespaceDescriptors)
{
System.out.println(namespaceDescriptor.getName());//获取名字空间名称的字符串
}
}
//查看指定名字空间的表
public static void listTables(String strNameSpace) throws IOException//实现列出所有的表名
{
//检查名字空间是否存在
if(!isNameSpacesExists(strNameSpace)) //不存在则直接返回
{
return;
}
TableName[] tableNames = admin.listTableNamesByNamespace(strNameSpace);
listTables(tableNames);
}
public static boolean isNameSpacesExists(String strNameSpace) throws IOException//实现列出所有的表名
{
NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
for(NamespaceDescriptor namespaceDescriptor: namespaceDescriptors)
{
if(namespaceDescriptor.getName().equals(strNameSpace))//判断名字空间名称的字符串是否相等
{
return true;
}
}
// System.out.println("NameSpace does not exists");
return false;
}
public static void listTables(TableName[] tableNames) throws IOException//实现列出所有的表名
{
for(TableName tableName : tableNames)
{
System.out.println(tableName.getNameAsString());
}
}
//createNameSpace
public static void createNameSpace(String strNameSpace) throws IOException//实现列出所有的表名
{
if(isNameSpacesExists(strNameSpace))//如果名字空间存在
{
System.out.println("NameSpace already exists");
return;
}
// NamespaceDescriptor namespaceDescriptor = new NamespaceDescriptor();//构造方法是私有的,不能直接调用
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(strNameSpace).build();
admin.createNamespace(namespaceDescriptor);
}
public static void disable(String strTableName) throws IOException {
if(!isTableExist(strTableName)) //如果表不存在则直接返回
{
System.out.println("Table dose not exists");
return;
}
TableName tableName = TableName.valueOf(strTableName);
if(admin.isTableEnabled(tableName)) //判断表是否被禁用,如果是enabled,返回true
{
admin.disableTable(tableName);//删除前先将表禁用 ,使其off-line
}
return ;
}
}
hbaseshell
import java.io.IOException;
import java.util.Scanner;
public class hbaseshell {
public static void main(String[] args) throws IOException {
hbaseutils.init();//执行数据库操作之前,先 初始化,生成配置,建立连接
String input = "";
String[] param = {};
while (true) {
System.out.print("HbaseShell>");
Scanner scan = new Scanner(System.in);
input = scan.nextLine();//获取终端的下一行输入,nextLine()回车结束,不会以空格结束
String tmp="";
if(input.length()>0) {
tmp+=input.charAt(0);
}
//将各参数之间的多个空格去除,仅保留一个空格
for(int i=1;i
用一个命令来展示一下效果



