利用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");
}
}
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
- 利用JavaAPI对HBase操作
- 利用javaApI【eclipse】操作HBase时,出现异常的解决方案
- spark操作hbase的javaapi
- HBase Shell及JavaAPI操作
- Hbase调用JavaAPI实现批量导入操作
- Hbase-1.2.4 javaAPI操作总结
- HBase伪分布式部署及JavaAPI操作
- hbase的JavaAPI操作:连接、创建、删除
- 关于Hbase在javaAPI操作表的操作
- Hbase调用JavaAPI实现批量导入操作(应用)
- JavaAPI操作hbase遇到的java.lang.UnsupportedClassVersionError问题
- HBase的JavaAPI使用
- Hbase javaapi curd笔记
- Hbase的JavaAPI----HbaseDao
- HBase之JavaAPI
- javaApi调用Hbase 单机模式
- Hbase葱岭探秘--JavaApi实践
- Java对hbase的操作
- 覆铜规则
- AIDL需要注意的两点
- sqoop基本操作
- CodeForces 709B Checkpoints
- CodeForces 709C Letters Cyclic Shift
- 利用JavaAPI对HBase操作
- 关于远程调用协议的对比
- 转:机器人工程师学习计划(YY硕)(后悔自己没有早点看到强力推荐)
- 一起写RPC框架(四)RPC网络模块的搭建二 Netty的编码器和译码器
- CodeForces 708B Recover the String
- POJ 1037 A decorative fence DP
- PHPStorm 16.2 注册码-16年9月10日亲测可用
- Spark系列之数据倾斜:数据倾斜之痛
- redhat安装Oracle11g