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.mapreduce.lib.db; 019 020import java.sql.PreparedStatement; 021import java.sql.ResultSet; 022import java.sql.SQLException; 023 024import org.apache.hadoop.classification.InterfaceAudience; 025import org.apache.hadoop.classification.InterfaceStability; 026import org.apache.hadoop.io.Writable; 027 028/** 029 * Objects that are read from/written to a database should implement 030 * <code>DBWritable</code>. DBWritable, is similar to {@link Writable} 031 * except that the {@link #write(PreparedStatement)} method takes a 032 * {@link PreparedStatement}, and {@link #readFields(ResultSet)} 033 * takes a {@link ResultSet}. 034 * <p> 035 * Implementations are responsible for writing the fields of the object 036 * to PreparedStatement, and reading the fields of the object from the 037 * ResultSet. 038 * 039 * <p>Example:</p> 040 * If we have the following table in the database : 041 * <pre> 042 * CREATE TABLE MyTable ( 043 * counter INTEGER NOT NULL, 044 * timestamp BIGINT NOT NULL, 045 * ); 046 * </pre> 047 * then we can read/write the tuples from/to the table with : 048 * <p><pre> 049 * public class MyWritable implements Writable, DBWritable { 050 * // Some data 051 * private int counter; 052 * private long timestamp; 053 * 054 * //Writable#write() implementation 055 * public void write(DataOutput out) throws IOException { 056 * out.writeInt(counter); 057 * out.writeLong(timestamp); 058 * } 059 * 060 * //Writable#readFields() implementation 061 * public void readFields(DataInput in) throws IOException { 062 * counter = in.readInt(); 063 * timestamp = in.readLong(); 064 * } 065 * 066 * public void write(PreparedStatement statement) throws SQLException { 067 * statement.setInt(1, counter); 068 * statement.setLong(2, timestamp); 069 * } 070 * 071 * public void readFields(ResultSet resultSet) throws SQLException { 072 * counter = resultSet.getInt(1); 073 * timestamp = resultSet.getLong(2); 074 * } 075 * } 076 * </pre></p> 077 */ 078@InterfaceAudience.Public 079@InterfaceStability.Stable 080public interface DBWritable { 081 082 /** 083 * Sets the fields of the object in the {@link PreparedStatement}. 084 * @param statement the statement that the fields are put into. 085 * @throws SQLException 086 */ 087 public void write(PreparedStatement statement) throws SQLException; 088 089 /** 090 * Reads the fields of the object from the {@link ResultSet}. 091 * @param resultSet the {@link ResultSet} to get the fields from. 092 * @throws SQLException 093 */ 094 public void readFields(ResultSet resultSet) throws SQLException ; 095}