Package com.facebook.thrift.transport

Examples of com.facebook.thrift.transport.TTransport


        databasename = parts[1];
        port = Integer.parseInt(hostport[1]);
      }
      catch (Exception e) {
      }
      TTransport transport = new TSocket(host, port);
      TProtocol protocol = new TBinaryProtocol(transport);
      client = new HiveClient(protocol);
      transport.open();
    }
  }
View Full Code Here


    }
  }
 
  private void run() throws IOError, TException, NotFound, IllegalArgument,
      AlreadyExists {
    TTransport transport = new TSocket("localhost", port);
    TProtocol protocol = new TBinaryProtocol(transport, true, true);
    Hbase.Client client = new Hbase.Client(protocol);

    transport.open();

    byte[] t = bytes("demo_table");
   
    //
    // Scan all tables, look for the demo table and delete it.
    //
    System.out.println("scanning tables...");
    for (byte[] name : client.getTableNames()) {
      System.out.println("  found: " + utf8(name));
      if (utf8(name).equals(utf8(t))) {
        System.out.println("    deleting table: " + utf8(name))
        client.deleteTable(name);
      }
    }
   
    //
    // Create the demo table with two column families, entry: and unused:
    //
    ArrayList<ColumnDescriptor> columns = new ArrayList<ColumnDescriptor>();
    ColumnDescriptor col = null;
    col = new ColumnDescriptor();
    col.name = bytes("entry:");
    col.maxVersions = 10;
    columns.add(col);
    col = new ColumnDescriptor();
    col.name = bytes("unused:");
    columns.add(col);

    System.out.println("creating table: " + utf8(t));
    try {
      client.createTable(t, columns);
    } catch (AlreadyExists ae) {
      System.out.println("WARN: " + ae.message);
    }
   
    System.out.println("column families in " + utf8(t) + ": ");
    AbstractMap<byte[], ColumnDescriptor> columnMap = client.getColumnDescriptors(t);
    for (ColumnDescriptor col2 : columnMap.values()) {
      System.out.println("  column: " + utf8(col2.name) + ", maxVer: " + Integer.toString(col2.maxVersions));
    }
   
    //
    // Test UTF-8 handling
    //
    byte[] invalid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xfc, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1 };
    byte[] valid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xE7, (byte) 0x94, (byte) 0x9F, (byte) 0xE3, (byte) 0x83, (byte) 0x93, (byte) 0xE3, (byte) 0x83, (byte) 0xBC, (byte) 0xE3, (byte) 0x83, (byte) 0xAB};

    // non-utf8 is fine for data
    client.put(t, bytes("foo"), bytes("entry:foo"), invalid);

    // try empty strings
    client.put(t, bytes(""), bytes("entry:"), bytes(""));
   
    // this row name is valid utf8
    client.put(t, valid, bytes("entry:foo"), valid);
   
    // non-utf8 is not allowed in row names
    try {
      client.put(t, invalid, bytes("entry:foo"), invalid);
      System.out.println("FATAL: shouldn't get here");
      System.exit(-1);
    } catch (IOError e) {
      System.out.println("expected error: " + e.message);
    }
   
    // Run a scanner on the rows we just created
    ArrayList<byte[]> columnNames = new ArrayList<byte[]>();
    columnNames.add(bytes("entry:"));
   
    System.out.println("Starting scanner...");
    int scanner = client
        .scannerOpen(t, bytes(""), columnNames);
    try {
      while (true) {
        ScanEntry value = client.scannerGet(scanner);
        printEntry(value);
      }
    } catch (NotFound nf) {
      client.scannerClose(scanner);
      System.out.println("Scanner finished");
    }
   
    //
    // Run some operations on a bunch of rows
    //
    for (int i = 100; i >= 0; --i) {
      // format row keys as "00000" to "00100"
      NumberFormat nf = NumberFormat.getInstance();
      nf.setMinimumIntegerDigits(5);
      nf.setGroupingUsed(false);
      byte[] row = bytes(nf.format(i));
     
      client.put(t, row, bytes("unused:"), bytes("DELETE_ME"));
      printRow(row, client.getRow(t, row));
      client.deleteAllRow(t, row);

      client.put(t, row, bytes("entry:num"), bytes("0"));
      client.put(t, row, bytes("entry:foo"), bytes("FOO"));
      printRow(row, client.getRow(t, row));

      Mutation m = null;     
      ArrayList<Mutation> mutations = new ArrayList<Mutation>();
      m = new Mutation();
      m.column = bytes("entry:foo");
      m.isDelete = true;
      mutations.add(m);
      m = new Mutation();
      m.column = bytes("entry:num");
      m.value = bytes("-1");
      mutations.add(m);
      client.mutateRow(t, row, mutations);
      printRow(row, client.getRow(t, row));
     
      client.put(t, row, bytes("entry:num"), bytes(Integer.toString(i)));
      client.put(t, row, bytes("entry:sqr"), bytes(Integer.toString(i * i)));
      printRow(row, client.getRow(t, row));

      // sleep to force later timestamp
      try {
        Thread.sleep(50);
      } catch (InterruptedException e) {
        // no-op
      }
     
      mutations.clear();
      m = new Mutation();
      m.column = bytes("entry:num");
      m.value = bytes("-999");
      mutations.add(m);
      m = new Mutation();
      m.column = bytes("entry:sqr");
      m.isDelete = true;
      client.mutateRowTs(t, row, mutations, 1); // shouldn't override latest
      printRow(row, client.getRow(t, row));

      ArrayList<byte[]> versions = client.getVer(t, row, bytes("entry:num"), 10);
      printVersions(row, versions);
      if (versions.size() != 4) {
        System.out.println("FATAL: wrong # of versions");
        System.exit(-1);
      }
     
      try {
        client.get(t, row, bytes("entry:foo"));
        System.out.println("FATAL: shouldn't get here");
        System.exit(-1);
      } catch (NotFound nf2) {
        // blank
      }

      System.out.println("");
    }
   
    // scan all rows/columnNames
   
    columnNames.clear();
    for (ColumnDescriptor col2 : client.getColumnDescriptors(t).values()) {
      columnNames.add(col2.name);
    }
   
    System.out.println("Starting scanner...");
    scanner = client.scannerOpenWithStop(t, bytes("00020"), bytes("00040"),
        columnNames);
    try {
      while (true) {
        ScanEntry value = client.scannerGet(scanner);
        printEntry(value);
      }
    } catch (NotFound nf) {
      client.scannerClose(scanner);
      System.out.println("Scanner finished");
    }
   
    transport.close();
  }
View Full Code Here

  }
 
  private void run() throws IOError, TException, NotFound, IllegalArgument,
      AlreadyExists {
   
    TTransport transport = new TSocket("localhost", port);
    TProtocol protocol = new TBinaryProtocol(transport, true, true);
    Hbase.Client client = new Hbase.Client(protocol);

    transport.open();

    byte[] t = bytes("demo_table");
   
    //
    // Scan all tables, look for the demo table and delete it.
    //
    System.out.println("scanning tables...");
    for (byte[] name : client.getTableNames()) {
      System.out.println("  found: " + utf8(name));
      if (utf8(name).equals(utf8(t))) {
        System.out.println("    disabling table: " + utf8(name));
        if (client.isTableEnabled(name))
          client.disableTable(name);
        System.out.println("    deleting table: " + utf8(name));
        client.deleteTable(name);
      }
    }
   
    //
    // Create the demo table with two column families, entry: and unused:
    //
    ArrayList<ColumnDescriptor> columns = new ArrayList<ColumnDescriptor>();
    ColumnDescriptor col = null;
    col = new ColumnDescriptor();
    col.name = bytes("entry:");
    col.maxVersions = 10;
    columns.add(col);
    col = new ColumnDescriptor();
    col.name = bytes("unused:");
    columns.add(col);

    System.out.println("creating table: " + utf8(t));
    try {
      client.createTable(t, columns);
    } catch (AlreadyExists ae) {
      System.out.println("WARN: " + ae.message);
    }
   
    System.out.println("column families in " + utf8(t) + ": ");
    Map<byte[], ColumnDescriptor> columnMap = client.getColumnDescriptors(t);
    for (ColumnDescriptor col2 : columnMap.values()) {
      System.out.println("  column: " + utf8(col2.name) + ", maxVer: " + Integer.toString(col2.maxVersions));
    }
   
    //
    // Test UTF-8 handling
    //
    byte[] invalid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xfc, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1 };
    byte[] valid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xE7, (byte) 0x94, (byte) 0x9F, (byte) 0xE3, (byte) 0x83, (byte) 0x93, (byte) 0xE3, (byte) 0x83, (byte) 0xBC, (byte) 0xE3, (byte) 0x83, (byte) 0xAB};

    ArrayList<Mutation> mutations;
    // non-utf8 is fine for data
    mutations = new ArrayList<Mutation>();
    mutations.add(new Mutation(false, bytes("entry:foo"), invalid));
    client.mutateRow(t, bytes("foo"), mutations);

    // try empty strings
    mutations = new ArrayList<Mutation>();
    mutations.add(new Mutation(false, bytes("entry:"), bytes("")));
    client.mutateRow(t, bytes(""), mutations);

    // this row name is valid utf8
    mutations = new ArrayList<Mutation>();
    mutations.add(new Mutation(false, bytes("entry:foo"), valid));
    client.mutateRow(t, bytes("foo"), mutations);
   
    // non-utf8 is not allowed in row names
    try {
      mutations = new ArrayList<Mutation>();
      mutations.add(new Mutation(false, bytes("entry:foo"), invalid));
      client.mutateRow(t, invalid, mutations);
      System.out.println("FATAL: shouldn't get here");
      System.exit(-1);
    } catch (IOError e) {
      System.out.println("expected error: " + e.message);
    }
   
    // Run a scanner on the rows we just created
    ArrayList<byte[]> columnNames = new ArrayList<byte[]>();
    columnNames.add(bytes("entry:"));
   
    System.out.println("Starting scanner...");
    int scanner = client.scannerOpen(t, bytes(""), columnNames);
    try {
      while (true) {
        TRowResult entry = client.scannerGet(scanner);
        printRow(entry);
      }
    } catch (NotFound nf) {
      client.scannerClose(scanner);
      System.out.println("Scanner finished");
    }
   
    //
    // Run some operations on a bunch of rows
    //
    for (int i = 100; i >= 0; --i) {
      // format row keys as "00000" to "00100"
      NumberFormat nf = NumberFormat.getInstance();
      nf.setMinimumIntegerDigits(5);
      nf.setGroupingUsed(false);
      byte[] row = bytes(nf.format(i));
     
      mutations = new ArrayList<Mutation>();
      mutations.add(new Mutation(false, bytes("unused:"), bytes("DELETE_ME")));
      client.mutateRow(t, row, mutations);
      printRow(client.getRow(t, row));
      client.deleteAllRow(t, row);

      mutations = new ArrayList<Mutation>();
      mutations.add(new Mutation(false, bytes("entry:num"), bytes("0")));
      mutations.add(new Mutation(false, bytes("entry:foo"), bytes("FOO")));
      client.mutateRow(t, row, mutations);
      printRow(client.getRow(t, row));

      Mutation m = null;
      mutations = new ArrayList<Mutation>();
      m = new Mutation();
      m.column = bytes("entry:foo");
      m.isDelete = true;
      mutations.add(m);
      m = new Mutation();
      m.column = bytes("entry:num");
      m.value = bytes("-1");
      mutations.add(m);
      client.mutateRow(t, row, mutations);
      printRow(client.getRow(t, row));
     
      mutations = new ArrayList<Mutation>();
      mutations.add(new Mutation(false, bytes("entry:num"), bytes(Integer.toString(i))));
      mutations.add(new Mutation(false, bytes("entry:sqr"), bytes(Integer.toString(i * i))));
      client.mutateRow(t, row, mutations);
      printRow(client.getRow(t, row));

      // sleep to force later timestamp
      try {
        Thread.sleep(50);
      } catch (InterruptedException e) {
        // no-op
      }
     
      mutations.clear();
      m = new Mutation();
      m.column = bytes("entry:num");
      m.value = bytes("-999");
      mutations.add(m);
      m = new Mutation();
      m.column = bytes("entry:sqr");
      m.isDelete = true;
      client.mutateRowTs(t, row, mutations, 1); // shouldn't override latest
      printRow(client.getRow(t, row));

      List<TCell> versions = client.getVer(t, row, bytes("entry:num"), 10);
      printVersions(row, versions);
      if (versions.size() != 4) {
        System.out.println("FATAL: wrong # of versions");
        System.exit(-1);
      }
     
      try {
        client.get(t, row, bytes("entry:foo"));
        System.out.println("FATAL: shouldn't get here");
        System.exit(-1);
      } catch (NotFound nf2) {
        // blank
      }

      System.out.println("");
    }
   
    // scan all rows/columnNames
   
    columnNames.clear();
    for (ColumnDescriptor col2 : client.getColumnDescriptors(t).values()) {
      System.out.println("column name is " + new String(col2.name));
      System.out.println(col2.toString());
      columnNames.add((utf8(col2.name) + ":").getBytes());
    }
   
    System.out.println("Starting scanner...");
    scanner = client.scannerOpenWithStop(t, bytes("00020"), bytes("00040"),
        columnNames);
    try {
      while (true) {
        TRowResult entry = client.scannerGet(scanner);
        printRow(entry);
      }
    } catch (NotFound nf) {
      client.scannerClose(scanner);
      System.out.println("Scanner finished");
    }
   
    transport.close();
  }
View Full Code Here

    }
  }
 
  private void run() throws IOError, TException, NotFound, IllegalArgument,
      AlreadyExists {
    TTransport transport = new TSocket("localhost", port);
    TProtocol protocol = new TBinaryProtocol(transport, true, true);
    Hbase.Client client = new Hbase.Client(protocol);

    transport.open();

    byte[] t = bytes("demo_table");
   
    //
    // Scan all tables, look for the demo table and delete it.
    //
    System.out.println("scanning tables...");
    for (byte[] name : client.getTableNames()) {
      System.out.println("  found: " + utf8(name));
      if (utf8(name).equals(utf8(t))) {
        System.out.println("    deleting table: " + utf8(name))
        client.deleteTable(name);
      }
    }
   
    //
    // Create the demo table with two column families, entry: and unused:
    //
    ArrayList<ColumnDescriptor> columns = new ArrayList<ColumnDescriptor>();
    ColumnDescriptor col = null;
    col = new ColumnDescriptor();
    col.name = bytes("entry:");
    col.maxVersions = 10;
    columns.add(col);
    col = new ColumnDescriptor();
    col.name = bytes("unused:");
    columns.add(col);

    System.out.println("creating table: " + utf8(t));
    try {
      client.createTable(t, columns);
    } catch (AlreadyExists ae) {
      System.out.println("WARN: " + ae.message);
    }
   
    System.out.println("column families in " + utf8(t) + ": ");
    AbstractMap<byte[], ColumnDescriptor> columnMap = client.getColumnDescriptors(t);
    for (ColumnDescriptor col2 : columnMap.values()) {
      System.out.println("  column: " + utf8(col2.name) + ", maxVer: " + Integer.toString(col2.maxVersions));
    }
   
    //
    // Test UTF-8 handling
    //
    byte[] invalid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xfc, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1 };
    byte[] valid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xE7, (byte) 0x94, (byte) 0x9F, (byte) 0xE3, (byte) 0x83, (byte) 0x93, (byte) 0xE3, (byte) 0x83, (byte) 0xBC, (byte) 0xE3, (byte) 0x83, (byte) 0xAB};

    // non-utf8 is fine for data
    client.put(t, bytes("foo"), bytes("entry:foo"), invalid);

    // try empty strings
    client.put(t, bytes(""), bytes("entry:"), bytes(""));
   
    // this row name is valid utf8
    client.put(t, valid, bytes("entry:foo"), valid);
   
    // non-utf8 is not allowed in row names
    try {
      client.put(t, invalid, bytes("entry:foo"), invalid);
      System.out.println("FATAL: shouldn't get here");
      System.exit(-1);
    } catch (IOError e) {
      System.out.println("expected error: " + e.message);
    }
   
    // Run a scanner on the rows we just created
    ArrayList<byte[]> columnNames = new ArrayList<byte[]>();
    columnNames.add(bytes("entry:"));
   
    System.out.println("Starting scanner...");
    int scanner = client
        .scannerOpen(t, bytes(""), columnNames);
    try {
      while (true) {
        ScanEntry value = client.scannerGet(scanner);
        printEntry(value);
      }
    } catch (NotFound nf) {
      client.scannerClose(scanner);
      System.out.println("Scanner finished");
    }
   
    //
    // Run some operations on a bunch of rows
    //
    for (int i = 100; i >= 0; --i) {
      // format row keys as "00000" to "00100"
      NumberFormat nf = NumberFormat.getInstance();
      nf.setMinimumIntegerDigits(5);
      nf.setGroupingUsed(false);
      byte[] row = bytes(nf.format(i));
     
      client.put(t, row, bytes("unused:"), bytes("DELETE_ME"));
      printRow(row, client.getRow(t, row));
      client.deleteAllRow(t, row);

      client.put(t, row, bytes("entry:num"), bytes("0"));
      client.put(t, row, bytes("entry:foo"), bytes("FOO"));
      printRow(row, client.getRow(t, row));

      Mutation m = null;     
      ArrayList<Mutation> mutations = new ArrayList<Mutation>();
      m = new Mutation();
      m.column = bytes("entry:foo");
      m.isDelete = true;
      mutations.add(m);
      m = new Mutation();
      m.column = bytes("entry:num");
      m.value = bytes("-1");
      mutations.add(m);
      client.mutateRow(t, row, mutations);
      printRow(row, client.getRow(t, row));
     
      client.put(t, row, bytes("entry:num"), bytes(Integer.toString(i)));
      client.put(t, row, bytes("entry:sqr"), bytes(Integer.toString(i * i)));
      printRow(row, client.getRow(t, row));

      // sleep to force later timestamp
      try {
        Thread.sleep(50);
      } catch (InterruptedException e) {
        // no-op
      }
     
      mutations.clear();
      m = new Mutation();
      m.column = bytes("entry:num");
      m.value = bytes("-999");
      mutations.add(m);
      m = new Mutation();
      m.column = bytes("entry:sqr");
      m.isDelete = true;
      client.mutateRowTs(t, row, mutations, 1); // shouldn't override latest
      printRow(row, client.getRow(t, row));

      ArrayList<byte[]> versions = client.getVer(t, row, bytes("entry:num"), 10);
      printVersions(row, versions);
      if (versions.size() != 4) {
        System.out.println("FATAL: wrong # of versions");
        System.exit(-1);
      }
     
      try {
        client.get(t, row, bytes("entry:foo"));
        System.out.println("FATAL: shouldn't get here");
        System.exit(-1);
      } catch (NotFound nf2) {
        // blank
      }

      System.out.println("");
    }
   
    // scan all rows/columnNames
   
    columnNames.clear();
    for (ColumnDescriptor col2 : client.getColumnDescriptors(t).values()) {
      columnNames.add(col2.name);
    }
   
    System.out.println("Starting scanner...");
    scanner = client.scannerOpenWithStop(t, bytes("00020"), bytes("00040"),
        columnNames);
    try {
      while (true) {
        ScanEntry value = client.scannerGet(scanner);
        printEntry(value);
      }
    } catch (NotFound nf) {
      client.scannerClose(scanner);
      System.out.println("Scanner finished");
    }
   
    transport.close();
  }
View Full Code Here

  }
 
  private void run() throws IOError, TException, NotFound, IllegalArgument,
      AlreadyExists {
   
    TTransport transport = new TSocket("localhost", port);
    TProtocol protocol = new TBinaryProtocol(transport, true, true);
    Hbase.Client client = new Hbase.Client(protocol);

    transport.open();

    byte[] t = bytes("demo_table");
   
    //
    // Scan all tables, look for the demo table and delete it.
    //
    System.out.println("scanning tables...");
    for (byte[] name : client.getTableNames()) {
      System.out.println("  found: " + utf8(name));
      if (utf8(name).equals(utf8(t))) {
        if (client.isTableEnabled(name)) {
          System.out.println("    disabling table: " + utf8(name));
          client.disableTable(name);
        }
        System.out.println("    deleting table: " + utf8(name));
        client.deleteTable(name);
      }
    }
   
    //
    // Create the demo table with two column families, entry: and unused:
    //
    ArrayList<ColumnDescriptor> columns = new ArrayList<ColumnDescriptor>();
    ColumnDescriptor col = null;
    col = new ColumnDescriptor();
    col.name = bytes("entry:");
    col.maxVersions = 10;
    columns.add(col);
    col = new ColumnDescriptor();
    col.name = bytes("unused:");
    columns.add(col);

    System.out.println("creating table: " + utf8(t));
    try {
      client.createTable(t, columns);
    } catch (AlreadyExists ae) {
      System.out.println("WARN: " + ae.message);
    }
   
    System.out.println("column families in " + utf8(t) + ": ");
    Map<byte[], ColumnDescriptor> columnMap = client.getColumnDescriptors(t);
    for (ColumnDescriptor col2 : columnMap.values()) {
      System.out.println("  column: " + utf8(col2.name) + ", maxVer: " + Integer.toString(col2.maxVersions));
    }
   
    //
    // Test UTF-8 handling
    //
    byte[] invalid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xfc, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1 };
    byte[] valid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xE7, (byte) 0x94, (byte) 0x9F, (byte) 0xE3, (byte) 0x83, (byte) 0x93, (byte) 0xE3, (byte) 0x83, (byte) 0xBC, (byte) 0xE3, (byte) 0x83, (byte) 0xAB};

    ArrayList<Mutation> mutations;
    // non-utf8 is fine for data
    mutations = new ArrayList<Mutation>();
    mutations.add(new Mutation(false, bytes("entry:foo"), invalid));
    client.mutateRow(t, bytes("foo"), mutations);

    // try empty strings
    mutations = new ArrayList<Mutation>();
    mutations.add(new Mutation(false, bytes("entry:"), bytes("")));
    client.mutateRow(t, bytes(""), mutations);

    // this row name is valid utf8
    mutations = new ArrayList<Mutation>();
    mutations.add(new Mutation(false, bytes("entry:foo"), valid));
    client.mutateRow(t, valid, mutations);
   
    // non-utf8 is not allowed in row names
    try {
      mutations = new ArrayList<Mutation>();
      mutations.add(new Mutation(false, bytes("entry:foo"), invalid));
      client.mutateRow(t, invalid, mutations);
      System.out.println("FATAL: shouldn't get here");
      System.exit(-1);
    } catch (IOError e) {
      System.out.println("expected error: " + e.message);
    }
   
    // Run a scanner on the rows we just created
    ArrayList<byte[]> columnNames = new ArrayList<byte[]>();
    columnNames.add(bytes("entry:"));
   
    System.out.println("Starting scanner...");
    int scanner = client.scannerOpen(t, bytes(""), columnNames);
    try {
      while (true) {
        TRowResult entry = client.scannerGet(scanner);
        printRow(entry);
      }
    } catch (NotFound nf) {
      client.scannerClose(scanner);
      System.out.println("Scanner finished");
    }
   
    //
    // Run some operations on a bunch of rows
    //
    for (int i = 100; i >= 0; --i) {
      // format row keys as "00000" to "00100"
      NumberFormat nf = NumberFormat.getInstance();
      nf.setMinimumIntegerDigits(5);
      nf.setGroupingUsed(false);
      byte[] row = bytes(nf.format(i));
     
      mutations = new ArrayList<Mutation>();
      mutations.add(new Mutation(false, bytes("unused:"), bytes("DELETE_ME")));
      client.mutateRow(t, row, mutations);
      printRow(client.getRow(t, row));
      client.deleteAllRow(t, row);

      mutations = new ArrayList<Mutation>();
      mutations.add(new Mutation(false, bytes("entry:num"), bytes("0")));
      mutations.add(new Mutation(false, bytes("entry:foo"), bytes("FOO")));
      client.mutateRow(t, row, mutations);
      printRow(client.getRow(t, row));

      Mutation m = null;
      mutations = new ArrayList<Mutation>();
      m = new Mutation();
      m.column = bytes("entry:foo");
      m.isDelete = true;
      mutations.add(m);
      m = new Mutation();
      m.column = bytes("entry:num");
      m.value = bytes("-1");
      mutations.add(m);
      client.mutateRow(t, row, mutations);
      printRow(client.getRow(t, row));
     
      mutations = new ArrayList<Mutation>();
      mutations.add(new Mutation(false, bytes("entry:num"), bytes(Integer.toString(i))));
      mutations.add(new Mutation(false, bytes("entry:sqr"), bytes(Integer.toString(i * i))));
      client.mutateRow(t, row, mutations);
      printRow(client.getRow(t, row));

      // sleep to force later timestamp
      try {
        Thread.sleep(50);
      } catch (InterruptedException e) {
        // no-op
      }
     
      mutations.clear();
      m = new Mutation();
      m.column = bytes("entry:num");
      m.value = bytes("-999");
      mutations.add(m);
      m = new Mutation();
      m.column = bytes("entry:sqr");
      m.isDelete = true;
      client.mutateRowTs(t, row, mutations, 1); // shouldn't override latest
      printRow(client.getRow(t, row));

      List<TCell> versions = client.getVer(t, row, bytes("entry:num"), 10);
      printVersions(row, versions);
      if (versions.size() != 4) {
        System.out.println("FATAL: wrong # of versions");
        System.exit(-1);
      }
     
      try {
        client.get(t, row, bytes("entry:foo"));
        System.out.println("FATAL: shouldn't get here");
        System.exit(-1);
      } catch (NotFound nf2) {
        // blank
      }

      System.out.println("");
    }
   
    // scan all rows/columnNames
   
    columnNames.clear();
    for (ColumnDescriptor col2 : client.getColumnDescriptors(t).values()) {
      System.out.println("column with name: " + new String(col2.name));
      System.out.println(col2.toString());
      columnNames.add((utf8(col2.name) + ":").getBytes());
    }
   
    System.out.println("Starting scanner...");
    scanner = client.scannerOpenWithStop(t, bytes("00020"), bytes("00040"),
        columnNames);
    try {
      while (true) {
        TRowResult entry = client.scannerGet(scanner);
        printRow(entry);
      }
    } catch (NotFound nf) {
      client.scannerClose(scanner);
      System.out.println("Scanner finished");
    }
   
    transport.close();
  }
View Full Code Here

  }
 
  private void run() throws IOError, TException, NotFound, IllegalArgument,
      AlreadyExists {
   
    TTransport transport = new TSocket("localhost", port);
    TProtocol protocol = new TBinaryProtocol(transport, true, true);
    Hbase.Client client = new Hbase.Client(protocol);

    transport.open();

    byte[] t = bytes("demo_table");
   
    //
    // Scan all tables, look for the demo table and delete it.
    //
    System.out.println("scanning tables...");
    for (byte[] name : client.getTableNames()) {
      System.out.println("  found: " + utf8(name));
      if (utf8(name).equals(utf8(t))) {
        if (client.isTableEnabled(name)) {
          System.out.println("    disabling table: " + utf8(name));
          client.disableTable(name);
        }
        System.out.println("    deleting table: " + utf8(name));
        client.deleteTable(name);
      }
    }
   
    //
    // Create the demo table with two column families, entry: and unused:
    //
    ArrayList<ColumnDescriptor> columns = new ArrayList<ColumnDescriptor>();
    ColumnDescriptor col = null;
    col = new ColumnDescriptor();
    col.name = bytes("entry:");
    col.maxVersions = 10;
    columns.add(col);
    col = new ColumnDescriptor();
    col.name = bytes("unused:");
    columns.add(col);

    System.out.println("creating table: " + utf8(t));
    try {
      client.createTable(t, columns);
    } catch (AlreadyExists ae) {
      System.out.println("WARN: " + ae.message);
    }
   
    System.out.println("column families in " + utf8(t) + ": ");
    Map<byte[], ColumnDescriptor> columnMap = client.getColumnDescriptors(t);
    for (ColumnDescriptor col2 : columnMap.values()) {
      System.out.println("  column: " + utf8(col2.name) + ", maxVer: " + Integer.toString(col2.maxVersions));
    }
   
    //
    // Test UTF-8 handling
    //
    byte[] invalid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xfc, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1 };
    byte[] valid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xE7, (byte) 0x94, (byte) 0x9F, (byte) 0xE3, (byte) 0x83, (byte) 0x93, (byte) 0xE3, (byte) 0x83, (byte) 0xBC, (byte) 0xE3, (byte) 0x83, (byte) 0xAB};

    ArrayList<Mutation> mutations;
    // non-utf8 is fine for data
    mutations = new ArrayList<Mutation>();
    mutations.add(new Mutation(false, bytes("entry:foo"), invalid));
    client.mutateRow(t, bytes("foo"), mutations);

    // try empty strings
    mutations = new ArrayList<Mutation>();
    mutations.add(new Mutation(false, bytes("entry:"), bytes("")));
    client.mutateRow(t, bytes(""), mutations);

    // this row name is valid utf8
    mutations = new ArrayList<Mutation>();
    mutations.add(new Mutation(false, bytes("entry:foo"), valid));
    client.mutateRow(t, valid, mutations);
   
    // non-utf8 is not allowed in row names
    try {
      mutations = new ArrayList<Mutation>();
      mutations.add(new Mutation(false, bytes("entry:foo"), invalid));
      client.mutateRow(t, invalid, mutations);
      System.out.println("FATAL: shouldn't get here");
      System.exit(-1);
    } catch (IOError e) {
      System.out.println("expected error: " + e.message);
    }
   
    // Run a scanner on the rows we just created
    ArrayList<byte[]> columnNames = new ArrayList<byte[]>();
    columnNames.add(bytes("entry:"));
   
    System.out.println("Starting scanner...");
    int scanner = client.scannerOpen(t, bytes(""), columnNames);
    try {
      while (true) {
        TRowResult entry = client.scannerGet(scanner);
        printRow(entry);
      }
    } catch (NotFound nf) {
      client.scannerClose(scanner);
      System.out.println("Scanner finished");
    }
   
    //
    // Run some operations on a bunch of rows
    //
    for (int i = 100; i >= 0; --i) {
      // format row keys as "00000" to "00100"
      NumberFormat nf = NumberFormat.getInstance();
      nf.setMinimumIntegerDigits(5);
      nf.setGroupingUsed(false);
      byte[] row = bytes(nf.format(i));
     
      mutations = new ArrayList<Mutation>();
      mutations.add(new Mutation(false, bytes("unused:"), bytes("DELETE_ME")));
      client.mutateRow(t, row, mutations);
      printRow(client.getRow(t, row));
      client.deleteAllRow(t, row);

      mutations = new ArrayList<Mutation>();
      mutations.add(new Mutation(false, bytes("entry:num"), bytes("0")));
      mutations.add(new Mutation(false, bytes("entry:foo"), bytes("FOO")));
      client.mutateRow(t, row, mutations);
      printRow(client.getRow(t, row));

      Mutation m = null;
      mutations = new ArrayList<Mutation>();
      m = new Mutation();
      m.column = bytes("entry:foo");
      m.isDelete = true;
      mutations.add(m);
      m = new Mutation();
      m.column = bytes("entry:num");
      m.value = bytes("-1");
      mutations.add(m);
      client.mutateRow(t, row, mutations);
      printRow(client.getRow(t, row));
     
      mutations = new ArrayList<Mutation>();
      mutations.add(new Mutation(false, bytes("entry:num"), bytes(Integer.toString(i))));
      mutations.add(new Mutation(false, bytes("entry:sqr"), bytes(Integer.toString(i * i))));
      client.mutateRow(t, row, mutations);
      printRow(client.getRow(t, row));

      // sleep to force later timestamp
      try {
        Thread.sleep(50);
      } catch (InterruptedException e) {
        // no-op
      }
     
      mutations.clear();
      m = new Mutation();
      m.column = bytes("entry:num");
      m.value = bytes("-999");
      mutations.add(m);
      m = new Mutation();
      m.column = bytes("entry:sqr");
      m.isDelete = true;
      client.mutateRowTs(t, row, mutations, 1); // shouldn't override latest
      printRow(client.getRow(t, row));

      List<TCell> versions = client.getVer(t, row, bytes("entry:num"), 10);
      printVersions(row, versions);
      if (versions.size() != 4) {
        System.out.println("FATAL: wrong # of versions");
        System.exit(-1);
      }
     
      try {
        client.get(t, row, bytes("entry:foo"));
        System.out.println("FATAL: shouldn't get here");
        System.exit(-1);
      } catch (NotFound nf2) {
        // blank
      }

      System.out.println("");
    }
   
    // scan all rows/columnNames
   
    columnNames.clear();
    for (ColumnDescriptor col2 : client.getColumnDescriptors(t).values()) {
      System.out.println("column with name: " + new String(col2.name));
      System.out.println(col2.toString());
      columnNames.add((utf8(col2.name) + ":").getBytes());
    }
   
    System.out.println("Starting scanner...");
    scanner = client.scannerOpenWithStop(t, bytes("00020"), bytes("00040"),
        columnNames);
    try {
      while (true) {
        TRowResult entry = client.scannerGet(scanner);
        printRow(entry);
      }
    } catch (NotFound nf) {
      client.scannerClose(scanner);
      System.out.println("Scanner finished");
    }
   
    transport.close();
  }
View Full Code Here

        }
      } catch (Exception x) {
        x.printStackTrace();
      }

      TTransport transport;

      if (url != null) {
        transport = new THttpClient(url);
      } else {
        TSocket socket = new TSocket(host, port);
        socket.setTimeout(socketTimeout);
        transport = socket;
        if (framed) {
          transport = new TFramedTransport(transport);
        } else if (header) {
          THeaderTransport htrans = new THeaderTransport(transport);
          htrans.addTransform(THeaderTransport.Transforms.ZLIB_TRANSFORM);
          // Test that decode works with multiple small blocks
          htrans.setZlibBufferSize(10);
          transport = htrans;
        }
      }

      TProtocol prot;
      if (header) {
        prot = new TCompactProtocol(transport);
      } else {
        prot = new TBinaryProtocol(transport);
      }
      ThriftTest.Client testClient =
        new ThriftTest.Client(prot);
      Insanity insane = new Insanity();

      long timeMin = 0;
      long timeMax = 0;
      long timeTot = 0;

      for (int test = 0; test < numTests; ++test) {

        /**
         * CONNECT TEST
         */
        System.out.println("Test #" + (test+1) + ", " + "connect " + host + ":" + port);
        try {
          transport.open();
        } catch (TTransportException ttx) {
          System.out.println("Connect failed: " + ttx.getMessage());
          continue;
        }

        long start = System.nanoTime();

        /**
         * VOID TEST
         */
        try {
          System.out.print("testVoid()");
          testClient.testVoid();
          System.out.print(" = void\n");
        } catch (TApplicationException tax) {
          tax.printStackTrace();
        }

        /**
         * STRING TEST
         */
        System.out.print("testString(\"Test\")");
        String s = testClient.testString("Test");
        System.out.print(" = \"" + s + "\"\n");

        /**
         * BYTE TEST
         */
        System.out.print("testByte(1)");
        byte i8 = testClient.testByte((byte)1);
        System.out.print(" = " + i8 + "\n");

        /**
         * I32 TEST
         */
        System.out.print("testI32(-1)");
        int i32 = testClient.testI32(-1);
        System.out.print(" = " + i32 + "\n");

        /**
         * I64 TEST
         */
        System.out.print("testI64(-34359738368)");
        long i64 = testClient.testI64(-34359738368L);
        System.out.print(" = " + i64 + "\n");

        /**
         * DOUBLE TEST
         */
        System.out.print("testDouble(5.325098235)");
        double dub = testClient.testDouble(5.325098235);
        System.out.print(" = " + dub + "\n");

        /**
         * STRUCT TEST
         */
        System.out.print("testStruct({\"Zero\", 1, -3, -5})");
        Xtruct out = new Xtruct();
        out.string_thing = "Zero";
        out.byte_thing = (byte) 1;
        out.i32_thing = -3;
        out.i64_thing = -5;
        Xtruct in = testClient.testStruct(out);
        System.out.print(" = {" + "\"" + in.string_thing + "\", " + in.byte_thing + ", " + in.i32_thing + ", " + in.i64_thing + "}\n");

        /**
         * NESTED STRUCT TEST
         */
        System.out.print("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
        Xtruct2 out2 = new Xtruct2();
        out2.byte_thing = (short)1;
        out2.struct_thing = out;
        out2.i32_thing = 5;
        Xtruct2 in2 = testClient.testNest(out2);
        in = in2.struct_thing;
        System.out.print(" = {" + in2.byte_thing + ", {" + "\"" + in.string_thing + "\", " + in.byte_thing + ", " + in.i32_thing + ", " + in.i64_thing + "}, " + in2.i32_thing + "}\n");

        /**
         * MAP TEST
         */
        Map<Integer,Integer> mapout = new HashMap<Integer,Integer>();
        for (int i = 0; i < 5; ++i) {
          mapout.put(i, i-10);
        }
        System.out.print("testMap({");
        boolean first = true;
        for (int key : mapout.keySet()) {
          if (first) {
            first = false;
          } else {
            System.out.print(", ");
          }
          System.out.print(key + " => " + mapout.get(key));
        }
        System.out.print("})");
        Map<Integer,Integer> mapin = testClient.testMap(mapout);
        System.out.print(" = {");
        first = true;
        for (int key : mapin.keySet()) {
          if (first) {
            first = false;
          } else {
            System.out.print(", ");
          }
          System.out.print(key + " => " + mapout.get(key));
        }
        System.out.print("}\n");

        /**
         * SET TEST
         */
        Set<Integer> setout = new HashSet<Integer>();
        for (int i = -2; i < 3; ++i) {
          setout.add(i);
        }
        System.out.print("testSet({");
        first = true;
        for (int elem : setout) {
          if (first) {
            first = false;
          } else {
            System.out.print(", ");
          }
          System.out.print(elem);
        }
        System.out.print("})");
        Set<Integer> setin = testClient.testSet(setout);
        System.out.print(" = {");
        first = true;
        for (int elem : setin) {
          if (first) {
            first = false;
          } else {
            System.out.print(", ");
          }
          System.out.print(elem);
        }
        System.out.print("}\n");

        /**
         * LIST TEST
         */
        List<Integer> listout = new ArrayList<Integer>();
        for (int i = -2; i < 3; ++i) {
          listout.add(i);
        }
        System.out.print("testList({");
        first = true;
        for (int elem : listout) {
          if (first) {
            first = false;
          } else {
            System.out.print(", ");
          }
          System.out.print(elem);
        }
        System.out.print("})");
        List<Integer> listin = testClient.testList(listout);
        System.out.print(" = {");
        first = true;
        for (int elem : listin) {
          if (first) {
            first = false;
          } else {
            System.out.print(", ");
          }
          System.out.print(elem);
        }
        System.out.print("}\n");

        /**
         * ENUM TEST
         */
        System.out.print("testEnum(ONE)");
        int ret = testClient.testEnum(Numberz.ONE);
        System.out.print(" = " + ret + "\n");

        System.out.print("testEnum(TWO)");
        ret = testClient.testEnum(Numberz.TWO);
        System.out.print(" = " + ret + "\n");

        System.out.print("testEnum(THREE)");
        ret = testClient.testEnum(Numberz.THREE);
        System.out.print(" = " + ret + "\n");

        System.out.print("testEnum(FIVE)");
        ret = testClient.testEnum(Numberz.FIVE);
        System.out.print(" = " + ret + "\n");

        System.out.print("testEnum(EIGHT)");
        ret = testClient.testEnum(Numberz.EIGHT);
        System.out.print(" = " + ret + "\n");

        /**
         * TYPEDEF TEST
         */
        System.out.print("testTypedef(309858235082523)");
        long uid = testClient.testTypedef(309858235082523L);
        System.out.print(" = " + uid + "\n");

        /**
         * NESTED MAP TEST
         */
        System.out.print("testMapMap(1)");
        Map<Integer,Map<Integer,Integer>> mm =
          testClient.testMapMap(1);
        System.out.print(" = {");
        for (int key : mm.keySet()) {
          System.out.print(key + " => {");
          Map<Integer,Integer> m2 = mm.get(key);
          for (int k2 : m2.keySet()) {
            System.out.print(k2 + " => " + m2.get(k2) + ", ");
          }
          System.out.print("}, ");
        }
        System.out.print("}\n");

        /**
         * INSANITY TEST
         */
        insane = new Insanity();
        insane.userMap = new HashMap<Integer, Long>();
        insane.userMap.put(Numberz.FIVE, (long)5000);
        Xtruct truck = new Xtruct();
        truck.string_thing = "Truck";
        truck.byte_thing = (byte)8;
        truck.i32_thing = 8;
        truck.i64_thing = 8;
        insane.xtructs = new ArrayList<Xtruct>();
        insane.xtructs.add(truck);
        System.out.print("testInsanity()");
        Map<Long,Map<Integer,Insanity>> whoa =
          testClient.testInsanity(insane);
        System.out.print(" = {");
        for (long key : whoa.keySet()) {
          Map<Integer,Insanity> val = whoa.get(key);
          System.out.print(key + " => {");

          for (int k2 : val.keySet()) {
            Insanity v2 = val.get(k2);
            System.out.print(k2 + " => {");
            Map<Integer, Long> userMap = v2.userMap;
            System.out.print("{");
            if (userMap != null) {
              for (int k3 : userMap.keySet()) {
                System.out.print(k3 + " => " + userMap.get(k3) + ", ");
              }
            }
            System.out.print("}, ");

            List<Xtruct> xtructs = v2.xtructs;
            System.out.print("{");
            if (xtructs != null) {
              for (Xtruct x : xtructs) {
                System.out.print("{" + "\"" + x.string_thing + "\", " + x.byte_thing + ", " + x.i32_thing + ", "+ x.i64_thing + "}, ");
              }
            }
            System.out.print("}");

            System.out.print("}, ");
          }
          System.out.print("}, ");
        }
        System.out.print("}\n");

        // Test oneway
        System.out.print("testOneway(3)...");
        long startOneway = System.nanoTime();
        testClient.testOneway(3);
        long onewayElapsedMillis = (System.nanoTime() - startOneway) / 1000000;
        if (onewayElapsedMillis > 200) {
          throw new Exception("Oneway test failed: took " +
                              Long.toString(onewayElapsedMillis) +
                              "ms");
        } else {
          System.out.println("Success - took " +
                             Long.toString(onewayElapsedMillis) +
                             "ms");
        }


        long stop = System.nanoTime();
        long tot = stop-start;

        System.out.println("Total time: " + tot/1000 + "us");

        if (timeMin == 0 || tot < timeMin) {
          timeMin = tot;
        }
        if (tot > timeMax) {
          timeMax = tot;
        }
        timeTot += tot;

        transport.close();
      }

      long timeAvg = timeTot / numTests;

      System.out.println("Min time: " + timeMin/1000 + "us");
View Full Code Here

    if (args.length != 2) {
      System.out.println("usage: java -cp build/classes com.facebook.thrift.test.WriteStruct filename proto_factory_class");
      System.out.println("Write out an instance of Fixtures.compactProtocolTestStruct to 'file'. Use a protocol from 'proto_factory_class'.");
    }

    TTransport trans = new TIOStreamTransport(new BufferedOutputStream(new FileOutputStream(args[0])));

    TProtocolFactory factory = (TProtocolFactory)Class.forName(args[1]).newInstance();

    TProtocol proto = factory.getProtocol(trans);

    Fixtures.compactProtoTestStruct.write(proto);
    trans.flush();
  }
View Full Code Here

    r.join();
  }

  public void doTransports(boolean unframed,
                            boolean framed, boolean header) throws TException{
    TTransport transport;
    TSocket socket = new TSocket("localhost", TEST_PORT);
    socket.setTimeout(1000);
    transport = socket;
    TProtocol prot = new TBinaryProtocol(transport);
    if (unframed) {
View Full Code Here

    r.join();
  }

  public void doTransports(boolean unframed,
                            boolean framed, boolean header) throws TException{
    TTransport transport;
    TSocket socket = new TSocket("localhost", TEST_PORT);
    socket.setTimeout(1000);
    transport = socket;
    TProtocol prot = new TBinaryProtocol(transport);
    if (unframed) {
View Full Code Here

TOP

Related Classes of com.facebook.thrift.transport.TTransport

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.