package top.gujm.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.HttpServerUtil;
import org.apache.velocity.runtime.directive.Foreach;
import java.io.IOException;
public class JavaApi {
public static Configuration conf = null;
static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.1.113");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("zookeeper.znode.parent", "/hbase");
}
public static void main(String[] args) throws IOException {
// System.out.println(existsTable("123"));
// createTable("zhangsan", "sf1", "sf2");
// for (int i = 0; i < 100; i++){
// insertData("zhangsan", String.valueOf(i), "sf1","name", String.valueOf(i));
// insertData("zhangsan", String.valueOf(i), "sf2","age", String.valueOf(i+500));
// }
scan("zhangsan");
}
/**
* 扫描整张表
* @param tableName
* @throws IOException
*/
public static void scan(String tableName) throws IOException {
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for(Result result : scanner){
System.out.print(Bytes.toString(result.getRow())+"\t");
Cell[] cells = result.rawCells();
int i = 0;
for (Cell cell : cells){
System.out.print(i==0?"":",");
System.out.print(Bytes.toString(CellUtil.cloneFamily(cell))+":"+Bytes.toString(CellUtil.cloneQualifier(cell))+"="+Bytes.toString(CellUtil.cloneValue(cell)));
i++;
}
System.out.println();
}
}
/**
* 插入数据
* @param tableName 表名
* @param rowkey 行键
* @param columnFamily 列簇
* @param column 列名
* @param value 值
*/
public static void insertData(String tableName, String rowkey, String columnFamily, String column, String value) throws IOException {
HTable table = new HTable(conf, tableName);
Put put = new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);
table.close();
}
/**
* 创建表
* @param tableName 表名
* @param columnFamilys 列族
* @throws IOException
*/
public static void createTable(String tableName, String... columnFamilys) throws IOException {
if(!existsTable(tableName)) {
Connection connection = null;
HBaseAdmin admin = null;
try {
connection = ConnectionFactory.createConnection(conf);
admin = (HBaseAdmin) connection.getAdmin();
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
for (String columnFamily : columnFamilys){
descriptor.addFamily(new HColumnDescriptor(columnFamily));
}
admin.createTable(descriptor);
} catch (IOException e) {
e.printStackTrace();
}finally {
close(connection, admin);
}
}else {
System.out.println("表已存在!");
}
}
/**
* 判断表是否存在
* @param tableName
* @return
* @throws IOException
*/
public static boolean existsTable(String tableName) throws IOException {
Connection connection = null;
HBaseAdmin admin = null;
try {
connection = ConnectionFactory.createConnection(conf);
admin = (HBaseAdmin) connection.getAdmin();
return admin.tableExists(tableName);
} catch (IOException e) {
e.printStackTrace();
}finally {
close(connection, admin);
}
return false;
}
/**
* 关闭资源
*/
public static void close(Connection connection, HBaseAdmin admin){
if(admin != null) {
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(connection != null) {
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
V、 HBase-JavaApi
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 一、添加用户 表格练习body部分: js部分: 效果图: 二、v-if/v-else/v-else-if:条件渲...
- 1.v-if v-else v-else if 的使用例: 注释:生成一个随机数,当条件符合任意一个时,执行所要执...