赞
踩
到firefox中按照网站找以下文件进行下载
新建一个窗口启动节点
下载完成则回到下载目录
如果下载慢也可以将文件放在share中,然后拷贝到当前目录
进入到root
然后回到hadoop
解压到/usr/local
进入到local,将hbase改名
修改权限
配置环境变量
执行
回到hbase配置conf文件
打开另一个窗口找到这个路径
然后复制到这个下面
最底下注释掉这个
配置site
启动hbase
通过shell进入hbase查看版本号,可以通过exit退出
再通过version
再进入Hbase shell启动管理器。然后通过list查看
如果有student这个表可以先删除了
打开eclipse,然后新建一个项目
添加jar包
Lib包下除了文件夹其他都添加进去
再添加这个jar包,即可finish
在该项目下创建一个新的类
然后编写以下代码进行测试
- package hbaseTestfile;
-
- 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 hBaseTest {
-
- public static Configuration configuration;
- public static Connection connection;
- public static Admin admin;
- public static void main(String[] args) throws IOException{
- init();
- createTable("student", new String[]{"score"});
- insertData("student", "zhangsan", "score", "English", "69");
- insertData("student", "zhangsan", "score", "Math", "86");
- insertData("student", "zhangsan", "score", "Computer", "77");
- getData("student", "zhangsan", "score", "English");
- close();
- }
-
- public static void init(){
- configuration = HBaseConfiguration.create();
- configuration.set("Hbase.rootdir","hdfs://localhost:9000/hbase");
- try {
- connection = ConnectionFactory.createConnection(configuration);
- admin = connection.getAdmin();
- }catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public static void close(){
- try {
- if(admin != null) {
- admin.close();
- }
- if(null != connection){
- connection.close();
- }
- }catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void createTable(String myTableName, String[] colFamily) throws IOException{
- TableName tableName = TableName.valueOf(myTableName);
- if(admin.tableExists(tableName)) {
- System.out.println("table exists");
- }else {
- TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
- for(String str: colFamily) {
- ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();
- tableDescriptor.setColumnFamily(family);
- }
- admin.createTable(tableDescriptor.build());
- }
- }
- public static void insertData(String tableName, String rowKey, String colFamily, String col, String val) throws IOException {
- Table table = connection.getTable(TableName.valueOf(tableName));
- Put put = new Put(rowKey.getBytes());
- put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
- table.put(put);
- table.close();
- }
- public static void getData(String tableName, String rowKey, String colFamily, String col) throws IOException{
- Table table = connection.getTable(TableName.valueOf(tableName));
- Get get = new Get(rowKey.getBytes());
- get.addColumn(colFamily.getBytes(), col.getBytes());
- Result result = table.get(get);
- System.out.println(new String(result.getValue(colFamily.getBytes(), col==null?null:col.getBytes())));
- table.close();
- }
-
- }
启动项目后
可以看到正确输出69
也可以看到有student这个表
通过scan指令可以查看表里的数据
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。