hbase建表
import java.util.ArrayList; 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.KeyValue;
import org.apache.hadoop.hbase.client.Delete;
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; public class HBaseUtil {
private static HBaseConfiguration conf = null; static {
conf = new HBaseConfiguration();
//conf.set("hbase.master", "10.3.61.141:60000");
//conf.set("hbase.zookeeper.quorum", "hadoop141,hadoop142,hadoop143,hadoop144");
//conf.set("hbase.master.port", "60000");
conf.addResource("hbase-site.xml");
} // add
public void createTable(String tableName, String[] cfs) throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
if (admin.tableExists(tableName)) {
System.out.println("表已经存在了");
} else {
HTableDescriptor tableDesc = new HTableDescriptor(tableName);//表描述
for (int i = 0; i < cfs.length; i++) {
tableDesc.addFamily(new HColumnDescriptor(cfs[i]));//列族
}
admin.createTable(tableDesc);
System.out.println("表创建成功!");
}
} // delete table
public void deleteTable(String tableName) throws IOException {
try {
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println("删除表成功");
} catch (Exception e) {
e.printStackTrace();
}
} public void writeRow(String tableName, String[] cfs) {
try {
HTable table = new HTable(conf, tableName);
Put put = new Put(org.apache.hadoop.hbase.util.Bytes.toBytes(cfs[0]));
put.add(org.apache.hadoop.hbase.util.Bytes.toBytes(cfs[1]),
org.apache.hadoop.hbase.util.Bytes.toBytes(cfs[2]),
org.apache.hadoop.hbase.util.Bytes.toBytes(cfs[3]));
table.put(put);
} catch (Exception e) {
e.printStackTrace();
}
}
// delete value
public void deleteRow(String tableName, String rowKey) throws IOException {
HTable table = new HTable(conf, tableName);
java.util.List<Delete> list = new ArrayList<Delete>();
Delete dl = new Delete(rowKey.getBytes());
list.add(dl);
table.delete(list);
System.out.println("删除成功!");
} public void selectRow(String tableName, String rowKey) throws IOException {
HTable table = new HTable(conf, tableName);
Get g = new Get(rowKey.getBytes());
Result rs = table.get(g);
for (KeyValue kv : rs.raw()) {
System.out.println(new String(kv.getRow()) + " ");
System.out.println(new String(kv.getFamily()) + ":");
System.out.println(new String(kv.getQualifier()) + " ");
System.out.println(kv.getTimestamp() + " ");
System.out.println(new String(kv.getValue()) + " ");
}
} public void scaner(String tableName) {
try {
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
ResultScanner rs = table.getScanner(scan); for (Result r : rs) {
KeyValue[] kv = r.raw();
for (int i = 0; i < kv.length; i++) {
System.out.print(new String(kv[i].getRow()) + " ");
System.out.print(new String(kv[i].getFamily()) + ": ");
System.out.print(new String(kv[i].getQualifier()) + " ");
System.out.print(kv[i].getTimestamp() + " ");
System.out.println(new String(kv[i].getValue()));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.HBaseTest.java
package cn.netcenter.test; import java.io.IOException; import cn.netcenter.hbase.HBaseUtil; public class HBaseTest { public static void main(String[] args) throws IOException {
//createTable();
getRow();
//put(); } public static void createTable() throws IOException{
HBaseUtil util=new HBaseUtil();
String[] cfs={"grade","course"};
util.createTable("score", cfs);
} public static void put() throws IOException{
HBaseUtil util=new HBaseUtil();
//String[] cfs={"zkb","grade","","5"};
String[] cfs={"baoniu","course","art","80"}; util.writeRow("scores", cfs); } public static void scaner()throws IOException{
HBaseUtil util=new HBaseUtil();
util.scaner("bio");
} public static void getRow()throws IOException{
HBaseUtil util=new HBaseUtil();
util.selectRow("scores", "baoniu");
}
public static void deleteTable()throws IOException{
HBaseUtil util=new HBaseUtil();
util.deleteTable("score");
}
}
hbase建表的更多相关文章
- Hbase(二)hbase建表
一.建表高级属性 下面几个 shell 命令在 hbase 操作中可以起到很到的作用,且主要体现在建表的过程中,看 下面几个 create 属性 1.bloomfilter 布隆过滤器 默认是 NON ...
- Hbase建表时遇到的问题This could be a sign that the server has too many connections
Hbase创建表时遇到以下错误: ERROR: org.apache.hadoop.hbase.ZooKeeperConnectionException: HBase is able to conne ...
- HBase 建表新增数据记录
login as: root root@192.168.12.23's password: ********* Last login: Wed Aug 20 00:41:17 2014 from 19 ...
- Hbase 建表基本命令总结
访问hbase,以及操作hbase,命令不用使用分号 hbase shell 进入hbase list 查看表 hbase shell -d hbase(main):024:0> scan '. ...
- hbase建表时 ERROR: java.io.IOException: Table Namespace Manager not ready yet, try again later
其实解决不难,是因为时钟不同步,把每个节点切换到root用户下同步时钟就好了,在重启hbase!
- 【hbase】——Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...
- (转)Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...
- Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...
- hbase操作(shell 命令,如建表,清空表,增删改查)以及 hbase表存储结构和原理
两篇讲的不错文章 http://www.cnblogs.com/nexiyi/p/hbase_shell.html http://blog.csdn.net/u010967382/article/de ...
随机推荐
- 转自知乎(JAVA后台开发可以纯粹用JAVA SE吗?)
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:巴多崽链接:http://www.zhihu.com/question/29663744/answer/45154839来源: ...
- Linux安装pytorch的具体过程以及其中出现问题的解决办法
1.安装Anaconda 安装步骤参考了官网的说明:https://docs.anaconda.com/anaconda/install/linux.html 具体步骤如下: 首先,在官网下载地址 h ...
- 基于NIO的Socket通信
一.NIO模式的基本原理: 服务端: 首先,服务端打开一个通道(ServerSocketChannel),并向通道中注册一个通道调度器(Selector):然后向通道调度器注册感兴趣的事件Select ...
- 设置MySQL最大连接数
<pre name="code" class="sql">在使用MySQL数据库的时候,经常会遇到这么一个问题,就是"Can not co ...
- Echarts数据可视化visualMap,开发全解+完美注释
全栈工程师开发手册 (作者:栾鹏) Echarts数据可视化开发代码注释全解 Echarts数据可视化开发参数配置全解 6大公共组件详解(点击进入): title详解. tooltip详解.toolb ...
- SSM框架整合项目 :租房管理系统
使用ssm框架整合,oracle数据库 框架: Spring SpringMVC MyBatis 导包: 1, spring 2, MyBatis 3, mybatis-spring 4, fastj ...
- mac idea sbt工程打jar包
1.首先保证sbt已下载,否则下载homebrew:在命令行输入/usr/bin/ruby XXX ->下载完成后在终端输入brew install sbt ->安装完毕后可以打jar包 ...
- 用FastDFS一步步搭建文件管理系统
一.FastDFS介绍 FastDFS开源地址:https://github.com/happyfish100 参考:分布式文件系统FastDFS设计原理 参考:FastDFS分布式文件系统 个人封装 ...
- cf290-2015-2-3总结与反思(dfs判断无向图是否有环)
bool dfs(int i,int pre) { visit[i]=true; ;j<=v;j++) if(g[i][j]) { if(!visit[j]) return dfs(j,i); ...
- python第五课——自定义线程池
内容概要: 1.low版线程池 2.绝版线程池 1.low版线程池 设计思路:运用队列queue 将线程类名放入队列中,执行一个就拿一个出来 import queue import threading ...