package com.quyf;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; /**
* Created by Administrator on 2018/4/8.
*/ public class HbaseDemo { private static Connection conn;
private static Configuration configuration;
private static Admin admin;
public static void main(String[] args) throws Exception{
//System.out.println("hello world"); } @Before
public void setup() throws Exception{
configuration = new Configuration();
configuration.set("hbase.zookeeper.quorum", "bg1:2181,bg2:2181,bg3:2181");
// configuration.addResource(new Path("I:\\gitproject\\hbase_demo\\src\\main\\resources\\hbase-site.xml"));
conn = ConnectionFactory.createConnection(configuration);
admin = conn.getAdmin();
}
/**
* 结束之后 关闭对象
*/
@After
public void End_up() throws Exception {
if (conn != null) conn.close();
}
@Test
public void create() throws IOException {
String[] cols = {"phone","addr","age"};
createTable("helloworld", cols);
} /**
* @param tableName
* @param columns
* @throws IOException
*/
private void createTable(String tableName, String[] columns) throws IOException { TableName tb = TableName.valueOf( tableName );
if( admin.tableExists( tb )){
System.out.println("已经存在");
}else{
System.out.println("not存在"); HTableDescriptor desc = new HTableDescriptor( tb );
for (String column:columns) {
desc.addFamily( new HColumnDescriptor(column));
}
admin.createTable(desc);
System.out.println("create "+tableName+" success");
}
} @Test
public void showTable() throws IOException { TableName[] tables = admin.listTableNames();
for(TableName tab:tables){
System.out.println("表名:"+tab.getNameAsString());
HTableDescriptor desc = admin.getTableDescriptor( tab );
for(HColumnDescriptor column: desc.getColumnFamilies()){
System.out.println(column.getNameAsString()+"==="+column.toString());
}
}
} @Test
public void getByRowKey() throws IOException {
//admin.getTableDescriptor(TableName.valueOf("test"));
Table table = conn.getTable(TableName.valueOf("test"));
Get get = new Get("0004".getBytes());
Result rt = table.get(get);
System.out.println( rt.toString());
for(Cell cell:rt.rawCells()){
System.out.println(
Bytes.toString(CellUtil.cloneRow(cell))+"\t"+
Bytes.toString( CellUtil.cloneFamily(cell))+"\t"+
Bytes.toString(CellUtil.cloneQualifier(cell))+"\t"+
Bytes.toString( CellUtil.cloneValue(cell))
);
}
} @Test
public void put() throws IOException {
Table tab = conn.getTable(TableName.valueOf("test"));
Put put1 = new Put("0004".getBytes());
put1.addColumn("info".getBytes(),"age".getBytes(),Bytes.toBytes("4"));
//put1.addColumn("info".getBytes(),"username".getBytes(),Bytes.toBytes("hellooww")); Put put2 = new Put("0005".getBytes());
put2.addColumn("info".getBytes(),"age".getBytes(),Bytes.toBytes("45"));
// put2.addColumn("info".getBytes(),"username".getBytes(),Bytes.toBytes("alibaba")); List<Put> list = new ArrayList<Put>();
list.add(put1);
list.add(put2);
tab.put(list);
} @Test
public void deletByRowKey() throws IOException {
Table table = conn.getTable(TableName.valueOf("test"));
Delete delete = new Delete(Bytes.toBytes("0004"));
//如果age列有多个版本的话,这里只删除了最新的一个版本,其他版本的数据还在的,
// addColumn是删除某一个列簇里的最新时间戳版本。
// delete.addColumns是删除某个列簇里的所有时间戳版本。
//delete.addColumn("info".getBytes(),"age".getBytes()); //测试下删除不存在的列
// delete.addColumn("info".getBytes(),"hh".getBytes());
//测试下删除不存在的列簇 会报错 column family info22 does not exist in region test
delete.addColumn("info22".getBytes(),"hh".getBytes());
table.delete(delete);
table.close();
} @Test
public void scanTable() throws IOException {
Table table = conn.getTable(TableName.valueOf("test"));
Scan scan = new Scan();
//scan可以加很多条件和范围
scan.withStartRow("0002".getBytes());
//scan.withStopRow("0004".getBytes());//不包含末尾行
scan.withStopRow("0004".getBytes(),true);//包含末尾行
ResultScanner rtScan = table.getScanner( scan );
// Iterator itor = rtScan.iterator();
// while(itor.hasNext()){
// System.out.println( itor.next().toString());
// }
for (org.apache.hadoop.hbase.client.Result next = rtScan.next();next !=null;next = rtScan.next() ){
System.out.println(next.toString());
}
}
}

  

JAVA API操作hbase1.4.2的更多相关文章

  1. hive-通过Java API操作

    通过Java API操作hive,算是测试hive第三种对外接口 测试hive 服务启动 package org.admln.hive; import java.sql.SQLException; i ...

  2. hadoop2-HBase的Java API操作

    Hbase提供了丰富的Java API,以及线程池操作,下面我用线程池来展示一下使用Java API操作Hbase. 项目结构如下: 我使用的Hbase的版本是 hbase-0.98.9-hadoop ...

  3. 使用Java API操作HDFS文件系统

    使用Junit封装HFDS import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org ...

  4. Kafka系列三 java API操作

    使用java API操作kafka 1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs ...

  5. Hadoop之HDFS(三)HDFS的JAVA API操作

    HDFS的JAVA API操作 HDFS 在生产应用中主要是客户端的开发,其核心步骤是从 HDFS 提供的 api中构造一个 HDFS 的访问客户端对象,然后通过该客户端对象操作(增删改查)HDFS ...

  6. MongoDB Java API操作很全的整理

    MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写,一般生产上建议以共享分片的形式来部署. 但是MongoDB官方也提供了其它语言的客户端操作API.如下图所示: 提供了C.C++ ...

  7. zookeeper的java api操作

    zookeeper的java api操作 创建会话: Zookeeper(String connectString,int sessionTimeout,Watcher watcher) Zookee ...

  8. java api操作

    java api操作 导入开发包 将hbase安装包中lib下包导入java项目   创建表   Configuration conf = HBaseConfiguration.create(); c ...

  9. HDFS 05 - HDFS 常用的 Java API 操作

    目录 0 - 配置 Hadoop 环境(Windows系统) 1 - 导入 Maven 依赖 2 - 常用类介绍 3 - 常见 API 操作 3.1 获取文件系统(重要) 3.2 创建目录.写入文件 ...

随机推荐

  1. bash编程语法自我总结

    脚本2种执行方式: 1 直接执行,等于bash衍生一个子程序,当该子程序完成后,子程序内各项变量活动作不会传回父程序 2 利用source执行,直接在父程序中执行 X=/bin/xdo cmd 执行c ...

  2. 关于java和c++中布尔量的比较

    在c++中允许 bool 量和 int 整形常量相互转换,并且用cout<<true; 在控制台上可以输出为 1 int main(int argc, _TCHAR* argv[]) { ...

  3. [ Java学习基础 ] Java的对象容器 -- 集合

    当你有很多书时,你会考虑买一个书柜,将你的书分门别类摆放进入.使用了书柜不仅仅使房间变得整洁,也便于以后使用书时方便查找.在计算机中管理对象亦是如此,当获得多个对象后,也需要一个容器将它们管理起来,这 ...

  4. asp.net session分布式共享解决方案

    Session共享是分布式系统设计时必须考虑的一个重要的点.相比较java中的session共享解决方案,.net中的解决方案还是比较少,MemcachedSessionProvider类库是比较优秀 ...

  5. Java工程师成神之路思维导图

    前面看Hollis的微信公众号更新了Java工程师成神之路的文档,感觉里面的内容清晰.齐全,可以用来审视自己,也能够知道自己在那些方面可以继续前行,想着有时间把它画下来,画下来之后分享出来. 主要内容 ...

  6. 3 sum closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  7. java 远程调试方法

    http://wenku.baidu.com/link?url=5p3GZhPcfvM-VOzAFeCjbLeVv0OQrAGJh4HxirqImuK9VxPfmW243T_l5Plj6KdDZB1I ...

  8. MTCNN人脸检测 附完整C++代码

    人脸检测 识别一直是图像算法领域一个主流话题. 前年 SeetaFace 开源了人脸识别引擎,一度成为热门话题. 虽然后来SeetaFace 又放出来 2.0版本,但是,我说但是... 没有训练代码, ...

  9. Failed building wheel for scandir 解决方案

    unbuntu 16.04 运行 pip install jupyter --upgrade 的时候出现了下面的错误 Failed building wheel for scandir Running ...

  10. 深度学习之 TensorFlow(二):TensorFlow 基础知识

    1.TensorFlow 系统架构: 分为设备层和网络层.数据操作层.图计算层.API 层.应用层.其中设备层和网络层.数据操作层.图计算层是 TensorFlow 的核心层. 2.TensorFlo ...