惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
H
Help Net Security
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
量子位
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
AI
AI
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
U
Unit 42
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
Schneier on Security
Schneier on Security
博客园 - Franky
H
Heimdal Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
N
News | PayPal Newsroom
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
雷峰网
雷峰网

博客园 - cndavy

如何在WINDOW环境下搭建ActivateMQ和zookeeper集群环境 Esp8266 例子 树莓派 安装 docker CSV 文件处理成 String[] ,网银下载的文本中使用逗号分割, 使用双引号标记字段. 使用String split 会出现把引号中的逗号识别的情况 例如 " ,,,, " , "aaa" 切换网卡的脚本 apache xampp 目录防止解析php FTPS 客户端 demo, WordPress搬家教程:换空间与换域名 sympy 的 符号计算的例子 Conda 简单使用 spoolsv.exe 无法启动 太阳高度角和方位角的计算 树莓派 3 alsa 声卡驱动 PHP 7 Xdebug 深深的坑 java 线性规划 和lingo 比较 Cannot find or open the PDB file问题的解决 Python Microsoft Visual C++ Compiler Package for Python 2.7 Node debug angular 调试 js (分 karms protractor / test e2e unit )
hbase scan 的例子
cndavy · 2016-02-15 · via 博客园 - cndavy

/**
 * Created by han on 2016/1/28.
 */
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class test {

    public static Configuration configuration;
    static {
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        configuration.set("hbase.zookeeper.quorum", "48.0.191.154:2181,48.0.191.154:2182,48.0.191.154:2183");
        configuration.set("hbase.master", "48.0.191.154:60000");
        System.setProperty("java.library.path","E:\\资料\\hadoop-2.6.2\\bin");
        System.setProperty("hadoop.home.dir", "E:\\资料\\hadoop-2.6.2");
        System.setProperty("PATH", "E:\\资料\\hadoop-2.6.2\\bin");

    }

    public static void main(String[] args) {
      //   createTable("h");
      //   insertData("h");
     //   QueryAll("h");
   //     QueryByCondition1("h");
       QueryByCondition2("h");
    //   QueryByCondition3("h");
      //  deleteRow("h","abcdef");
      //  deleteByCondition("h","abcdef");
      //  dropTable("h");
    }

    /**
     * 创建表
     * @param tableName
     */
    public static void createTable(String tableName) {
        System.out.println("start create table ......");
        try {
            HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
            if (hBaseAdmin.tableExists(tableName)) {// 如果存在要创建的表,那么先删除,再创建
                hBaseAdmin.disableTable(tableName);
                hBaseAdmin.deleteTable(tableName);
                System.out.println(tableName + " is exist,detele....");
            }
            HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
            tableDescriptor.addFamily(new HColumnDescriptor("column1"));
            tableDescriptor.addFamily(new HColumnDescriptor("column2"));
            tableDescriptor.addFamily(new HColumnDescriptor("column3"));
            tableDescriptor.addFamily(new HColumnDescriptor("cf"));
            hBaseAdmin.createTable(tableDescriptor);
        } catch (MasterNotRunningException e) {
            e.printStackTrace();
        } catch (ZooKeeperConnectionException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("end create table ......");
    }

    /**
     * 插入数据
     * @param tableName
     */
    public static void insertData(String tableName) {
        System.out.println("start insert data ......");
        HTablePool pool = new HTablePool(configuration, 1000);
        HTableInterface table =   pool.getTable(tableName);
        Put put = new Put("abcdef".getBytes());// 一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值
        put.add("column1".getBytes(), null, "aaa".getBytes());// 本行数据的第一列
        put.add("column2".getBytes(), null, "bbb".getBytes());// 本行数据的第三列
        put.add("column3".getBytes(), null, "ccc".getBytes());// 本行数据的第三列
        Put put1 = new Put("han_r1".getBytes());
        put1.addColumn("cf".getBytes(),"a".getBytes(),"han_cf_a".getBytes());

        try {
            table.put(put);
            table.put(put1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("end insert data ......");
    }

    /**
     * 删除一张表
     * @param tableName
     */
    public static void dropTable(String tableName) {
        try {
            HBaseAdmin admin = new HBaseAdmin(configuration);
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
        } catch (MasterNotRunningException e) {
            e.printStackTrace();
        } catch (ZooKeeperConnectionException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * 根据 rowkey删除一条记录
     * @param tablename
     * @param rowkey
     */
    public static void deleteRow(String tablename, String rowkey)  {
        try {
            HTable table = new HTable(configuration, tablename);
            List list = new ArrayList();
            Delete d1 = new Delete(rowkey.getBytes());
            list.add(d1);

            table.delete(list);
            System.out.println("删除行成功!");

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    /**
     * 组合条件删除
     * @param tablename
     * @param rowkey
     */
    public static void deleteByCondition(String tablename, String rowkey)  {
        //目前还没有发现有效的API能够实现 根据非rowkey的条件删除 这个功能能,还有清空表全部数据的API操作

    }


    /**
     * 查询所有数据
     * @param tableName
     */
    public static void QueryAll(String tableName) {
        HTablePool pool = new HTablePool(configuration, 1000);
        HTableInterface  table =  pool.getTable(tableName);
        try {
            ResultScanner rs = table.getScanner(new Scan());
            for (Result r : rs) {
                System.out.println("获得到rowkey:" + new String(r.getRow()));
                for (KeyValue keyValue : r.raw()) {
                    System.out.println("列:" + new String(keyValue.getFamily())
                                    +
                                    " :" + new String(keyValue.getQualifier())
                                    + "====值:" + new String(keyValue.getValue())
                                    + "===TIME===" + String.valueOf(keyValue.getTimestamp() )

                    );
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 单条件查询,根据rowkey查询唯一一条记录
     * @param tableName
     */
    public static void QueryByCondition1(String tableName) {
        System.out.println(" QueryByCondition1");
        HTablePool pool = new HTablePool(configuration, 1000);
        HTableInterface  table =   pool.getTable(tableName);
        try {
            Get scan = new Get("abcdef".getBytes());// 根据rowkey查询
            Result r = table.get(scan);
            System.out.println("获得到rowkey:" + new String(r.getRow()));
            for (KeyValue keyValue : r.raw()) {
                System.out.println("列:" + new String(keyValue.getFamily())
                                +
                                " :" + new String(keyValue.getQualifier())
                                + "====值:" + new String(keyValue.getValue())
                                + "===TIME===" + String.valueOf(keyValue.getTimestamp() )

                );
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 单条件按查询,查询多条记录
     * @param tableName
     */
    public static void QueryByCondition2(String tableName) {
        System.out.println(" QueryByCondition2");
        try {
            HTablePool pool = new HTablePool(configuration, 1000);
            HTableInterface  table =   pool.getTable(tableName);
            FilterList filterList=new FilterList();
            SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes
                    .toBytes("column1"), null, CompareOp.EQUAL, Bytes
                    .toBytes("aaa") ); // 当列column1的值为aaa时进行查询
             filter.setFilterIfMissing(true);
            filterList.addFilter(filter);
            Scan s = new Scan();
            s.setFilter(filterList);
            ResultScanner rs = table.getScanner(s);
            for (Result r : rs) {
                System.out.println("获得到rowkey:" + new String(r.getRow()));
                for (KeyValue keyValue : r.raw()) {
                    System.out.println("列:" + new String(keyValue.getFamily())
                            +
                            " :" + new String(keyValue.getQualifier())
                            + "====值:" + new String(keyValue.getValue())
                            + "===TIME===" + String.valueOf(keyValue.getTimestamp())

                     );
                }
            }
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 组合条件查询
     * @param tableName
     */
    public static void QueryByCondition3(String tableName) {
        System.out.println(" QueryByCondition3");
        try {
            HTablePool pool = new HTablePool(configuration, 1000);
            HTableInterface  table =   pool.getTable(tableName);

            List<Filter> filters = new ArrayList<Filter>();

            Filter filter1 = new SingleColumnValueFilter(Bytes
                    .toBytes("column1"), null, CompareOp.EQUAL, Bytes
                    .toBytes("aaa"));
            filters.add(filter1);

            Filter filter2 = new SingleColumnValueFilter(Bytes
                    .toBytes("column2"), null, CompareOp.EQUAL, Bytes
                    .toBytes("bbb"));
            filters.add(filter2);

            Filter filter3 = new SingleColumnValueFilter(Bytes
                    .toBytes("column3"), null, CompareOp.EQUAL, Bytes
                    .toBytes("ccc"));
            filters.add(filter3);

            FilterList filterList1 = new FilterList(filters);

            Scan scan = new Scan();
            scan.setFilter(filterList1);
            ResultScanner rs = table.getScanner(scan);
            for (Result r : rs) {
                System.out.println("获得到rowkey:" + new String(r.getRow()));
                for (KeyValue keyValue : r.raw()) {
                    System.out.println("列:" + new String(keyValue.getFamily())
                                    +
                                    " :" + new String(keyValue.getQualifier())
                                    + "====值:" + new String(keyValue.getValue())
                                    + "===TIME===" + String.valueOf(keyValue.getTimestamp())

                    );
                }
            }
            rs.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

View Code

根据 SingleColumnValueFilter 按照列和值查询,会把没有该列的行查询出来,加入
filter.setFilterIfMissing(true);
就可以了