Mark——java查询hbase,https://blog.csdn.net/weixin_46408961/article/details/124224169
查询Hbase数据分为Get方式查询,Scan方式查询,Scan配合Filter过滤查询
01.Get方式查询
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; public class SelectGetHbaseData { public static void main(String[] args) throws Exception{ Configuration conf = new Configuration(); conf.set("hbase.zookeeper.quorum","192.168.1.10:2181"); Connection conn = ConnectionFactory.createConnection(conf); Table gadaite = conn.getTable(TableName.valueOf("Gadaite")); Result result = gadaite.get(new Get(Bytes.toBytes("002"))); Cell[] cells = result.rawCells(); for(Cell cell:cells){ System.out.println("INFO: "+cell); System.out.println(new String(CellUtil.cloneRow(cell))); // 行键 System.out.println(new String(CellUtil.cloneFamily(cell))); // 列簇 System.out.println(new String(CellUtil.cloneQualifier(cell))); // 字段 System.out.println(new String(CellUtil.cloneValue(cell))); // 值 System.out.println(cell.getTimestamp()); // 时间戳 } gadaite.close(); conn.close(); } }
输出结果:
02.使用Scan查询
下面查询全部,不进行过滤
import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; public class SelectScanAllHbaseData { public static void main(String[] args) throws Exception{ // 连接信息,连接对象 org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum","192.168.1.10:2181"); Connection conn = ConnectionFactory.createConnection(conf); Admin admin = conn.getAdmin(); // 连接Hbase数据库中的表 Table gadaite = conn.getTable(TableName.valueOf("Gadaite")); Scan scan = new Scan(); ResultScanner scanner = gadaite.getScanner(scan); // 读取Hbase表中的数据 for(Result sc:scanner){ // sc:对应cell集合,Hbase的一个列簇 for (Cell c:sc.rawCells()){ // c:sc的对象,对应一条数据 String rowkey = new String(CellUtil.cloneRow(c));//行键 String column = new String(CellUtil.cloneFamily(c));//列簇 String field = new String(CellUtil.cloneQualifier(c));//字段 String values = new String(CellUtil.cloneValue(c));//值 // String s = new String(CellUtil.copyRow(c));//行键 System.out.println(rowkey+" "+column+" "+field+" "+values); // System.out.println(s); } } } }
输出结果:
03.使用scan结合filter进行过滤查询
3.1 按照行键值进行过滤
关于CompareOperator.EQUAL方法类似的还有:
// LESS < // LESS_OR_EQUAL <= // EQUAL = // NOT_EQUAL <> // GREATER_OR_EQUAL >= // GREATER > // NO_OP
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.CompareOperator; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.RegexStringComparator; import org.apache.hadoop.hbase.filter.RowFilter; public class SelectScanRowkeyHbaseData { public static void main(String[] args) throws Exception{ Configuration conf = new Configuration(); conf.set("hbase.zookeeper.quorum","192.168.1.10"); Connection conn = ConnectionFactory.createConnection(conf); Table gadaite = conn.getTable(TableName.valueOf("Gadaite")); Scan scanrowkey = new Scan(); // 匹配以2结尾的行键 RowFilter rowFilter = new RowFilter(CompareOperator.EQUAL, new RegexStringComparator("2$")); scanrowkey.setFilter(rowFilter); ResultScanner scanner = gadaite.getScanner(scanrowkey); for(Result sc:scanner){ // sc:对应cell集合,Hbase的一个列簇 for (Cell c:sc.rawCells()){ // c:sc的对象,对应一条数据 String rowkey = new String(CellUtil.cloneRow(c));//行键 String column = new String(CellUtil.cloneFamily(c));//列簇 String field = new String(CellUtil.cloneQualifier(c));//字段 String values = new String(CellUtil.cloneValue(c));//值 // String s = new String(CellUtil.copyRow(c));//行键 System.out.println(rowkey+" "+column+" "+field+" "+values); // System.out.println(s); } } } }
输出结果:
3.2 按照列簇字段值进行过滤
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.CompareOperator; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.util.Bytes; public class SelectScanColumnHbaseData { public static void main(String[] args) throws Exception{ Configuration conf = new Configuration(); conf.set("hbase.zookeeper.quorum","192.168.1.10"); Connection conn = ConnectionFactory.createConnection(conf); Table gadaite = conn.getTable(TableName.valueOf("Gadaite")); Scan columnscan = new Scan(); // 这里如果为没有满足筛选条件的值的时候会将空值所在的数据进行返回 SingleColumnValueFilter columnfilter = new SingleColumnValueFilter(Bytes.toBytes("F2"), Bytes.toBytes("F21"), CompareOperator.EQUAL,Bytes.toBytes("G2.G21")); columnscan.setFilter(columnfilter); ResultScanner scanner = gadaite.getScanner(columnscan); for (Result sc:scanner){ for (Cell c:sc.rawCells()){ System.out.println(c); System.out.println(new String(CellUtil.cloneValue(c))); } } } }
输出结果:
3.3 只查询某个列簇下的数据
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.ColumnPrefixFilter; import org.apache.hadoop.hbase.util.Bytes; // 列名前缀过滤器 public class SelectColumnPrefixFilter { public static void main(String[] args) throws Exception{ Configuration conf = new Configuration(); conf.set("hbase.zookeeper.quorum","192.168.1.10:2181"); Connection conn = ConnectionFactory.createConnection(conf); Table gadaite = conn.getTable(TableName.valueOf("Gadaite")); Scan scan = new Scan(); // 值返回指定列簇的数据 ColumnPrefixFilter f2 = new ColumnPrefixFilter(Bytes.toBytes("F2")); scan.setFilter(f2); ResultScanner scanner = gadaite.getScanner(scan); for (Result sc:scanner){ for (Cell c:sc.rawCells()){ System.out.println(c); System.out.println(new String(CellUtil.cloneValue(c))); } } } }
输出结果:
如下为F2列簇
3.5 多条件集合查询
如下,只查询行键为002,且只查询F2列簇下的数据
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.CompareOperator; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.ColumnPrefixFilter; import org.apache.hadoop.hbase.filter.FilterList; import org.apache.hadoop.hbase.filter.RegexStringComparator; import org.apache.hadoop.hbase.filter.RowFilter; import org.apache.hadoop.hbase.util.Bytes; // 多条件过滤集合 public class SelectSetFilter { public static void main(String[] args) throws Exception{ Configuration conf = new Configuration(); conf.set("hbase.zookeeper.quorum","192.168.1.10"); Connection conn = ConnectionFactory.createConnection(conf); Table gadaite = conn.getTable(TableName.valueOf("Gadaite")); FilterList list = new FilterList(); Scan scan = new Scan(); RowFilter rowFilter = new RowFilter(CompareOperator.EQUAL, new RegexStringComparator("002")); ColumnPrefixFilter f2 = new ColumnPrefixFilter(Bytes.toBytes("F2")); list.addFilter(rowFilter); list.addFilter(f2); scan.setFilter(list); ResultScanner scanner = gadaite.getScanner(scan); for (Result sc:scanner){ for (Cell c:sc.rawCells()){ System.out.println(c); System.out.println(new String(CellUtil.cloneValue(c))); } } } }
输出结果:
标签:java,hadoop,new,查询,import,apache,org,hbase From: https://www.cnblogs.com/tianyun5115/p/17277204.html