第一步:创建maven工程,导入jar包
clouderahttps://repository.cloudera.com/artifactory/cloudera-repos/
org.apache.hadoophadoop-client2.6.0-mr1-cdh5.14.0org.apache.hbasehbase-client1.2.0-cdh5.14.0org.apache.hbasehbase-server1.2.0-cdh5.14.0junitjunit4.12testorg.testngtestng6.14.3testorg.junit.jupiterjunit-jupiter-apiRELEASEcompile
org.apache.maven.pluginsmaven-compiler-plugin3.01.8UTF-8org.apache.maven.pluginsmaven-shade-plugin2.2packageshade*:*META-INF/*.SFMETA-INF/*.DSAMETA-INF/*/RSA
第二步:开发 javaAPI 操作 HBase 表数据 列族小于F2
//导的 jar包import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;import static org.apache.hadoop.hbase.filter.CompareFilter.CompareOp.*;public class HBaseAPI {//列族小于F2public static void FamilyFilter() throws IOException {//连接数据库Configuration conf=new Configuration();conf.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");Connection connection = ConnectionFactory.createConnection(conf);//读取表Table mytable = connection.getTable(TableName.valueOf("myuser"));//全表扫描Scan scan=new Scan();FamilyFilter familyFilter=new FamilyFilter(LESS,new BinaryComparator("f2".getBytes()));scan.setFilter(familyFilter);ResultScanner scanner = mytable.getScanner(scan);//result 是与一行数据(有多个列族,多个列)for (Result result : scanner) {System.out.println(Bytes.toString(result.getRow()));System.out.println(Bytes.toString(result.getValue("f2".getBytes(),"phone".getBytes())));}//关闭连接connection.close();}public static void main(String[] args) throws IOException{FamilyFilter();}
}