zoukankan      html  css  js  c++  java
  • Java API 实现HBase的数据添加与过滤查询

    包依赖比较麻烦,找了好久,我用的CDH5.0 现将所依赖的包的列表清单如下:

    public class EmployeeDao {

    /**
    * @param args
    */
    public static Configuration configuration;
      static {
        configuration = new Configuration();
        String filePath = "hbase-site.xml";
        Path path = new Path(filePath);
        configuration.addResource(path);
        configuration = HBaseConfiguration.create(configuration);
      }
      //使用过滤器实现查询
      public List<Employee> QueryEmployeeByName(String name) throws Exception {
        String rowKey = name;
        HTable table = new HTable(configuration, "employee".getBytes());
        Scan scan = new Scan();
        // select ..... from .. where filter
        scan.addColumn("info".getBytes(), "age".getBytes());
        scan.addColumn("info".getBytes(), "sex".getBytes());
        RowFilter filter = new RowFilter(CompareOp.EQUAL,
        new RegexStringComparator(rowKey));
        scan.setFilter(filter);
        List<Employee> list = new ArrayList<Employee>();
        ResultScanner rScanner = table.getScanner(scan);

        for (Result rs : rScanner) {
          Employee e = new Employee();
          for (KeyValue kValue : rs.list()) {
            if ("sex".equalsIgnoreCase(new String(kValue.getQualifier()))) {
              e.setSex(new String(kValue.getValue()));
            }
            if ("age".equalsIgnoreCase(new String(kValue.getQualifier()))) {
              e.setAge(Integer.parseInt((new String(kValue.getValue()))));
            }
          }
          list.add(e);
        }
        return list;
      }
      // 插入一个单元格数据
      public static void insertOneRow(String tableName, String rowkey,
        String columnFamily, String column, String value) throws Exception {
        HTable table = new HTable(configuration, tableName);
        Put put = new Put(Bytes.toBytes(rowkey));
        put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),
        Bytes.toBytes(value));
        table.put(put);// 放入表
        table.close();// 释放资源
      }

      public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        EmployeeDao dao = new EmployeeDao();
        List<Employee> l = dao.QueryEmployeeByName("oftenlin");
        for (Employee e : l) {
          System.out.println("Name:oftenlin," + "Sex:" + e.getSex() + ",Age:"+ e.getAge());
        }
        //insertOneRow("employee","allen","info","scholl","shi yan mid school");
        insertOneRow("employee","gold fly","info","scholl","shi yan mid school");
        System.out.println("写入成功!");
      }

    }

    hbase-site.xml 的清单

    <?xml version="1.0" encoding="UTF-8"?>

    <!--Autogenerated by Cloudera Manager-->
    <configuration>
    <property>
    <name>hbase.rootdir</name>
    <value>hdfs://CentOS-cm:8020/hbase</value>
    </property>
    <property>
    <name>hbase.client.write.buffer</name>
    <value>2097152</value>
    </property>
    <property>
    <name>hbase.client.pause</name>
    <value>100</value>
    </property>
    <property>
    <name>hbase.client.retries.number</name>
    <value>35</value>
    </property>
    <property>
    <name>hbase.client.scanner.caching</name>
    <value>100</value>
    </property>
    <property>
    <name>hbase.client.keyvalue.maxsize</name>
    <value>10485760</value>
    </property>
    <property>
    <name>hbase.rpc.timeout</name>
    <value>60000</value>
    </property>
    <property>
    <name>hbase.snapshot.enabled</name>
    <value>true</value>
    </property>
    <property>
    <name>hbase.security.authentication</name>
    <value>simple</value>
    </property>
    <property>
    <name>zookeeper.session.timeout</name>
    <value>60000</value>
    </property>
    <property>
    <name>zookeeper.znode.parent</name>
    <value>/hbase</value>
    </property>
    <property>
    <name>zookeeper.znode.rootserver</name>
    <value>root-region-server</value>
    </property>
    <property>
    <name>hbase.zookeeper.quorum</name>
    <value>CentOS-server3,CentOS-server2,CentOS-server1</value>
    </property>
    <property>
    <name>hbase.zookeeper.property.clientPort</name>
    <value>2181</value>
    </property>
    </configuration>

  • 相关阅读:
    参考阿里规范,优秀的 Java 项目代码该如何分层?
    SpringBoot 中实现跨域的5种方式
    美团一面:你既然写过Mybatis插件,说说它底层是怎么加载一个自定义插件的
    陌陌面试官:说说Spring AOP 的原理、SpringMVC 的处理过程?
    这16条规范代码,同事,拍桌子 大喊 “666”
    微服务很简单,用一张架构图了解一下
    K8S部署Metrics-Server服务
    cookie
    html标签默认样式整理
    html 语义化标签
  • 原文地址:https://www.cnblogs.com/oftenlin/p/4043205.html
Copyright © 2011-2022 走看看