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.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseClient {
public static void main(String[] args) throws Exception{
create();
insert();
get();
delete();
}
public static void create() throws Exception {
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
String tableName = "test-hbase";
String columnName = "info";
Admin admin = connection.getAdmin();
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
HColumnDescriptor columnDescriptor = new HColumnDescriptor(columnName);
tableDescriptor.addFamily(columnDescriptor);
// admin.addColumn(TableName.valueOf(tableName), columnDescriptor);
admin.createTable(tableDescriptor);
admin.close();
connection.close();
}
public static void insert() throws Exception{
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
String tableName = "test-hbase";
String columnName = "info";
String rowKey = "rk1";
String qulifier = "c1";
String value = "value1";
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowKey.getBytes());
put.addColumn(columnName.getBytes(), qulifier.getBytes(), value.getBytes());
table.put(put);
table.close();
connection.close();
}
public static void get() throws Exception{
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
String tableName = "test-hbase";
String columnName = "info";
String rowKey = "rk1";
String qualifier = "c1";
String value = "value1";
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
get.addColumn(columnName.getBytes(), qualifier.getBytes());
Result result = table.get(get);
String valueStr = Bytes.toString(result.getValue(columnName.getBytes(), qualifier.getBytes()));
System.out.println(valueStr);
table.close();
connection.close();
}
public static void delete() throws Exception{
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
String tableName = "test-hbase";
Admin admin = connection.getAdmin();
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
admin.close();
connection.close();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/140812.html