天天看點

hbase 修改表名_Hbase之修改表結構

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.Admin;

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

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

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

import org.apache.hadoop.hbase.util.Pair;

import java.io.IOException;

public class ModifyStructOfTable {

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

Configuration conf = HBaseConfiguration.create();

Connection connection = ConnectionFactory.createConnection(conf);

Admin admin = connection.getAdmin();

//

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

//列族

HColumnDescriptor coldef1 = new HColumnDescriptor("colfam1");

//表

HTableDescriptor desc = new HTableDescriptor(tableName).addFamily(coldef1).setValue("Description", "ModifyTableExample: Original Table");

//Create the table with the original structure and 50 regions.

admin.createTable(desc, Bytes.toBytes(1L),Bytes.toBytes(10000L),50);

//表描述

HTableDescriptor htd1 = admin.getTableDescriptor(tableName);

//列族2

HColumnDescriptor coldef2 = new HColumnDescriptor("colfam2");

//

htd1.addFamily(coldef2).setMaxFileSize(1024*1024*1024L).setValue("Description","ModifyTableExample: Modified Table");

admin.disableTable(tableName);

//更改表

admin.modifyTable(tableName,htd1);

Pair status = new Pair() {{

setFirst(50);

setSecond(50);

}};

for (int i = 0; status.getFirst() != 0 && i < 500; i++) {

status = admin.getAlterStatus(desc.getTableName());

if (status.getSecond() != 0) {

int pending = status.getSecond() - status.getFirst();

System.out.println(pending + " of " + status.getSecond() + " regions updated.");

try {

Thread.sleep(1 * 1000l);

}catch (Exception e){

System.out.println(e.getMessage());

}

} else {

System.out.println("All regions updated.");

break; }

}

if (status.getFirst() != 0) {

throw new IOException("Failed to update regions after 500 sec‐ onds.");

}

admin.enableTable(tableName);

HTableDescriptor htd2 = admin.getTableDescriptor(tableName);

System.out.println("Equals: " + htd1.equals(htd2));

System.out.println("New schema: " + htd2);

}

}

原文:http://www.cnblogs.com/similarface/p/5822798.html