Java API 实现HBase的数据添加与过滤查询

java

包依赖比较麻烦,找了好久,我用的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 API 实现HBase的数据添加与过滤查询 的全部内容, 来源链接: utcz.com/z/391443.html

回到顶部