ubantu操作hbase

到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指令可以查看表里的数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kkoneone11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值