Package org.kiji.schema

Examples of org.kiji.schema.KijiTableWriter


    final KijiTable table = kiji.openTable("table");
    try {
      final EntityId eid = table.getEntityId("eid");

      {
        final KijiTableWriter writer = table.openTableWriter();
        try {
          writer.put(eid, "family", "column", 1L, "me");
          writer.put(eid, "family", "column", 2L, "me-too");
        } finally {
          writer.close();
        }
      }

      final KijiTableReader reader = table.openTableReader();
      try {
View Full Code Here


    final Kiji kiji = getKiji();
    kiji.createTable(KijiTableLayouts.getLayout(LAYOUT_DEVELOPER));
    final KijiTable table = kiji.openTable("dev");
    try {
      final EntityId eid = table.getEntityId("row");
      final KijiTableWriter writer = table.getWriterFactory().openTableWriter();
      try {
        // Registers writer schema 'long':
        writer.put(eid, "info", "user_id", (Long) 1L);

        // Should not need to register writer schema 'long':
        {
          final String layoutIdBefore = table.getLayout().getDesc().getLayoutId();
          writer.put(eid, "info", "user_id", (Long) 2L);
          final String layoutIdAfter = table.getLayout().getDesc().getLayoutId();
          Assert.assertEquals(layoutIdBefore, layoutIdAfter);
        }

        // Register writer schema 'string':
        writer.put(eid, "info", "user_id", "string");

        // Register writer schema for TestRecord1:
        final TestRecord1 record1 = TestRecord1.newBuilder().setInteger(314).build();
        writer.put(eid, "info", "user_id", record1);
      } finally {
        writer.close();
      }

      final List<AvroSchema> expectedIds = Lists.newArrayList(
          AvroSchema.newBuilder()
              .setUid(kiji.getSchemaTable().getOrCreateSchemaId(SCHEMA_LONG))
View Full Code Here

    kiji.createTable(desc);
    final KijiTable table = kiji.openTable("dev");
    try {
      final EntityId eid = table.getEntityId("row");
      final KijiTableWriter writer = table.getWriterFactory().openTableWriter();
      try {
        // Registering 'long' as a writer schema: compatible with 'long' reader schema:
        writer.put(eid, "info", "user_id", (Long) 1L);

        // Should not require additional registration:
        {
          final String layoutIdBefore = table.getLayout().getDesc().getLayoutId();
          writer.put(eid, "info", "user_id", (Long) 2L);
          final String layoutIdAfter = table.getLayout().getDesc().getLayoutId();
          Assert.assertEquals(layoutIdBefore, layoutIdAfter);
        }

        // Registering 'int' as a writer schema: compatible with 'long' reader schema:
        writer.put(eid, "info", "user_id", (Integer) 1);

        // Register writer schema 'string' must fail: incompatible with 'long':
        try {
          writer.put(eid, "info", "user_id", "1");
          Assert.fail("Registering writer schema 'string' should fail.");
        } catch (InvalidLayoutSchemaException ilse) {
          LOG.info("Expected error: {}", ilse.toString());
          Assert.assertTrue(ilse.toString().contains(
              "column: 'info:user_id' "
              + "Reader schema: \"long\" is incompatible with writer schema: \"string\""));
        }
      } finally {
        writer.close();
      }

    } finally {
      table.release();
    }
View Full Code Here

    kiji.createTable(desc);
    final KijiTable table = kiji.openTable("dev");
    try {
      final EntityId eid = table.getEntityId("row");
      final KijiTableWriter writer = table.getWriterFactory().openTableWriter();
      try {
        // Register writer schema TestRecord1, compatible with reader TestRecord2:
        final GenericData.Record record1 = new GenericData.Record(INT_RECORD1);
        record1.put("integer", 314);
        writer.put(eid, "info", "user_id", record1);

        // Register writer schema TestRecord2, is exactly reader TestRecord2:
        final GenericData.Record record2 = new GenericData.Record(INT_TEXT_RECORD1);
        record2.put("integer", 314);
        record2.put("text", "text");
        writer.put(eid, "info", "user_id", record2);

        // Register writer schema TestRecord3, compatible with reader TestRecord2:
        final GenericData.Record record3 = new GenericData.Record(INT_ANOTHER_TEXT_RECORD1);
        record3.put("integer", 314);
        record3.put("another_text", "text");
        writer.put(eid, "info", "user_id", record3);

        // Any primitive type is incompatible with reader schema TestRecord1:
        try {
          writer.put(eid, "info", "user_id", "1");
          Assert.fail("Registering writer schema 'string' should fail.");
        } catch (InvalidLayoutSchemaException ilse) {
          LOG.info("Expected error: {}", ilse.toString());
          Assert.assertTrue(
              ilse.getMessage(),
              ilse.getMessage().contains("In column: 'info:user_id'"));
          Assert.assertTrue(
              ilse.getMessage(),
              ilse.getMessage().contains("is incompatible with writer schema"));
        }

      } finally {
        writer.close();
      }

    } finally {
      table.release();
    }
View Full Code Here

    try {
      assertNotNull(mTable);
      // Fill local variables.
      KijiTableReader mReader = mTable.openTableReader();
      try {
        KijiTableWriter mWriter = mTable.openTableWriter();
        try {
          EntityId mEntityId = mTable.getEntityId("eid-foo");

          mWriter.put(mEntityId, "family", "column", 0L, "Value at timestamp 0.");
          mWriter.put(mEntityId, "family", "column", 1L, "Value at timestamp 1.");

          final KijiDataRequest dataRequest = KijiDataRequest.builder()
              .addColumns(
                  KijiDataRequestBuilder.ColumnsDef.create()
                      .withMaxVersions(2)
                      .add("family", "column"))
              .build();

          // Try this as a get.
          KijiRowData rowData = mReader.get(mEntityId, dataRequest);
          String s = rowData.getValue("family", "column", 0L).toString();
          assertEquals(s, "Value at timestamp 0.");

          // Delete the cell and make sure that this value is missing.
          mWriter.deleteCell(mEntityId, "family", "column", 0L);

          rowData = mReader.get(mEntityId, dataRequest);
          assertFalse(rowData.containsCell("family", "column", 0L));
        } finally {
          mWriter.close();
        }
      } finally {
        mReader.close();
      }
    } finally {
View Full Code Here

      // Write a (generic) record:
      final Schema writerSchema = Schema.createRecord("Found", null, "class.not", false);
      writerSchema.setFields(Lists.newArrayList(
          new Field("field", Schema.create(Schema.Type.STRING), null, null)));

      final KijiTableWriter writer = table.openTableWriter();
      try {
        final GenericData.Record record = new GenericRecordBuilder(writerSchema)
            .set("field", "value")
            .build();
        writer.put(table.getEntityId("eid"), "family", "qualifier", 1L, record);

      } finally {
        writer.close();
      }

      // Read the record back (should be a generic record):
      final KijiTableReader reader = table.openTableReader();
      try {
View Full Code Here

  protected int run(List<String> nonFlagArgs) throws Exception {
    final Kiji kiji = Kiji.Factory.open(mCellURI, getConf());
    try {
      final KijiTable table = kiji.openTable(mCellURI.getTable());
      try {
        final KijiTableWriter writer = table.openTableWriter();
        try {
          for (KijiColumnName column : mCellURI.getColumns()) {
            try {
              final CellSchema cellSchema = table.getLayout().getCellSchema(column);
              if (cellSchema.getType() != SchemaType.COUNTER) {
                LOG.error("Can't increment non counter-type column '{}'", column);
                return FAILURE;
              }
              final EntityId entityId =
                  ToolUtils.createEntityIdFromUserInputs(mEntityId, table.getLayout());
              writer.increment(entityId, column.getFamily(), column.getQualifier(), mValue);

            } catch (IOException ioe) {
              LOG.error("Error while incrementing column '{}'", column);
              return FAILURE;
            }
View Full Code Here

    final WordSynthesizer nameSynth = new WordSynthesizer(random, nameDictionary);
    final NGramSynthesizer fullNameSynth = new NGramSynthesizer(nameSynth, 2);
    final EmailSynthesizer emailSynth = new EmailSynthesizer(random, nameDictionary);

    getPrintStream().printf("Generating %d users on kiji table '%s'...%n", mNumUsers, mTableURI);
    final KijiTableWriter tableWriter = mTable.openTableWriter();
    try {
      for (int iuser = 0; iuser < mNumUsers; iuser++) {
        final String fullName = fullNameSynth.synthesize();
        final String email =
            EmailSynthesizer.formatEmail(fullName.replace(" ", "."), emailSynth.synthesizeDomain());
        final EntityId entityId = mTable.getEntityId(email);
        tableWriter.put(entityId, "info", "name", fullName);
        tableWriter.put(entityId, "info", "email", email);

        // Print some status so the user knows it's working.
        if (iuser % 1000 == 0) {
          getPrintStream().printf("%d rows synthesized...%n", iuser);
        }
View Full Code Here

    final CellSchema cellSchema = layout.getCellSchema(column);

    final EntityId entityId =
        ToolUtils.createEntityIdFromUserInputs(mEntityId, layout);

    final KijiTableWriter writer = mTable.openTableWriter();
    try {
      if (cellSchema.getType() == SchemaType.COUNTER) {
        if (-1 == mTimestamp) {
          try {
            long value = Long.parseLong(mJsonValue);
            writer.put(entityId, column.getFamily(), column.getQualifier(), value);
          } catch (NumberFormatException nfe) {
            LOG.error("Could not parse value flag to a long: " + nfe.getMessage());
            return FAILURE;
          }
        } else {
          LOG.error("Counters do not support writing to a specific timestamp."
              + "  Remove the \"timestamp\" flag.");
          return FAILURE;
        }
      } else {
        // Get writer schema.
        // Otherwise, set writer Schema in mSchema in preparation to write an Avro record.
        if (null != mSchemaFlag) {
          try {
            LOG.debug("Schema is " + mSchemaFlag);
            mSchema = new Schema.Parser().parse(mSchemaFlag);
          } catch (AvroRuntimeException are) {
            LOG.error("Could not parse writer schema: " + are.toString());
            return FAILURE;
          }
        } else {
          try {
            mSchema = layout.getSchema(column);
          } catch (Exception e) {
            LOG.error(e.getMessage());
            return FAILURE;
          }
        }
        Preconditions.checkNotNull(mSchema);

        // Create the Avro record to write.
        GenericDatumReader<Object> reader = new GenericDatumReader<Object>(mSchema);
        Object datum = reader.read(null, new DecoderFactory().jsonDecoder(mSchema, mJsonValue));

        // Write the put.
        if (-1 == mTimestamp) {
          writer.put(entityId, column.getFamily(), column.getQualifier(), datum);
        } else {
          writer.put(entityId, column.getFamily(), column.getQualifier(), mTimestamp, datum);
        }
      }

    } finally {
      ResourceUtils.closeOrLog(writer);
View Full Code Here

    try {
      final HBaseKijiTable htable = HBaseKijiTable.downcast(table);
      final TableLayoutMonitor monitor = htable.getTableLayoutMonitor();
      assertTrue(monitor.getLayoutConsumers().isEmpty());
      // Writers should register with the table as they open.
      final KijiTableWriter writer = table.openTableWriter();
      final KijiBufferedWriter bufferedWriter = table.getWriterFactory().openBufferedWriter();
      final AtomicKijiPutter atomicPutter = table.getWriterFactory().openAtomicPutter();

      // Check that all writers are accounted for.
      final Set<LayoutConsumer> consumers = monitor.getLayoutConsumers();
      assertTrue(consumers.size() == 3);

      // Writers should unregister as they close.
      writer.close();
      bufferedWriter.close();
      atomicPutter.close();

      // Check that no writers remain.
      assertTrue(monitor.getLayoutConsumers().isEmpty());
View Full Code Here

TOP

Related Classes of org.kiji.schema.KijiTableWriter

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.