Created
January 25, 2011 02:07
-
-
Save geoffeg/794395 to your computer and use it in GitHub Desktop.
HBase Hadoop ExampleClient
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
import java.io.*; | |
import org.apache.hadoop.conf.*; | |
import org.apache.hadoop.hbase.*; | |
import org.apache.hadoop.hbase.client.*; | |
import org.apache.hadoop.hbase.util.*; | |
public class ExampleClient { | |
public static void main(String[] args) throws IOException { | |
Configuration config = HBaseConfiguration.create(); | |
// Create table | |
HBaseAdmin admin = new HBaseAdmin(config); | |
HTableDescriptor htd = new HTableDescriptor("test"); | |
HColumnDescriptor hcd = new HColumnDescriptor("data"); | |
htd.addFamily(hcd); | |
admin.createTable(htd); | |
byte [] tablename = htd.getName(); | |
HTableDescriptor [] tables = admin.listTables(); | |
if (tables.length != 1 && Bytes.equals(tablename, tables[0].getName())) { | |
throw new IOException("Failed create of table"); | |
} | |
// Run some operations -- a put, a get, and a scan -- against the table. | |
HTable table = new HTable(config, tablename); | |
byte [] row1 = Bytes.toBytes("row1"); | |
Put p1 = new Put(row1); | |
byte [] databytes = Bytes.toBytes("data"); | |
p1.add(databytes, Bytes.toBytes("1"), Bytes.toBytes("value1")); | |
table.put(p1); | |
Get g = new Get(row1); | |
Result result = table.get(g); | |
System.out.println("Get: " + result); | |
Scan scan = new Scan(); | |
ResultScanner scanner = table.getScanner(scan); | |
try { | |
for (Result scannerResult: scanner) { | |
System.out.println("Scan: " + scannerResult); | |
} | |
} finally { | |
scanner.close(); | |
} | |
// Drop the table | |
admin.disableTable(tablename); | |
admin.deleteTable(tablename); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment