El contenido no se encuentra disponible en el idioma seleccionado. Estamos trabajando continuamente para agregar más idiomas. Gracias por su apoyo.

Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
On this page

Flink HBase Sample Program (Java)

Updated on 2024-08-10 GMT+08:00

Description

Call Flink APIs to read data from and write data to HBase.

Sample Code

The following example shows the main logic code of WriteHBase and ReadHBase.

For details about the complete code, see com.huawei.bigdata.flink.examples.WriteHBase and com.huawei.bigdata.flink.examples.ReadHBase.

  • Main logic code of WriteHBase
    public static void main(String[] args) throws Exception {
            System.out.println("use command as: ");
            System.out.println(
                    "./bin/flink run --class com.huawei.bigdata.flink.examples.WriteHBase"
                            + " /opt/test.jar --tableName t1 --confDir /tmp/hbaseConf");
            System.out.println(
                    "******************************************************************************************");
            System.out.println("<tableName> hbase tableName");
            System.out.println("<confDir> hbase conf dir");
            System.out.println(
                    "******************************************************************************************");
            StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
            env.setParallelism(1);
            ParameterTool paraTool = ParameterTool.fromArgs(args);
            DataStream<Row> messageStream = env.addSource(new SimpleStringGenerator());
            messageStream.addSink(
                    new HBaseWriteSink(paraTool.get("tableName"), createConf(paraTool.get("confDir"))));
            env.execute("WriteHBase");
        }
    
        private static org.apache.hadoop.conf.Configuration createConf(String confDir) {
            LOG.info("Create HBase configuration.");
            org.apache.hadoop.conf.Configuration hbaseConf = HBaseConfigurationUtil.getHBaseConfiguration();
            if (confDir != null) {
                File hbaseSite = new File(confDir + File.separator + "hbase-site.xml");
                if (hbaseSite.exists()) {
                    LOG.info("Add hbase-site.xml");
                    hbaseConf.addResource(new Path(hbaseSite.getPath()));
                }
                File coreSite = new File(confDir + File.separator + "core-site.xml");
                if (coreSite.exists()) {
                    LOG.info("Add core-site.xml");
                    hbaseConf.addResource(new Path(coreSite.getPath()));
                }
                File hdfsSite = new File(confDir + File.separator + "hdfs-site.xml");
                if (hdfsSite.exists()) {
                    LOG.info("Add hdfs-site.xml");
                    hbaseConf.addResource(new Path(hdfsSite.getPath()));
                }
            }
            LOG.info("HBase configuration created successfully.");
            return hbaseConf;
        }
    
        /**
         * @since 8.2.0
         */
        private static class HBaseWriteSink extends RichSinkFunction<Row> {
            private Connection conn;
            private BufferedMutator bufferedMutator;
            private String tableName;
            private final byte[] serializedConfig;
            private Admin admin;
            private org.apache.hadoop.conf.Configuration hbaseConf;
            private long flushTimeIntervalMillis = 5000; //5s
            private long preFlushTime;
    
            public HBaseWriteSink(String sourceTable, org.apache.hadoop.conf.Configuration conf) {
                this.tableName = sourceTable;
                this.serializedConfig = HBaseConfigurationUtil.serializeConfiguration(conf);
            }
    
            private void deserializeConfiguration() {
                LOG.info("Deserialize HBase configuration.");
                hbaseConf = HBaseConfigurationUtil.deserializeConfiguration(
                    serializedConfig, HBaseConfigurationUtil.getHBaseConfiguration());
                LOG.info("Deserialization successfully.");
            }
    
            private void createTable() throws IOException {
                LOG.info("Create HBase Table.");
                if (admin.tableExists(TableName.valueOf(tableName))) {
                    LOG.info("Table already exists.");
                    return;
                }
                // Specify the table descriptor.
                TableDescriptorBuilder htd = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
                // Set the column family name to f1.
                ColumnFamilyDescriptorBuilder hcd = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("f1"));
                // Set data encoding methods. HBase provides DIFF,FAST_DIFF,PREFIX
                hcd.setDataBlockEncoding(DataBlockEncoding.FAST_DIFF);
                // Set compression methods, HBase provides two default compression
                // methods:GZ and SNAPPY
                hcd.setCompressionType(Compression.Algorithm.SNAPPY);
                htd.setColumnFamily(hcd.build());
                try {
                    admin.createTable(htd.build());
                } catch (IOException e) {
                    if (!(e instanceof TableExistsException)
                    || !admin.tableExists(TableName.valueOf(tableName))) {
                        throw e;
                    }
                    LOG.info("Table already exists, ignore.");
                }
                LOG.info("Table created successfully.");
            }
    
            @Override
            public void open(Configuration parameters) throws Exception {
                LOG.info("Write sink open");
                super.open(parameters);
                deserializeConfiguration();
                conn = ConnectionFactory.createConnection(hbaseConf);
                admin = conn.getAdmin();
                createTable();
                bufferedMutator = conn.getBufferedMutator(TableName.valueOf(tableName));
                preFlushTime = System.currentTimeMillis();
            }
    
            @Override
            public void close() throws Exception {
                LOG.info("Close HBase Connection.");
                try {
                    if (admin != null) {
                        admin.close();
                        admin = null;
                    }
                    if (bufferedMutator != null) {
                        bufferedMutator.close();
                        bufferedMutator = null;
                    }
                    if (conn != null) {
                        conn.close();
                        conn = null;
                    }
                } catch (IOException e) {
                    LOG.error("Close HBase Exception:", e);
                    throw new RuntimeException(e);
                }
                LOG.info("Close successfully.");
            }
    
            @Override
            public void invoke(Row value, Context context) throws Exception {
                LOG.info("Write data to HBase.");
                Put put = new Put(Bytes.toBytes(value.getField(0).toString()));
                put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("q1"), (Bytes.toBytes(value.getField(1).toString())));
                bufferedMutator.mutate(put);
    
                if (preFlushTime + flushTimeIntervalMillis >= System.currentTimeMillis()) {
                    LOG.info("Flush data to HBase.");
                    bufferedMutator.flush();
                    preFlushTime = System.currentTimeMillis();
                    LOG.info("Flush successfully.");
                } else {
                    LOG.info("Skip Flush.");
                }
    
                LOG.info("Write successfully.");
            }
        }
    
        /**
         * @since 8.2.0
         */
        public static class SimpleStringGenerator implements SourceFunction<Row> {
            private static final long serialVersionUID = 2174904787118597072L;
            boolean running = true;
            long i = 0;
            Random random = new Random();
    
            @Override
            public void run(SourceContext<Row> ctx) throws Exception {
                while (running) {
                    Row row = new Row(2);
                    row.setField(0, "rk" + random.nextLong());
                    row.setField(1, "v" + random.nextLong());
                    ctx.collect(row);
                    Thread.sleep(1000);
                }
            }
    
            @Override
            public void cancel() {
                running = false;
            }
        }
  • Main logic code of ReadHBase
        public static void main(String[] args) throws Exception {
            System.out.println("use command as: ");
            System.out.println(
                    "./bin/flink run --class com.huawei.bigdata.flink.examples.ReadHBase"
                            + " /opt/test.jar --tableName t1 --confDir /tmp/hbaseConf");
    
            System.out.println(
                    "******************************************************************************************");
            System.out.println("<tableName> hbase tableName");
            System.out.println("<confDir> hbase conf dir");
            System.out.println(
                    "******************************************************************************************");
            StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
            env.setParallelism(1);
            ParameterTool paraTool = ParameterTool.fromArgs(args);
            DataStream<Row> messageStream =
                    env.addSource(
                            new HBaseReaderSource(
                                    paraTool.get("tableName"), createConf(paraTool.get("confDir"))));
            messageStream
                    .rebalance()
                    .map(
                            new MapFunction<Row, String>() {
                                @Override
                                public String map(Row s) throws Exception {
                                    return "Flink says " + s + System.getProperty("line.separator");
                                }
                            })
                    .print();
            env.execute("ReadHBase");
        }
    
        private static org.apache.hadoop.conf.Configuration createConf(String confDir) {
            LOG.info("Create HBase configuration.");
            org.apache.hadoop.conf.Configuration hbaseConf = HBaseConfigurationUtil.getHBaseConfiguration();
            if (confDir != null) {
                File hbaseSite = new File(confDir + File.separator + "hbase-site.xml");
                if (hbaseSite.exists()) {
                    LOG.info("Add hbase-site.xml");
                    hbaseConf.addResource(new Path(hbaseSite.getPath()));
                }
                File coreSite = new File(confDir + File.separator + "core-site.xml");
                if (coreSite.exists()) {
                    LOG.info("Add core-site.xml");
                    hbaseConf.addResource(new Path(coreSite.getPath()));
                }
                File hdfsSite = new File(confDir + File.separator + "hdfs-site.xml");
                if (hdfsSite.exists()) {
                    LOG.info("Add hdfs-site.xml");
                    hbaseConf.addResource(new Path(hdfsSite.getPath()));
                }
            }
            LOG.info("HBase configuration created successfully.");
            return hbaseConf;
        }
    
        private static class HBaseReaderSource extends RichSourceFunction<Row> {
    
            private Connection conn;
            private Table table;
            private Scan scan;
            private String tableName;
            private final byte[] serializedConfig;
            private Admin admin;
            private org.apache.hadoop.conf.Configuration hbaseConf;
    
            public HBaseReaderSource(String sourceTable, org.apache.hadoop.conf.Configuration conf) {
                this.tableName = sourceTable;
                this.serializedConfig = HBaseConfigurationUtil.serializeConfiguration(conf);
            }
    
            @Override
            public void open(Configuration parameters) throws Exception {
                LOG.info("Read source open");
                super.open(parameters);
                deserializeConfiguration();
                conn = ConnectionFactory.createConnection(hbaseConf);
                admin = conn.getAdmin();
                if (!admin.tableExists(TableName.valueOf(tableName))) {
                    throw new IOException("table does not exist.");
                }
                table = conn.getTable(TableName.valueOf(tableName));
                scan = new Scan();
            }
    
            private void deserializeConfiguration() {
                LOG.info("Deserialize HBase configuration.");
                hbaseConf = HBaseConfigurationUtil.deserializeConfiguration(
                    serializedConfig, HBaseConfigurationUtil.getHBaseConfiguration());
                LOG.info("Deserialization successfully.");
            }
    
            @Override
            public void run(SourceContext<Row> sourceContext) throws Exception {
                LOG.info("Read source run");
                try (ResultScanner scanner = table.getScanner(scan)) {
                    Iterator<Result> iterator = scanner.iterator();
                    while (iterator.hasNext()) {
                        Result result = iterator.next();
                        String rowKey = Bytes.toString(result.getRow());
                        byte[] value = result.getValue(Bytes.toBytes("f1"), Bytes.toBytes("q1"));
                        Row row = new Row(2);
                        row.setField(0, rowKey);
                        row.setField(1, Bytes.toString(value));
                        sourceContext.collect(row);
                        LOG.info("Send data successfully.");
                    }
                }
    
                LOG.info("Read successfully.");
            }
    
            @Override
            public void close() throws Exception {
                closeHBase();
            }
    
            private void closeHBase() {
                LOG.info("Close HBase Connection.");
                try {
                    if (admin != null) {
                        admin.close();
                        admin = null;
                    }
                    if (table != null) {
                        table.close();
                        table = null;
                    }
                    if (conn != null) {
                        conn.close();
                        conn = null;
                    }
                } catch (IOException e) {
                    LOG.error("Close HBase Exception:", e);
                    throw new RuntimeException(e);
                }
                LOG.info("Close successfully.");
            }
    
            @Override
            public void cancel() {
                closeHBase();
            }
        }

Utilizamos cookies para mejorar nuestro sitio y tu experiencia. Al continuar navegando en nuestro sitio, tú aceptas nuestra política de cookies. Descubre más

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback