package test;
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 javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/hbaseServlet")
public class hbase extends HttpServlet {
private static Connection connection=null;
private static Admin admin=null;
static {
try {
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
connection= ConnectionFactory.createConnection(configuration);
admin=connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
//1.判断表是否存在
public static boolean isTableExist(String tableName) throws IOException {
// //1.获取配置文件信息
// //HBaseConfiguration configuration = new HBaseConfiguration();
// Configuration configuration = HBaseConfiguration.create();
// configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
//
// //2.获取管理员对象
// //HBaseAdmin admin = new HBaseAdmin(configuration);
// Connection connection = ConnectionFactory.createConnection(configuration);
// Admin admin = connection.getAdmin();
//3.判断表是否存在
boolean exists=admin.tableExists(TableName.valueOf(tableName));
//4.关闭连接
// admin.close();
return exists;
}
//2.创建表
public static void createTable(String tableName,String... cfs) throws IOException {
//1.判断是否存在列族信息
if(cfs.length<=0){
System.out.println("列族信息为空");
return;
}
//2.判断表是否存在
if(isTableExist(tableName)){
System.out.println("表存在");
return;
}
//3.创建表描述器
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
//4.循环添加列族信息
for(String cf:cfs){
//5.创建列族描述器
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
//6.添加具体列族信息
hTableDescriptor.addFamily(hColumnDescriptor);
}
//7.创建表
admin.createTable(hTableDescriptor);
}
//3.删除表
public static void dropTable(String tableName) throws IOException {
//1.判断表是否存在
if(!isTableExist(tableName)){
System.out.println("表不存在");
return;
}
//2.使表下线
admin.disableTable(TableName.valueOf(tableName));
//3.删除表
admin.deleteTable(TableName.valueOf(tableName));
}
//4.创建命名空间
public static void createNameSpace(String ns){
//1.创建命名空间描述器
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(ns).build();
//2.创建命名空间
try {
admin.createNamespace(namespaceDescriptor);
}catch (NamespaceExistException e){
System.out.printf(ns+"命名空间存在");
} catch (IOException e) {
e.printStackTrace();
}
}
//关闭连接
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();
}
}
}
//5.向表中插入数据
public static void putData(String tableName,String rowKey,String cf,String cn,String value) throws IOException {
//1.获取表对象呢
Table table = connection.getTable(TableName.valueOf(tableName));
//2.创建put对象(添加多行,则创建多个put对象)
Put put = new Put(Bytes.toBytes(rowKey));
//3.给put对象赋值(同一个put对象可以添加多个列)
put.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn),Bytes.toBytes(value));
//4.插入数据
table.put(put);
//5.关闭表连接
}
//6.获取数据get
public static void getData(String tableName,String rowKey,String cf,String cn) throws IOException {
//1.获取表对象
Table table = connection.getTable(TableName.valueOf(tableName));
//2.创建get对象
Get get = new Get(Bytes.toBytes(rowKey));
//2.1指定获取的列族
//get.addFamily(Bytes.toBytes(cf));
//2.2指定列族和列
//get.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn));
//2.3获取数据的版本数
//get.setMaxVersions();
//3.获取数据
Result result=table.get(get);
//4.解析result
for( Cell cell:result.rawCells()){
//5.打印数据
System.out.printf("CF:"+Bytes.toString(CellUtil.cloneFamily(cell))+
",CN:"+Bytes.toString(CellUtil.cloneQualifier(cell))+
",Value:"+Bytes.toString(CellUtil.cloneValue(cell)));
}
//6.关闭表连接
table.close();
}
//7.获取数据scan
public static void scanTable(String tableName) throws IOException {
//1.获取表对象
Table table = connection.getTable(TableName.valueOf(tableName));
//2.构建scan对象
Scan scan = new Scan(Bytes.toBytes("1001"));
//3.扫描表
ResultScanner resultScanner = table.getScanner(scan);
//4.解析resultScanner
for(Result result:resultScanner){
//5.解析并打印
for(Cell cell:result.rawCells()){
System.out.printf("RK:"+Bytes.toString(CellUtil.cloneRow(cell))+",CF:"+Bytes.toString(CellUtil.cloneFamily(cell))+
",CN:"+Bytes.toString(CellUtil.cloneQualifier(cell))+
",Value:"+Bytes.toString(CellUtil.cloneValue(cell)));
}
}
table.close();
}
//8.删除数据
public static void deleteData(String tableName,String rowKey,String cf,String cn) throws IOException {
//1.获取表对象
Table table = connection.getTable(TableName.valueOf(tableName));
//2.构建删除对象
Delete delete = new Delete(Bytes.toBytes(rowKey));//删除行
//2.1设置删除的列
//delete.addColumns(Bytes.toBytes(cf),Bytes.toBytes(cn));//不指定时间戳会删除所有
//delete.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn));//不指定时间戳会删除最新的
//2.2删除指定的列族
//delete.addFamily(Bytes.toBytes(cf));
//3.执行删除操作
table.delete(delete);
table.close();
}
public static void main(String[] args) throws IOException{
//1.测试表是否存在
System.out.println(isTableExist("stu5"));
// //2.创建表测试
// createTable("stu5","info1","info2");
// //3.删除表
// dropTable("stu5");
// System.out.println(isTableExist("stu5"));
// //4.插入数据
// putData("stu","1001","info2","sex","man");
// //5.获取单行数据
// getData("stu","1001","info2","name");
// //6.scan方法获取数据
// scanTable("stu");
//7.删除
// deleteData("stu","1001","","");
// close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String kjcxwt_cxfw=req.getParameter("kjcxwt_cxfw");
String kjcxwt_cname=req.getParameter("kjcxwt_cname");
String nov_purpose=req.getParameter("nov_purpose");
String kjcxwt_cxyd=req.getParameter("kjcxwt_cxyd");
String finishdate=req.getParameter("finishdate");
String subject=req.getParameter("subject");
String industry=req.getParameter("industry");
String kjcxwt_memo=req.getParameter("kjcxwt_memo");
System.out.printf(kjcxwt_cxfw+":"+kjcxwt_cname+":"+nov_purpose+":"+kjcxwt_cxyd+":"
+finishdate+":"+subject+":"+industry+":"+kjcxwt_memo);
}
}
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.1</version>
</dependency>