001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.client.example; 019 020import java.util.ArrayList; 021import java.util.List; 022import java.util.Map; 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.fs.Path; 025import org.apache.hadoop.hbase.HBaseConfiguration; 026import org.apache.hadoop.hbase.TableName; 027import org.apache.hadoop.hbase.client.Admin; 028import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 029import org.apache.hadoop.hbase.client.Connection; 030import org.apache.hadoop.hbase.client.ConnectionFactory; 031import org.apache.hadoop.hbase.client.Put; 032import org.apache.hadoop.hbase.client.Scan; 033import org.apache.hadoop.hbase.client.Table; 034import org.apache.hadoop.hbase.client.TableDescriptor; 035import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 036import org.apache.hadoop.hbase.coprocessor.Export; 037import org.apache.hadoop.hbase.util.Bytes; 038import org.apache.yetus.audience.InterfaceAudience; 039 040/** 041 * A simple example on how to use {@link org.apache.hadoop.hbase.coprocessor.Export}. 042 * <p> 043 * For the protocol buffer definition of the ExportService, see the source file located under 044 * hbase-endpoint/src/main/protobuf/Export.proto. 045 * </p> 046 */ 047@InterfaceAudience.Private 048public final class ExportEndpointExample { 049 050 public static void main(String[] args) throws Throwable { 051 int rowCount = 100; 052 byte[] family = Bytes.toBytes("family"); 053 Configuration conf = HBaseConfiguration.create(); 054 TableName tableName = TableName.valueOf("ExportEndpointExample"); 055 try (Connection con = ConnectionFactory.createConnection(conf); Admin admin = con.getAdmin()) { 056 TableDescriptor desc = TableDescriptorBuilder.newBuilder(tableName) 057 // MUST mount the export endpoint 058 .setCoprocessor(Export.class.getName()) 059 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)).build(); 060 admin.createTable(desc); 061 062 List<Put> puts = new ArrayList<>(rowCount); 063 for (int row = 0; row != rowCount; ++row) { 064 byte[] bs = Bytes.toBytes(row); 065 Put put = new Put(bs); 066 put.addColumn(family, bs, bs); 067 puts.add(put); 068 } 069 try (Table table = con.getTable(tableName)) { 070 table.put(puts); 071 } 072 073 Path output = new Path("/tmp/ExportEndpointExample_output"); 074 Scan scan = new Scan(); 075 Map<byte[], Export.Response> result = Export.run(conf, tableName, scan, output); 076 final long totalOutputRows = result.values().stream().mapToLong(v -> v.getRowCount()).sum(); 077 final long totalOutputCells = result.values().stream().mapToLong(v -> v.getCellCount()).sum(); 078 System.out.println("table:" + tableName); 079 System.out.println("output:" + output); 080 System.out.println("total rows:" + totalOutputRows); 081 System.out.println("total cells:" + totalOutputCells); 082 } 083 } 084 085 private ExportEndpointExample() { 086 } 087}