java 操作 hbase_Java操作Hbase的基本操作

最基本的入门展示

package hbasetest;

import java.io.IOException;

import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.HColumnDescriptor;

import org.apache.hadoop.hbase.HTableDescriptor;

import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.hbase.client.Admin;

import org.apache.hadoop.hbase.client.Connection;

import org.apache.hadoop.hbase.client.ConnectionFactory;

public class HelloHBase {

public static void main(String[] args) throws IOException, URISyntaxException {

//获取配置文件

Configuration conf= HBaseConfiguration.create();

conf.addResource(new Path(ClassLoader.getSystemResource("hbase-site.xml").toURI()));

conf.addResource(new Path(ClassLoader.getSystemResource("core-site.xml").toURI()));

//创建连接

try(Connection connection=ConnectionFactory.createConnection(conf);

Admin admin=connection.getAdmin()){

//定义表名

TableName tableName=TableName.valueOf("mytable6");

//定义表

HTableDescriptor table=new HTableDescriptor(tableName);

//定义列族

HColumnDescriptor mycf=new HColumnDescriptor("mycf");

//执行创建表操作

table.addFamily(new HColumnDescriptor(mycf));

admin.createTable(table);

admin.close();

connection.close();

}

}

基本的增删改查操作集合

package hbasetest;

import java.io.IOException;

import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.HColumnDescriptor;

import org.apache.hadoop.hbase.HConstants;

import org.apache.hadoop.hbase.HTableDescriptor;

import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.hbase.client.Admin;

import org.apache.hadoop.hbase.client.Connection;

import org.apache.hadoop.hbase.client.ConnectionFactory;

import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;

//import org.apache.hadoop.hbase.client.Delete;

//import org.apache.hadoop.hbase.client.Get;

//import org.apache.hadoop.hbase.client.HBaseAdmin;

//import org.apache.hadoop.hbase.client.HTable;

//import org.apache.hadoop.hbase.client.Put;

//import org.apache.hadoop.hbase.client.Result;

//import org.apache.hadoop.hbase.client.ResultScanner;

//import org.apache.hadoop.hbase.client.Scan;

//import org.apache.hadoop.hbase.filter.Filter;

//import org.apache.hadoop.hbase.util.Bytes;

public class HelloHBase {

/**

* 检查mytable表是否存在,若存在则需要先删除

* @param admin

* @param table

* @throws IOException

*/

public static void createOrOvewrite(Admin admin, HTableDescriptor table)throws IOException{

if(admin.tableExists(table.getTableName())) {

admin.disableTable(table.getTableName());

admin.deleteTable(table.getTableName());

}

admin.createTable(table);

}

/**

* 建立mytable表

* @param config

* @throws IOException

*/

public static void createSchemaTables (Configuration config) throws IOException{

try(Connection connection=ConnectionFactory.createConnection(config);

Admin admin=connection.getAdmin()){

HTableDescriptor table=new HTableDescriptor(TableName.valueOf("mytable"));

table.addFamily(new HColumnDescriptor("mycf").setCompressionType(Algorithm.NONE));

System.out.print("Creating table.");

createOrOvewrite(admin,table);

System.out.println("Done.");

}

}

public static void modifySchema(Configuration config) throws IOException{

try(Connection connection=ConnectionFactory.createConnection(config);

Admin admin=connection.getAdmin()){

TableName tableName=TableName.valueOf("mytable");

if (!admin.tableExists(tableName)) {

System.out.println("Table does not exist.");

System.exit(-1);

}

//往mytable 里面添加newcf列族

HColumnDescriptor newColumn=new HColumnDescriptor("newcf");

newColumn.setCompactionCompressionType(Algorithm.GZ);

newColumn.setMaxVersions(HConstants.ALL_VERSIONS);

admin.addColumn(tableName, newColumn);

//获取表的定义

HTableDescriptor table=admin.getTableDescriptor(tableName);

//更新mycf这个列族

HColumnDescriptor mycf=new HColumnDescriptor("mycf");

newColumn.setCompactionCompressionType(Algorithm.GZ);

newColumn.setMaxVersions(HConstants.ALL_VERSIONS);

table.modifyFamily(mycf);

admin.modifyTable(tableName, table);

}

}

/**

* 删除表操作

* @param config

* @throws IOException

*/

public static void deleteSchema (Configuration config) throws IOException{

try(Connection connection=ConnectionFactory.createConnection(config);

Admin admin=connection.getAdmin()){

TableName tableName=TableName.valueOf("mytable");

//停用(disable)mytable

admin.disableTable(tableName);

//删除掉mycf列族

admin.deleteColumn(tableName, "mycf".getBytes("UTF-8"));

//删除mytable表(先停用再删除)

admin.deleteTable(tableName);

}

}

public static void main(String[] args) throws IOException, URISyntaxException {

Configuration config= HBaseConfiguration.create();

//添加必要的配置文件

config.addResource(new Path(ClassLoader.getSystemResource("hbase-site.xml").toURI()));

config.addResource(new Path(ClassLoader.getSystemResource("core-site.xml").toURI()));

config.set("hbase.zookeeper.quorum","47.106.221.38");

config.set("zookeeper.znode.parent", "/hbase");

config.set("hbase.client.retries.number", "3");

config.set("hbase.rpc.timeout", "2000");

config.set("hbase.client.operation.timeout", "3000");

config.set("hbase.client.scanner.timeout.period", "10000");

//建表

createSchemaTables(config);

//改表

modifySchema(config);

//删表

deleteSchema(config);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值