1.version
2.table_help
3.whoami
创建表
import java.io.IOException;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.conf.Configuration;
public class CreateTable {
public static void main(String[] args) throws IOException {
// Instantiating configuration class
Configuration con = HBaseConfiguration.create();
// Instantiating HbaseAdmin class
HBaseAdmin admin = new HBaseAdmin(con);
// Instantiating table descriptor class
HTableDescriptor tableDescriptor = new
TableDescriptor(TableName.valueOf("emp"));
// Adding column families to table descriptor
tableDescriptor.addFamily(new HColumnDescriptor("personal"));
tableDescriptor.addFamily(new HColumnDescriptor("professional"));
// Execute the table through admin
admin.createTable(tableDescriptor);
System.out.println(" Table created ");
}
}
HBase列出表
list
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class ListTables {
public static void main(String args[])throws MasterNotRunningException, IOException{
// Instantiating a configuration class
Configuration conf = HBaseConfiguration.create();
// Instantiating HBaseAdmin class
HBaseAdmin admin = new HBaseAdmin(conf);
// Getting all the list of tables using HBaseAdmin object
HTableDescriptor[] tableDescriptor =admin.listTables();
// printing all the table names.
for (int i=0; i<tableDescriptor.length;i++ ){
System.out.println(tableDescriptor[i].getNameAsString());
}
}
}
HBase禁用表
is_disabled
is_disabled ‘table name’
disable_all ‘r.*’
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class DisableTable{
public static void main(String args[]) throws MasterNotRunningException, IOException{
// Instantiating configuration class
Configuration conf = HBaseConfiguration.create();
// Instantiating HBaseAdmin class
HBaseAdmin admin = new HBaseAdmin(conf);
// Verifying weather the table is disabled
Boolean bool = admin.isTableDisabled("emp");
System.out.println(bool);
// Disabling the table using HBaseAdmin object
if(!bool){
admin.disableTable("emp");
System.out.println("Table disabled");
}
}
}
HBase启用表
enable ‘emp’
scan ‘emp’
is_enabled ‘table name’
is_enabled ‘emp’
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class EnableTable{
public static void main(String args[]) throws MasterNotRunningException, IOException{
// Instantiating configuration class
Configuration conf = HBaseConfiguration.create();
// Instantiating HBaseAdmin class
HBaseAdmin admin = new HBaseAdmin(conf);
// Verifying weather the table is disabled
Boolean bool = admin.isTableEnabled("emp");
System.out.println(bool);
// Disabling the table using HBaseAdmin object
if(!bool){
admin.enableTable("emp");
System.out.println("Table Enabled");
}
}
}
HBase表描述和修改
describe ‘emp’
更改列族单元格的最大数目
单元的最大数目设置为5
alter ‘emp’, NAME => ‘personal data’, VERSIONS => 5
设置只读
alter ‘emp’, READONLY
删除表范围运算符
alter ‘t1’, METHOD => ‘table_att_unset’, NAME => ‘MAX_FILESIZE’
删除列族
使用alter删除列族的语法
alter ‘ table name ’, ‘delete’ => ‘ column family ’
alter 'employee','delete'=>'professional'
使用Java API添加一列族
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class AddColoumn{
public static void main(String args[]) throws MasterNotRunningException, IOException{
// Instantiating configuration class.
Configuration conf = HBaseConfiguration.create();
// Instantiating HBaseAdmin class.
HBaseAdmin admin = new HBaseAdmin(conf);
// Instantiating columnDescriptor class
HColumnDescriptor columnDescriptor = new HColumnDescriptor("contactDetails");
// Adding column family
admin.addColumn("employee", columnDescriptor);
System.out.println("coloumn added");
}
}
使用Java API删除列族
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class DeleteColoumn{
public static void main(String args[]) throws MasterNotRunningException, IOException{
// Instantiating configuration class.
Configuration conf = HBaseConfiguration.create();
// Instantiating HBaseAdmin class.
HBaseAdmin admin = new HBaseAdmin(conf);
// Deleting a column family
admin.deleteColumn("employee","contactDetails");
System.out.println("coloumn deleted");
}
}
HBase Exists
exists ‘student’
import java.io.IOException;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class TableExists{
public static void main(String args[])throws IOException{
// Instantiating configuration class
Configuration conf = HBaseConfiguration.create();
// Instantiating HBaseAdmin class
HBaseAdmin admin = new HBaseAdmin(conf);
// Verifying the existance of the table
boolean bool = admin.tableExists("emp");
System.out.println( bool);
}
}
HBase关闭
import java.io.IOException;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class ShutDownHbase{
public static void main(String args[])throws IOException {
// Instantiating configuration class
Configuration conf = HBaseConfiguration.create();
// Instantiating HBaseAdmin class
HBaseAdmin admin = new HBaseAdmin(conf);
// Shutting down HBase
System.out.println("Shutting down hbase");
admin.shutdown();
}
}
put
package com.hfqx.decodelpd.dao;
import com.hfqx.decodelpd.utils.HBaseUtil;
import com.stumbleupon.async.Deferred;
import org.apache.hadoop.conf.Configuration;
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.HBaseAdmin;
import org.hbase.async.HBaseClient;
import org.hbase.async.PutRequest;
import java.io.IOException;
import java.util.List;
/**
* description
*
* @author ${user}
* @Time 2019-06-21
*/
public class HbaseDao {
private static String hbaseip = null;
private static String zkhost = null;
private static String zkport = null;
private static HBaseClient hBaseClient = null;
private static Configuration config = null;
static {
hbaseip = HBaseUtil.getHbaseMaster();
zkhost = HBaseUtil.getZkhost();
zkport = HBaseUtil.getZkport();
config = HBaseConfiguration.create();// 配置
config.set("hbase.zookeeper.quorum", zkhost); // zookeeper地址
config.set("hbase.zookeeper.property.clientPort", zkport);// zookeeper端口
hBaseClient = new HBaseClient(hbaseip);
}
/**
* check table that is exists or not
* @param tablename
* @return
*/
public boolean checkTable(String tablename) {
HBaseAdmin admin = null; // hbase表管理
try {
admin = new HBaseAdmin(config);
String[]tablenames = admin.getTableNames();
for( String table1:tablenames){
if ( table1.equals(tablename)){
System.out.println("表已经存在");
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public boolean checkTable(HBaseAdmin admin, String tablename) {
try {
String[]tablenames = admin.getTableNames();
for( String table1:tablenames){
if ( table1.equals(tablename)){
System.out.println("表已经存在");
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* create table that is exists or not
* @param tablename
* @param columnfamily
* @return
*/
public boolean createTable(String tablename, String columnfamily) {
HBaseAdmin admin = null; // hbase表管理
try {
admin = new HBaseAdmin(config);
} catch (IOException e) {
e.printStackTrace();
}
if ( checkTable(admin, tablename) ) {
return true;
}
// 创建表描述类
TableName tableName = TableName.valueOf(tablename); // 表名称
HTableDescriptor desc = new HTableDescriptor(tableName);
HColumnDescriptor[] hColumnDescriptors = desc.getColumnFamilies();
System.out.println(hColumnDescriptors.toString());
// 创建列族的描述类
HColumnDescriptor family = new HColumnDescriptor(columnfamily); // 列族
// 将列族添加到表中
desc.addFamily(family);
// 创建表
try {
admin.createTable(desc); // 创建表
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("创建表成功!");
return true;
}
public Deferred<Object> Put(PutRequest putRequest) {
return hBaseClient.put(putRequest);
}
public void Put(List<PutRequest> putRequestList) {
for(PutRequest putRequest: putRequestList) {
try {
Put(putRequest).join();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
HbaseDao hbaseDao = new HbaseDao();
hbaseDao.createTable("QX_LPD","f");
}
}