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.mapred; 019 020import java.io.DataInput; 021import java.io.DataOutput; 022import java.io.IOException; 023import java.util.Arrays; 024import org.apache.hadoop.hbase.HConstants; 025import org.apache.hadoop.hbase.TableName; 026import org.apache.hadoop.hbase.util.Bytes; 027import org.apache.hadoop.mapred.InputSplit; 028import org.apache.yetus.audience.InterfaceAudience; 029 030/** 031 * A table split corresponds to a key range [low, high) 032 */ 033@InterfaceAudience.Public 034public class TableSplit implements InputSplit, Comparable<TableSplit> { 035 private TableName m_tableName; 036 private byte[] m_startRow; 037 private byte[] m_endRow; 038 private String m_regionLocation; 039 040 /** default constructor */ 041 public TableSplit() { 042 this((TableName) null, HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, ""); 043 } 044 045 /** 046 * Constructor nnnn 047 */ 048 public TableSplit(TableName tableName, byte[] startRow, byte[] endRow, final String location) { 049 this.m_tableName = tableName; 050 this.m_startRow = startRow; 051 this.m_endRow = endRow; 052 this.m_regionLocation = location; 053 } 054 055 public TableSplit(byte[] tableName, byte[] startRow, byte[] endRow, final String location) { 056 this(TableName.valueOf(tableName), startRow, endRow, location); 057 } 058 059 /** Returns table name */ 060 public TableName getTable() { 061 return this.m_tableName; 062 } 063 064 /** Returns table name */ 065 public byte[] getTableName() { 066 return this.m_tableName.getName(); 067 } 068 069 /** Returns starting row key */ 070 public byte[] getStartRow() { 071 return this.m_startRow; 072 } 073 074 /** Returns end row key */ 075 public byte[] getEndRow() { 076 return this.m_endRow; 077 } 078 079 /** Returns the region's hostname */ 080 public String getRegionLocation() { 081 return this.m_regionLocation; 082 } 083 084 public String[] getLocations() { 085 return new String[] { this.m_regionLocation }; 086 } 087 088 public long getLength() { 089 // Not clear how to obtain this... seems to be used only for sorting splits 090 return 0; 091 } 092 093 public void readFields(DataInput in) throws IOException { 094 this.m_tableName = TableName.valueOf(Bytes.readByteArray(in)); 095 this.m_startRow = Bytes.readByteArray(in); 096 this.m_endRow = Bytes.readByteArray(in); 097 this.m_regionLocation = Bytes.toString(Bytes.readByteArray(in)); 098 } 099 100 public void write(DataOutput out) throws IOException { 101 Bytes.writeByteArray(out, this.m_tableName.getName()); 102 Bytes.writeByteArray(out, this.m_startRow); 103 Bytes.writeByteArray(out, this.m_endRow); 104 Bytes.writeByteArray(out, Bytes.toBytes(this.m_regionLocation)); 105 } 106 107 @Override 108 public String toString() { 109 StringBuilder sb = new StringBuilder(); 110 sb.append("HBase table split("); 111 sb.append("table name: ").append(m_tableName); 112 sb.append(", start row: ").append(Bytes.toStringBinary(m_startRow)); 113 sb.append(", end row: ").append(Bytes.toStringBinary(m_endRow)); 114 sb.append(", region location: ").append(m_regionLocation); 115 sb.append(")"); 116 return sb.toString(); 117 } 118 119 @Override 120 public int compareTo(TableSplit o) { 121 return Bytes.compareTo(getStartRow(), o.getStartRow()); 122 } 123 124 @Override 125 public boolean equals(Object o) { 126 if (o == null || !(o instanceof TableSplit)) { 127 return false; 128 } 129 TableSplit other = (TableSplit) o; 130 return m_tableName.equals(other.m_tableName) && Bytes.equals(m_startRow, other.m_startRow) 131 && Bytes.equals(m_endRow, other.m_endRow) && m_regionLocation.equals(other.m_regionLocation); 132 } 133 134 @Override 135 public int hashCode() { 136 int result = m_tableName != null ? m_tableName.hashCode() : 0; 137 result = 31 * result + Arrays.hashCode(m_startRow); 138 result = 31 * result + Arrays.hashCode(m_endRow); 139 result = 31 * result + (m_regionLocation != null ? m_regionLocation.hashCode() : 0); 140 return result; 141 } 142}