1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| package twitBase;
import java.io.IOException; import java.nio.ByteBuffer; import java.util.ArrayList;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.client.TableDescriptor; import org.apache.hadoop.hbase.client.TableDescriptorBuilder; import org.apache.hadoop.hbase.util.Bytes;
public class Twit { private static Connection connection = null; private static Admin admin = null; private static final String TABLE_NAME = "follows"; static { try {
Configuration configuration = new Configuration(); configuration.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03"); connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } } public static void close() { if(connection!=null) try { connection.close(); } catch (IOException e) { e.printStackTrace(); } if(admin!=null) { admin.close(); } } public static void CreateTable(String tableName,String...cfs ) throws IOException { if(admin.tableExists(TableName.valueOf(tableName))) { System.out.println(tableName+"表已存在"); return ; } if(cfs.length<=0) { System.out.println("请输入列族"); return ; } TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName)); for(String cf:cfs) { ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf)).build(); tableDescriptor.setColumnFamily(family); admin.createTable(tableDescriptor.build()); }
} public static void putData(String tableName,String rowKey,String cf,String cn,String value) throws IOException { if(!admin.tableExists(TableName.valueOf(tableName))) { System.out.println(tableName+"表不存在"); return ; } Table table = connection.getTable(TableName.valueOf(tableName)); Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value)); table.put(put); } public static ArrayList<String> findWho(String tableName,String name) throws IOException { ArrayList<String> ans = new ArrayList<String>(); Table table = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(name)); Result result = table.get(get); for(Cell re:result.rawCells()) { ans.add(Bytes.toString(CellUtil.cloneValue(re))); } return ans; } public static void main(String[] args) throws IOException { CreateTable("follows","follows"); putData("follows", "TheFakeMT", "follows", "1", "TheRealMT"); putData("follows", "TheFakeMT", "follows", "2", "MTFanBoy"); putData("follows", "TheFakeMT", "follows", "3", "Olivia"); putData("follows", "TheFakeMT", "follows", "4", "HRogers"); putData("follows", "TheRealMT", "follows", "1", "HRogers"); putData("follows", "TheRealMT", "follows", "2", "Olivia"); CreateTable("followed", "followed"); putData("followed", "TheRealMT", "followed", "1", "TheFakeMT"); putData("followed", "MTFanBoy", "followed", "1", "TheFakeMT"); putData("followed", "Olivia", "followed", "1", "TheFakeMT"); putData("followed", "HRogers", "followed", "1", "TheFakeMT"); putData("followed", "HRogers", "followed", "2", "TheRealMT"); putData("followed", "Olivia", "followed", "2", "TheRealMT"); boolean f1 = false,f2 = false; ArrayList<String> ans = findWho("follows","TheFakeMT"); if(ans.indexOf("TheRealMT")!=-1) { f1 = true; } if(ans.size()!=0) { System.out.println("TheFakeMT关注了:"); for(String name:ans) { System.out.println(name); } } else { System.out.println("TheFakeMT没有关注任何人"); } System.out.println("==================================================="); if(f1) { System.out.println("TheFakeMT关注了TheRealMT"); } else { System.out.println("TheFakeMT没有关注TheRealMT"); } System.out.println("==================================================="); ans = findWho("followed","TheFakeMT"); if(ans.indexOf("TheRealMT")!=-1) f2 = true; if(ans.size()!=0) { for(String name:ans) { System.out.println(name); } System.out.println("关注了TheFakeMT."); } else { System.out.println("没有人关注TheFakeMT."); } System.out.println("==================================================="); if(f2) { System.out.println("TheRealMT关注了TheFakeMT"); } else { System.out.println("TheRealMT没有关注TheFakeMT"); } System.out.println("==================================================="); } }
|