forked from larsgeorge/hbase-book
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27f0e3a
commit 2395114
Showing
8 changed files
with
325 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package admin; | ||
|
||
import java.io.IOException; | ||
import java.util.Random; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver; | ||
import org.apache.hadoop.hbase.coprocessor.ObserverContext; | ||
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment; | ||
import org.apache.hadoop.hbase.regionserver.HRegion; | ||
|
||
// cc DelayRegionCloseObserver Special test observer creating delays | ||
public class DelayRegionCloseObserver extends BaseRegionObserver { | ||
public static final Log LOG = LogFactory.getLog(HRegion.class); | ||
|
||
// vv DelayRegionCloseObserver | ||
private Random rnd = new Random(); | ||
|
||
@Override | ||
public void preClose(ObserverContext<RegionCoprocessorEnvironment> c, | ||
boolean abortRequested) throws IOException { | ||
try { | ||
long delay = rnd.nextInt(3); | ||
LOG.info("@@@ Delaying region " + | ||
c.getEnvironment().getRegion().getRegionNameAsString() + | ||
" for " + delay + " seconds..."); | ||
Thread.sleep(delay * 1000); | ||
} catch (InterruptedException ie) { | ||
LOG.error(ie); | ||
} | ||
} | ||
// ^^ DelayRegionCloseObserver | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package admin; | ||
|
||
import java.io.IOException; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.hbase.HBaseConfiguration; | ||
import org.apache.hadoop.hbase.HTableDescriptor; | ||
import org.apache.hadoop.hbase.client.Admin; | ||
import org.apache.hadoop.hbase.client.Connection; | ||
import org.apache.hadoop.hbase.client.ConnectionFactory; | ||
|
||
import util.HBaseHelper; | ||
|
||
// cc ListTablesExample2 Example listing the existing tables with patterns | ||
public class ListTablesExample2 { | ||
|
||
private static void print(HTableDescriptor[] descriptors) { | ||
for (HTableDescriptor htd : descriptors) { | ||
System.out.println(htd.getTableName()); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
public static void main(String[] args) | ||
throws IOException, InterruptedException { | ||
Configuration conf = HBaseConfiguration.create(); | ||
|
||
HBaseHelper helper = HBaseHelper.getHelper(conf); | ||
helper.dropNamespace("testspace1", true); | ||
helper.dropNamespace("testspace2", true); | ||
helper.dropTable("testtable3"); | ||
helper.createNamespace("testspace1"); | ||
helper.createNamespace("testspace2"); | ||
helper.createTable("testspace1:testtable1", "colfam1"); | ||
helper.createTable("testspace2:testtable2", "colfam1"); | ||
helper.createTable("testtable3", "colfam1"); | ||
|
||
Connection connection = ConnectionFactory.createConnection(conf); | ||
Admin admin = connection.getAdmin(); | ||
|
||
System.out.println("List: .*"); | ||
// vv ListTablesExample2 | ||
HTableDescriptor[] htds = admin.listTables(".*"); | ||
// ^^ ListTablesExample2 | ||
print(htds); | ||
System.out.println("List: .*, including system tables"); | ||
// vv ListTablesExample2 | ||
htds = admin.listTables(".*", true); | ||
// ^^ ListTablesExample2 | ||
print(htds); | ||
|
||
System.out.println("List: hbase:.*, including system tables"); | ||
// vv ListTablesExample2 | ||
htds = admin.listTables("hbase:.*", true); | ||
// ^^ ListTablesExample2 | ||
print(htds); | ||
|
||
System.out.println("List: def.*:.*, including system tables"); | ||
// vv ListTablesExample2 | ||
htds = admin.listTables("def.*:.*", true); | ||
// ^^ ListTablesExample2 | ||
print(htds); | ||
|
||
System.out.println("List: test.*"); | ||
// vv ListTablesExample2 | ||
htds = admin.listTables("test.*"); | ||
// ^^ ListTablesExample2 | ||
print(htds); | ||
|
||
System.out.println("List: .*2, using Pattern"); | ||
// vv ListTablesExample2 | ||
Pattern pattern = Pattern.compile(".*2"); | ||
htds = admin.listTables(pattern); | ||
// ^^ ListTablesExample2 | ||
print(htds); | ||
|
||
System.out.println("List by Namespace: testspace1"); | ||
// vv ListTablesExample2 | ||
htds = admin.listTableDescriptorsByNamespace("testspace1"); | ||
// ^^ ListTablesExample2 | ||
print(htds); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package admin; | ||
|
||
import java.io.IOException; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.hbase.HBaseConfiguration; | ||
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 util.HBaseHelper; | ||
|
||
// cc ListTablesExample3 Example listing the existing tables with patterns | ||
public class ListTablesExample3 { | ||
|
||
private static void print(TableName[] tableNames) { | ||
for (TableName name : tableNames) { | ||
System.out.println(name); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
public static void main(String[] args) | ||
throws IOException, InterruptedException { | ||
Configuration conf = HBaseConfiguration.create(); | ||
|
||
HBaseHelper helper = HBaseHelper.getHelper(conf); | ||
helper.dropNamespace("testspace1", true); | ||
helper.dropNamespace("testspace2", true); | ||
helper.dropTable("testtable3"); | ||
helper.createNamespace("testspace1"); | ||
helper.createNamespace("testspace2"); | ||
helper.createTable("testspace1:testtable1", "colfam1"); | ||
helper.createTable("testspace2:testtable2", "colfam1"); | ||
helper.createTable("testtable3", "colfam1"); | ||
|
||
Connection connection = ConnectionFactory.createConnection(conf); | ||
Admin admin = connection.getAdmin(); | ||
|
||
System.out.println("List: .*"); | ||
// vv ListTablesExample3 | ||
TableName[] names = admin.listTableNames(".*"); | ||
// ^^ ListTablesExample3 | ||
print(names); | ||
System.out.println("List: .*, including system tables"); | ||
// vv ListTablesExample3 | ||
names = admin.listTableNames(".*", true); | ||
// ^^ ListTablesExample3 | ||
print(names); | ||
|
||
System.out.println("List: hbase:.*, including system tables"); | ||
// vv ListTablesExample3 | ||
names = admin.listTableNames("hbase:.*", true); | ||
// ^^ ListTablesExample3 | ||
print(names); | ||
|
||
System.out.println("List: def.*:.*, including system tables"); | ||
// vv ListTablesExample3 | ||
names = admin.listTableNames("def.*:.*", true); | ||
// ^^ ListTablesExample3 | ||
print(names); | ||
|
||
System.out.println("List: test.*"); | ||
// vv ListTablesExample3 | ||
names = admin.listTableNames("test.*"); | ||
// ^^ ListTablesExample3 | ||
print(names); | ||
|
||
System.out.println("List: .*2, using Pattern"); | ||
// vv ListTablesExample3 | ||
Pattern pattern = Pattern.compile(".*2"); | ||
names = admin.listTableNames(pattern); | ||
// ^^ ListTablesExample3 | ||
print(names); | ||
|
||
System.out.println("List by Namespace: testspace1"); | ||
// vv ListTablesExample3 | ||
names = admin.listTableNamesByNamespace("testspace1"); | ||
// ^^ ListTablesExample3 | ||
print(names); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package admin; | ||
|
||
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.HRegionInfo; | ||
import org.apache.hadoop.hbase.HRegionLocation; | ||
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.client.RegionLocator; | ||
import org.apache.hadoop.hbase.util.Bytes; | ||
|
||
import util.HBaseHelper; | ||
|
||
// cc ServerAndRegionNameExample Shows the use of server and region names | ||
public class ServerAndRegionNameExample { | ||
|
||
public static void main(String[] args) throws IOException, InterruptedException { | ||
Configuration conf = HBaseConfiguration.create(); | ||
HBaseHelper helper = HBaseHelper.getHelper(conf); | ||
helper.dropTable("testtable"); | ||
Connection connection = ConnectionFactory.createConnection(conf); | ||
Admin admin = connection.getAdmin(); | ||
|
||
// vv ServerAndRegionNameExample | ||
TableName tableName = TableName.valueOf("testtable"); | ||
HColumnDescriptor coldef1 = new HColumnDescriptor("colfam1"); | ||
HTableDescriptor desc = new HTableDescriptor(tableName) | ||
.addFamily(coldef1) | ||
.setValue("Description", "Chapter 5 - ServerAndRegionNameExample"); | ||
byte[][] regions = new byte[][] { Bytes.toBytes("ABC"), | ||
Bytes.toBytes("DEF"), Bytes.toBytes("GHI"), Bytes.toBytes("KLM"), | ||
Bytes.toBytes("OPQ"), Bytes.toBytes("TUV") | ||
}; | ||
admin.createTable(desc, regions); | ||
|
||
RegionLocator locator = connection.getRegionLocator(tableName); | ||
HRegionLocation location = locator.getRegionLocation(Bytes.toBytes("Foo")); | ||
HRegionInfo info = location.getRegionInfo(); | ||
System.out.println("Region Name: " + info.getRegionNameAsString()); | ||
System.out.println("Server Name: " + location.getServerName()); | ||
|
||
locator.close(); | ||
connection.close(); | ||
// ^^ ServerAndRegionNameExample | ||
} | ||
} |
Oops, something went wrong.