利用JavaAPI对HBase操作

来源:互联网 发布:郭天祥十天单片机pdf 编辑:程序博客网 时间:2024/06/06 02:10
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.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.util.Bytes;


public class HBaseJavaApi {


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


String tableName = "student";
String columnFamily = "cf";


//create(tableName, columnFamily);
//delete(tableName);
//put(tableName, "xiaoming", columnFamily, "age", "25");
//get(tableName, "xiaoming");
scan(tableName);
}


public static Configuration getConfiguration() {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
conf.set("hbase.zookeeper.quorum", "localhost");
return conf;
}


public static void create(String tableName, String columnFamily)
throws MasterNotRunningException, ZooKeeperConnectionException,
IOException {
HBaseAdmin hAdmin = new HBaseAdmin(getConfiguration());
if (hAdmin.tableExists(tableName)) {
System.err.println("table exists!");
} else {
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
hAdmin.createTable(tableDesc);
System.err.println("create table SUCCESS!");
}
}


public static void delete(String tableName) throws IOException {
HBaseAdmin hAdmin = new HBaseAdmin(getConfiguration());
if(hAdmin.tableExists(tableName)){
try {
hAdmin.disableTable(tableName);
hAdmin.deleteTable(tableName);
System.err.println("Delete table Success");
} catch (IOException e) {
System.err.println("Delete table Failed ");
}


}else{
System.err.println("table not exists");
}
}


public static void put(String tableName, String row, String columnFamily, String column, String data) throws IOException{
HTable table = new HTable(getConfiguration(), tableName);
Put put = new Put(Bytes.toBytes(row));
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));
table.put(put);
System.err.println("SUCCESS");
}


public static void get(String tableName, String row) throws IOException{
HTable table = new HTable(getConfiguration(), tableName);
Get get= new Get(Bytes.toBytes(row));
Result result = table.get(get);
byte[] value = result.getValue("cf".getBytes(), "age".getBytes());
System.err.println("SUCCESS");
System.err.println("Get:" + result + "\t" + new String(value));
}


public static void scan(String tableName) throws IOException{
HTable table = new HTable(getConfiguration(), tableName);


Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.err.println("Scan:" + result);
}
System.err.println("SUCCESS");
}
}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.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.util.Bytes;


public class HBaseJavaApi {


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


String tableName = "student";
String columnFamily = "cf";


create(tableName, columnFamily);
//delete(tableName);
//put(tableName, "xiaoming", columnFamily, "age", "25");
//get(tableName, "xiaoming");
scan(tableName);
}


public static Configuration getConfiguration() {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
conf.set("hbase.zookeeper.quorum", "localhost");
return conf;
}


public static void create(String tableName, String columnFamily)
throws MasterNotRunningException, ZooKeeperConnectionException,
IOException {
HBaseAdmin hAdmin = new HBaseAdmin(getConfiguration());
if (hAdmin.tableExists(tableName)) {
System.err.println("table exists!");
} else {
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
hAdmin.createTable(tableDesc);
System.err.println("create table SUCCESS!");
}
}


public static void delete(String tableName) throws IOException {
HBaseAdmin hAdmin = new HBaseAdmin(getConfiguration());
if(hAdmin.tableExists(tableName)){
try {
hAdmin.disableTable(tableName);
hAdmin.deleteTable(tableName);
System.err.println("Delete table Success");
} catch (IOException e) {
System.err.println("Delete table Failed ");
}


}else{
System.err.println("table not exists");
}
}


public static void put(String tableName, String row, String columnFamily, String column, String data) throws IOException{
HTable table = new HTable(getConfiguration(), tableName);
Put put = new Put(Bytes.toBytes(row));
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));
table.put(put);
System.err.println("SUCCESS");
}


public static void get(String tableName, String row) throws IOException{
HTable table = new HTable(getConfiguration(), tableName);
Get get= new Get(Bytes.toBytes(row));
Result result = table.get(get);
byte[] value = result.getValue("cf".getBytes(), "age".getBytes());
System.err.println("SUCCESS");
System.err.println("Get:" + result + "\t" + new String(value));
}


public static void scan(String tableName) throws IOException{
HTable table = new HTable(getConfiguration(), tableName);


Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.err.println("Scan:" + result);
}
System.err.println("SUCCESS");
}
}
0 0