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 的使用例: 注释:生成一个随机数,当条件符合任意一个时,执行所要执...