一.使用java接口对hbase进行表的创建
1.引入需要的jar包
2.代码:

public static void main(String[] args) throws Exception {
//得到配置
Configuration conf= HBaseConfiguration.create();
//连接zookeeper,就可以对hbase进行操作
conf.set("hbase.zookeeper.quorum", "itcast04:2181,itcast05:2181,itcast06:2181");
//使用java接口创建表
HBaseAdmin admin=new HBaseAdmin(conf);
//指定表名
HTableDescriptor htd=new HTableDescriptor(TableName.valueOf("peoples"));
//添加列族(info,data)
HColumnDescriptor hcd_info=new HColumnDescriptor("info");
hcd_info.setMaxVersions(3);
HColumnDescriptor hcd_data=new HColumnDescriptor("data");
htd.addFamily(hcd_info);
htd.addFamily(hcd_data);
//创建表
admin.createTable(htd);
//关闭
admin.close();
}

二.使用java接口对hbase中的表进行crud操作

package cn.itcast.hbase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import org.apache.hadoop.conf.Configuration;
//import org.apache.hadoop.fs.shell.CopyCommands.Get;
import org.apache.hadoop.hbase.client.*;
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.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hdfs.DFSClient.Conf;
import org.junit.Before;
import org.junit.Test; import org.apache.hadoop.hbase.util.Bytes; public class HBaseDemo { private Configuration conf=null; //在所有方法之间初始化
@Before
public void init(){
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "itcast04:2181,itcast05:2181,itcast06:2181");
} //-------------一次插入一条数据------------------
//插入数据
@Test
public void testPut() throws Exception{
//得到一个表对象
HTable table =new HTable(conf, "peoples");
//得到一个Put对象
//将字符串转换为字符数组
Put put=new Put(Bytes.toBytes("kr0001"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("zhangsanfeng"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("300"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(3000));
//在表中放入put对象
table.put(put);
table.close();
} //插入100万条,速度会很快(服务器几秒,自己的电脑1分半)对比Oracle,mysql
//-------------一次插入海量数据------------------
@Test
public void testPutAll() throws IOException{ //HTablePool pool=new HTablePool(config,10);
HTable table =new HTable(conf, "peoples");
//得到list对象
List<Put> puts=new ArrayList<Put>(10000);
// //第一种方式
// //将put放入list中
// for(int i=1;i<=1000000;i++){
// Put put=new Put(Bytes.toBytes("kr"+i));
// put.add(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(""+i));
// puts.add(put);
// }
// //在表中放入List
// table.put(puts);
// table.close(); //第二种方式
for(int i=1;i<=1000000;i++){
Put put=new Put(Bytes.toBytes("kr"+i));
put.add(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(""+i));
puts.add(put);
//每隔1w条放一次
if(i%10000==0){
table.put(puts);
puts=new ArrayList<Put>(10000);//相当于清空
}
}
table.put(puts);
table.close(); }
//--------查询一个(不到1s)-----------------
@Test
public void testGet() throws IOException{
HTable table =new HTable(conf, "peoples");
Get get =new Get(Bytes.toBytes("kr999999"));
//传get对象
//返回result对象
Result result=table.get(get);
String r=Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("money")));
System.out.println(r);
table.close();
} //--------查询多个-----------------
@Test
public void testScan() throws IOException{
HTable table =new HTable(conf, "peoples");
//创建scan对象(按照字典顺序排[))
Scan scan=new Scan(Bytes.toBytes("kr299990"), Bytes.toBytes("kr300000"));
//返回结果集
ResultScanner scanner=table.getScanner(scan);
for(Result result:scanner){
String r=Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("money")));
System.out.println(r);
}
table.close();
}
//--------更新(put将老版本覆盖,查询最新的)----------------- //--------删除成功--------------------
@Test
public void testDel() throws IOException{
HTable table =new HTable(conf, "peoples");
//创建delete对象
Delete delete = new Delete(Bytes.toBytes("kr999999"));
table.delete(delete);
table.close();
} //---------创建表---------------------------
public static void main(String[] args) throws Exception { //使用java接口创建表
HBaseAdmin admin=new HBaseAdmin(conf);
//指定表名
HTableDescriptor htd=new HTableDescriptor(TableName.valueOf("peoples"));
//添加列族(info,data)
HColumnDescriptor hcd_info=new HColumnDescriptor("info");
hcd_info.setMaxVersions(3);
HColumnDescriptor hcd_data=new HColumnDescriptor("data");
htd.addFamily(hcd_info);
htd.addFamily(hcd_data);
//创建表
admin.createTable(htd);
//关闭
admin.close();
} }

最新文章

  1. YourSQLDba 配置&mdash;&mdash;修改备份路径
  2. hdu[1711]number sequence
  3. 第九篇:在SOUI中使用多语言翻译
  4. MVC4中重复使用JQuery Mobile Dialog的做法实践.
  5. JDBCTemplate基础学习
  6. 慎用preg_replace危险的/e修饰符(一句话后门常用)
  7. 【Objective-C】2.自定义构造方法和description方法
  8. ###STL学习--迭代器
  9. C# 正则获取html内容
  10. oracle 非数字型转数字型
  11. Redis 设计与实现 (九)--Lua
  12. centos下nginx的启动
  13. App.js实现使用js开发app的应用,此文是中文文档
  14. 20155234 昝昕明《基于ARM实验箱的国密算法应用》课程设计个人报告
  15. 【linux】vim/vi常用指令
  16. Trailing Zeroes (II) LightOJ - 1090(预处理+前缀和)
  17. 4946: [Noi2017]蔬菜
  18. Centos 虚拟机网络问题,网卡起不来,重启network服务失败
  19. resin3.X那些事之resin.conf
  20. Python数据结构,计算问题

热门文章

  1. Django day 37 网站视频的播放,购物车接口,优惠券表分析
  2. redis之简单动态字符串(SDS)
  3. ElementaryOS 0.4快速配置工具
  4. [转]Mysql之Union用法
  5. jquery滚轮事件
  6. javascirpt中的数字在计算机内存储为多少Byte
  7. tomcat日志详释
  8. Git——基本操作
  9. magento category Ids Name
  10. [Tensorflow] 使用 model.save_weights() 保存 Keras Subclassed Model