第一步:启动hadoop,启动hbase
第二步,新建Java Project——>新建Class
第三步:在工程中导入Linux中路径/usr/local/hbase/lib下所有的jar包:
链接:https://pan.baidu.com/s/12GjSyFuvTG_lA2ni9xc1DQ
提取码:4zt6
注意:必须新建立项目,不能够在HDFS Java项目的基础上导入hbase的包,会引起包冲突。
1、在hbase中建表
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class MyCreate {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void main(String[] args) throws IOException {
//设置虚拟机ip和端口,端口号默认2181
Configuration configuration = HBaseConfiguration.create();
configuration.set("zookeeper.znode.parent", "/hbase-unsecure");
configuration.set("hbase.zookeeper.quorum", "192.168.19.128");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
//创建连接
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
System.out.println("start create table");
// 新建表(不能直接将字符串“Score”传入HTableDescriptor,必须将"Score"转化为对象,其实这个我觉得很奇怪,没必要·新建一个对象,有空研究一下)
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("Score"));
// 新建列族
HColumnDescriptor hColumnDescriptorSname = new HColumnDescriptor("sname");
hTableDescriptor.addFamily(hColumnDescriptorSname);
// 新建列族
HColumnDescriptor hColumnDescriptorCourse = new HColumnDescriptor("course");
hTableDescriptor.addFamily(hColumnDescriptorCourse);
// 建表
admin.createTable(hTableDescriptor);
// 关闭连接
admin.close();
connection.close();
System.out.println("ok");
}
}
2、删除表格
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class MyDeleteTable {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void main(String[] args) throws IOException {
//输入要删除的表格名字·
String tableName = "Score";
Configuration configuration = HBaseConfiguration.create();
configuration.set("zookeeper.znode.parent", "/hbase-unsecure");
configuration.set("hbase.zookeeper.quorum", "192.168.19.128");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
System.out.println("start delete table " + tableName);
//创建tableName这个对象,对ta禁用,然后删除
TableName tablename = TableName.valueOf(tableName);
admin.disableTable(tablename);
admin.deleteTable(tablename);
admin.close();
connection.close();
System.out.println("ok");
}
}
删除某一行:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class MyDeleteData {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void main(String[] args) throws IOException {
//设置表和行数
String tableName = "Score";
String rowkey = "95001";
Configuration configuration = HBaseConfiguration.create();
configuration.set("zookeeper.znode.parent", "/hbase-unsecure");
configuration.set("hbase.zookeeper.quorum", "192.168.19.128");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
System.out.println("start delete "+tableName+"'s "+rowkey+" line");
Table table = connection.getTable(TableName.valueOf(tableName));
//设置要删除的行键
Delete delete = new Delete(rowkey.getBytes());
table.delete(delete);
table.close();
admin.close();
connection.close();
System.out.println("ok");
}
}
3、查询
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class MyGetdata {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String tableName = "Score";
String rowkey = "95001";
String colFamily ="sname";
String col = "";
Configuration configuration = HBaseConfiguration.create();
configuration.set("zookeeper.znode.parent", "/hbase-unsecure");
configuration.set("hbase.zookeeper.quorum", "192.168.19.128");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
Table table = connection.getTable(TableName.valueOf(tableName));
System.out.println("start ");
Get get = new Get(rowkey.getBytes());
get.addColumn(colFamily.getBytes(), col.getBytes());
Result result = table.get(get);
System.out.println(result);
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("RowName:" + new String(CellUtil.cloneRow(cell)) + " ");
System.out.println("Timetamp:" + cell.getTimestamp() + " ");
System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)) + " ");
System.out.println("row Name:" + new String(CellUtil.cloneQualifier(cell)) + " ");
System.out.println("value:" + new String(CellUtil.cloneValue(cell)) + " ");
}
admin.close();
connection.close();
System.out.println("-------------------------------------------");
}
}