我是靠谱客的博主 专注泥猴桃,最近开发中收集的这篇文章主要介绍HBase的java代码开发(获取name这个列),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

第一步:创建maven工程,导入jar包

<repositories>
    <repository>
        <id>cloudera</id>
        <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>2.6.0-mr1-cdh5.14.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-client</artifactId>
        <version>1.2.0-cdh5.14.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-server</artifactId>
        <version>1.2.0-cdh5.14.0</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.14.3</version>
        <scope>test</scope>
 
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>RELEASE</version>
        <scope>compile</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
                <!-- <verbal>true</verbal>-->
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*/RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

第二步:开发 javaAPI 操作 HBase 表数据  获取name这个列

//导的 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 {

 //获取name这个列
    public static void qualifierFilter() 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();

        QualifierFilter qualifierFilter=new QualifierFilter(NOT_EQUAL,new SubstringComparator("name"));
            scan.setFilter(qualifierFilter);


        ResultScanner scanner = mytable.getScanner(scan);
        //result  是与一行数据(有多个列族,多个列)
        for (Result result : scanner) {
            System.out.println(Bytes.toString(result.getRow()));
            System.out.println(Bytes.toString(result.getValue("f1".getBytes(),"name".getBytes())));
            System.out.println(Bytes.toString(result.getValue("f2".getBytes(),"phone".getBytes())));

        }
        //关闭连接
        connection.close();
    }

  public static void main(String[] args) throws  IOException{
 
     qualifierFilter();
    
    }
 
 
}

最后

以上就是专注泥猴桃为你收集整理的HBase的java代码开发(获取name这个列)的全部内容,希望文章能够帮你解决HBase的java代码开发(获取name这个列)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部